简体   繁体   中英

how i can send more than 2 arguments on telnet session in python?

I have two lists that have user name and password. I want to check which one is correct by iterating through two zipped lists but it isn't working. Below is the traceback

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)

Below is the code throwing the exception.

import telnetlib
import re
HOST = "192.168.1.1"
USER =  ["admin\n","admin\n","admin"]
PASS = ["cpe#","1234"," "]

try:
  tn = telnetlib.Telnet(HOST)
except:
print "Oops! there is a connection error"
frist_log = tn.read_until(":")
 if "log" in frist_log:
while 1:
    for i,j in zip(USER,PASS):
         tn.write(j,i)
    break

Telnet.write(buffer) only takes two arguments first is the telnet object and the other is a buffer to send to your session, hence your solution will throw an exception. One way to solve the problem is like a except script using the expect api and use a list of expected output as shown in example below

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM