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,