1

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,

Minzkraut
  • 2,149
  • 25
  • 31
Okan Akca
  • 31
  • 7
  • 3
    So are you asking for someone to translate an expect script into a python script? – Eric Renouf Aug 26 '15 at 13:53
  • 1
    Take a look at Python's [telnetlib](https://docs.python.org/3.5/library/telnetlib.html) and put all your commands etc. in an iterable (eg. a dict). Try to iterate through those 'sets' of data and get your stuff done in this for loop. – albert Aug 26 '15 at 13:58
  • Alternately, just continue use `expect` and learn to love the [TCL](https://www.tcl.tk/) language, which like Python is a dynamic scripting language capable of doing everything you need (including loop, command line argument processing, etc). – larsks Aug 26 '15 at 14:08
  • Have you tried [pexpect](https://pexpect.readthedocs.org/en/latest/overview.html)? – primero Aug 26 '15 at 14:11
  • I agree with @albert, `telnetlib` is the way to go. http://stackoverflow.com/questions/10952514/telnetlib-python-example – Joseph Farah Aug 26 '15 at 14:16
  • i'd also like to add that this is a very well written question. nice job, OP. – Joseph Farah Aug 26 '15 at 14:17
  • @EricRenouf no actually. I included what I've done in order to get what I need, so that we can implement a python version of this task. – Okan Akca Aug 27 '15 at 06:27
  • @albert, thank you for your comment. I will now check the areas you mentioned. Regards, – Okan Akca Aug 27 '15 at 06:30
  • @larsks, I want to learn and love python mate.. :) I'll try to form up a suitable loop cycle to suit my needs. Regards, – Okan Akca Aug 27 '15 at 06:33
  • 1
    @silentphoenix, thank you for the heads up! I'll have a look at the example you pointed out and telnetlib as well. Regards, – Okan Akca Aug 27 '15 at 06:37

2 Answers2

0

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!

Okan Akca
  • 31
  • 7
-1

You should be able to use fabric to run same command for multiple servers.

http://www.fabfile.org/

http://docs.fabfile.org/en/1.10/usage/execution.html#roles

yilmazhuseyin
  • 6,442
  • 4
  • 34
  • 38
  • Thanks mate, never heard of this before. I will check it now. – Okan Akca Aug 27 '15 at 12:18
  • It's not a server actually. What he is trying to do run bunch of commands over telnet. I don't think Fab supports telnet as a transport mechanism. All those utilities run via SSH. – Eren T. Aug 28 '15 at 13:22