Search code examples
pythonlinuxbashshelltelnet

Execute commands on remote host via telnet with python script


I've been trying to write a script to simplify a set of my work. I tried it with shell commands but the code looks too straight forward and to be honest too amateur. I'm trying to learn python for scripting and with your help, I'm hoping that this problem of mine could turn into an explanatory practice.

Following is the shell code I've written:

#!/usr/bin/expect 

spawn telnet IPaddress
sleep 0.1
expect "Enter username and password"
send "username password \n"
sleep 0.1
send "debug; \n"
sleep 0.1
send "def t1 suspend_loader \n"
expect "enter subcommands"
send "traceback \n \n;"
sleep 1
send "act t1 \n"
sleep 0.1
send "quit \n"
sleep 0.1
send "stor2tst;audit_modules \n"
expect "PS Checksum audit completed"
send "quit \n"
sleep 0.1
send "debug"
sleep 0.1
send "print t1 \n"
sleep 0.5
send "quit \n"
sleep 0.1
send "logutil;open MOD;back all;quit \n"
sleep 0.1
send "debug \n"
sleep 0.1
send "di modules:pr.514 d 1 (&0) char n=68 \n"
send "quit \n"
sleep 1
send "quit \n"
send "logout \n"



interact

As you may also have guessed, this code is designed for a specific switch interface. Username and password prompt comes in a single line. There are specific shell levels (such as debug level) I require in order to execute several commands. I also used expect module here but I think it just passes on without checking the string in the expect part..

WHAT AM I TRYING TO DO?
I need to telnet to a list of known IP Addresses (20 servers).
All servers have the same username and password.
I need to execute these set of commands on each server and return whatever output there is to separate log files under a specified directory (for example /tmp/dir).
Also, several commands need "double enter" in order to execute! That is why I used \n \n after traceback command.

Any help is appreciated.
Thanks in advance,


Solution

  • I have written myself the following code folks, I hope this can help to people who may look for a similar solution:

    #!/usr/bin/expect
    
    set timeout 150
    
    array set hosts {0 <IPaddr> 1 <IPaddr> 2 <IPaddr> 3 <IPaddr> 4 <IPaddr> 5 <IPaddr> 6 <IPaddr> 7 <IPaddr> 8 <IPaddr> 9 <IPaddr> 10 <IPaddr> 11 <IPaddr>}
    
    for {set i 0} {$i < 12 } { incr i } {
    
    spawn telnet $hosts($i)
    expect "Enter username and password"
    send "root toor \n"
    sleep 2
    expect ">" {send "print '****************$hosts($i)****************' \n"}
    expect ">" {send "command \n"}
    expect ">" {send "command \n"}
    expect ">" {send "command \n"}
    expect "enter subcommands"
    expect ">" {send "command \n\n"}
    expect ">" {send "command \n"}
    expect ">" {send "command \n"}
    expect ">" {send "print '****************$hosts($i)****************' \n"}
    interact
    }
    

    As I mentioned at my description of the problem, this code was specifically designed to work on Nortel DMS type switches, so please execuse me for the possible low level design of the code.. All it matters is that it works and fulfill our needs.. :)

    Cheers!