python - Execute a CLI command and store in a variable using telnetlib -
i using python telnetlib taking "running config" output router.how store "show running-config" output in variable.and print variable.my requirement each , every output display in console when executing each , every line of code.is there option aviod these print statements.
import telnetlib #import getpass ipaddr = "10.1.1.1" passwd = "abcd123" tn = telnetlib.telnet(ipaddr) if password: try: print (tn.write (password + "\n")) print(tn.read_until("router>")) print(tn.write("enable\n")) print(tn.read_until("password:")) print(tn.write(passwd + "\n")) print(tn.read_until("router#")) print(tn.write("show clock\n")) print(tn.read_until("#")) print(tn.write("show running-config\n")) print(tn.write("\040\n")) print(tn.write("\040\n")) print(tn.write("\040\n")) print(tn.read_until("#")) print(tn.write("logout\n")) print(tn.read_until(">")) print tn.close
if understand correctly wish print out local console output of each command run on remote console. not sure why needs synchronous except requirement. might want make sure understand requirements. in case, since requirement output printed, don't need print input...
i highly recommend storing output variable if need print because see no benefit of retrieving data unless going act on data , if merely print data cannot act on it. store in variable , can printed acted upon. doubt human eye able tell difference in storing , writing @ once rather piecemeal.
your try block, written, never happen because have read telnet session first before can evaluate if 'password:' on remote console.
some further suggestions:
first, write terminal length 0
, avoid having handle paused output.
second, since lazy, variables know using pass remote unit save newline character.
third, give timeout or else runs risk of waiting forever match might never come.
fourth, have @ telnet.expect(list, [timeout])
. find far more useful simple read_until; allows multiple responses , act on each of them accordingly. quite useful handling errors. returns 3 item tuple represents index of matched item (-1 if no match) matched text (or in buffer if no match).
fifth, write decorator telnet session log in. know used @ least once every time interact remote unit, , more if loading new firmware. develop library of functions can reuse rather writing out each time. lazy good.
import telnetlib import sys ipaddr = "10.1.1.1" passwd = "abcd123" def login(tn): global passwd passwd=passwd+'\n' def error_check(tmp): if tmp[0]==-1: print "unexpected response" print "tmp[2] sys.exit() tmp=tn.expect(["password:",], 5) error_check(tmp) tn.write(passwd) tmp=expect([">",],5) error_check(tmp) tn.write('en\n') tmp=expect(["password", "#'],5) error_check(tmp) if tmp(0)==0: #if left enable unlocked, don't send password tn.write(passwd) tmp=expect(["#',],5) error_check(tmp) tn = telnetlib.telnet(ipaddr) login(tn) tn.write('terminal length 0') tmp=expect(["#',],5) tn.write('sho clock') now=tn.expect(["#",], 5) print tn.write('sho run') print run cfg=tn.expect(["#",], 5) tn.write("logout\n")) bye=tn.expect([">",], 5) print bye tn.close()
Comments
Post a Comment