python - How do I get a line of the what the console returns (string) and place in a variable? -
i have code using telnet , requires login. if login incorrect, returns "incorrect login" console. want catch exception , skip doesn't stop program. tried below:
try: session.write("username".encode('ascii') + b"\r") session.write("password".encode('ascii') + b"\r") ***this point console return "incorrect login"*** except sys.stdout == "incorrect login": print(sys.stdout) pass else: **rest of code**
it seems never catches output, continues on code , ends in index error (from not having data need logging in). tried searching had no luck. appreciated. i'm running python 3.3 , still learning. thanks!
edit: here telnet shows
login: badusername password: **blank b/c pw field** login incorrect login:
edit2: code else (edited confidentiality)
import telnetlib, time import sys, string, socket import cx_oracle sql = "select column table" con = cx_oracle.connect("login info blah...") cur = con.cursor() cur.execute(sql) row = cur.fetchone() rows = cur.fetchall() def tup(): return (rows) cur.close() con.close() = 0 while < len(rows): host = tup()[i][0] timeout = 120 print(host + ' =', end = ' ') try: session = telnetlib.telnet(host, 23, timeout) except: out = open("data.txt",'a') out.write(host + " = failed\n") print("failed") else: try: session.write("username".encode('ascii') + b"\r") session.write("pass".encode('ascii') + b"\r") except sys.stdout == "incorrect login": print(sys.stdout) pass else:
take [subprocess][1]
module, contains check_output
method returns output of executed command string.
try this. might need change syntactical details...
prompt = '$' # or '#' or '%', shell prompt timeout = 3 try: session.read_until(b"login:") session.write("username".encode('ascii') + b"\r") session.read_until(b"password:") session.write("password".encode('ascii') + b"\r") login_result = session.read_until(prompt, timeout) # make put whatever printed till prompt login_result. if takes more timeout seconds, can assume login failed (since prompt never came up) ***this point console return "incorrect login"*** if(login_result[-1] != prompt): # change -1 -2 or -3 if output trailed newline raise exception except exception: print("login failure") pass else: **rest of code**
Comments
Post a Comment