Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand you correctly you wish to print out to your local console the output of each command which you run on the remote console. I am not sure why it needs to be synchronous except you say that is a requirement. You might want to make sure you understand the requirements. In any case, since your requirement is that the output be printed, you don't need to print your input...</p> <p>I highly recommend storing the output into a variable even if you need to print it immediately simply because I see no benefit of retrieving the data unless you are going to act on the data and if you merely print the data you cannot act on it. Store it in a variable and then it can be printed as well as acted upon. I doubt the human eye would be able to tell the difference in storing it and then writing it all at once rather than piecemeal.</p> <p>Your try block, as written, will never happen because you have to read from the telnet session first before you can evaluate if 'Password:' is on the remote console. </p> <p>Some further suggestions:</p> <p>First, write <code>terminal length 0</code>, that will avoid having to handle paused output. </p> <p>Second, since I am lazy, any variables I know I am only using to pass to the remote unit I save with a newline character. </p> <p>Third, always give it a timeout or else it runs the risk of waiting forever for a match that might never come.</p> <p>Fourth, have a look at <code>Telnet.expect(list, [timeout])</code>. I find it far more useful than a simple read_until; it allows you to look for multiple responses and act on each of them accordingly. It is quite useful for handling errors. It returns a three item tuple that represents the index of the matched item (-1 if no match) as well as the matched text (or everything in the buffer if no match). </p> <p>Fifth, write a decorator for your telnet session to log in. You know it will be used at least once every time you interact with a remote unit, and more if you are loading new firmware. Develop a library of functions that you can reuse rather than writing it out each time. Lazy is good. </p> <pre><code>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(["&gt;",],5) error_check(tmp) tn.write('en\n') tmp=expect(["Password", "#'],5) error_check(tmp) if tmp(0)==0: #if someone left enable unlocked, don't send a 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 now tn.write('sho run') print run cfg=tn.expect(["#",], 5) tn.write("logout\n")) bye=tn.expect(["&gt;",], 5) print bye tn.close() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload