如何在python中的telnet会话上发送2个以上的参数?

如何在python中的telnet会话上发送2个以上的参数?,python,networking,telnet,Python,Networking,Telnet,我有两个包含用户名和密码的列表。我想通过遍历两个压缩列表来检查哪一个是正确的,但它不起作用。 下面是回溯 Traceback (most recent call last): File "C:\Users\mohamed\Downloads\second_PassWD_Test.py", line 27, in <module> mme=tn.write(j,i) TypeError: write() takes exactly 2 arguments (3 given) Teln

我有两个包含用户名和密码的列表。我想通过遍历两个压缩列表来检查哪一个是正确的,但它不起作用。 下面是回溯

Traceback (most recent call last):
File "C:\Users\mohamed\Downloads\second_PassWD_Test.py", line 27, in <module>
mme=tn.write(j,i)
TypeError: write() takes exactly 2 arguments (3 given)
Telnet.write(buffer)
只接受两个参数,第一个是Telnet对象,另一个是要发送到会话的缓冲区,因此您的解决方案将引发异常。解决此问题的一种方法类似于使用
expect
api的except脚本,并使用预期输出列表,如下例所示

USER =  ["admin\n","admin\n","admin"]
PASS = ["cpe#","1234"," "]
prompt = "#"    ## or your system prompt 
tn = telnetlib.Telnet(HOST)
first_log = tn.read_until(":")
for user,password in zip(USER,PASS):
    try:
        tn.write(user)
        tn.expect([":"],timeout = 3) # 3 second timeout for password prompt
        tn.write(password+"\n")
        index,obj,string = tn.expect([":",prompt],timeout = 3)
        found = string.find(prompt)
        if found >= 0:
            break # found the username password match break 
        ###else continue for next user/password match    
    except:
        print "exception",sys.exc_info()[0]

更改tn.write(i,j)上的顺序,并检查文档,有很多干净设置的好例子,谢谢Ari,但我找不到任何与我的报价相关的东西,我需要每次通过for循环
telnet.write(缓冲区)从列表中发送项目
只接受缓冲区参数您想实现什么?解决问题的一种方法是创建telnetlib expect脚本,在该脚本中,每次使用
write
时,您都会使用
expect
api调用等待会话。我的朋友cmidi,但不起作用。当我测试它时,我发现代码没有发送用户名“admin”因此,我尝试将用户列表更改为(((USER=“admin”))并发送它,否则我可以通过telnet applaction通过相同的用户和passWD远程登录此设备。。。。你能再和我核对一下吗..HOST=“192.168.1.1”USER=“admin”PASS=[“cpe”;“1234”,“c”]prompt=“>>”##;或者你的系统提示tn=telnetlib.Telnet(HOST)first\u log=tn.read\u直到(“:”)对于用户,密码在zip中(用户,PASS):try:me=tn.write(用户)print me tn.expect([“:”)],timeout=3)#密码提示tn.write(password)index,obj,string=tn.expect([“:”,prompt],timeout=3)find=string.find(prompt)if found>=0:@M.hosseny在哪里添加打印失败或启用调试可能会有所帮助。我添加的代码缺少我添加的
“\n”
。要启用调试
Telnet,请设置调试级别(4)
。另外请注意,
zip(USER,PASS)
如果大小不同,则会将结果压缩到较低的大小列表,这可能会导致问题,如果使用zip,请确保USER和PASS的大小相同
USER =  ["admin\n","admin\n","admin"]
PASS = ["cpe#","1234"," "]
prompt = "#"    ## or your system prompt 
tn = telnetlib.Telnet(HOST)
first_log = tn.read_until(":")
for user,password in zip(USER,PASS):
    try:
        tn.write(user)
        tn.expect([":"],timeout = 3) # 3 second timeout for password prompt
        tn.write(password+"\n")
        index,obj,string = tn.expect([":",prompt],timeout = 3)
        found = string.find(prompt)
        if found >= 0:
            break # found the username password match break 
        ###else continue for next user/password match    
    except:
        print "exception",sys.exc_info()[0]