[Tutor] Telnet and pexpect module

alex loutrbringa' castornightmare at gmail.com
Mon Dec 14 13:51:53 CET 2009


Hi everybody,

I hope i'm in the right place. I'm trying to automate an operation we
usually do manually with some RS232(serial port)/ethernet MOXA switches.
This operation reboots a linux debian device by sending a "magic-sysrq+b"
command to the kernel if this device does not respond through SSH.
To do that manually, we do :
# this is the connection to the RS/232-ethernet port, this switch is
connected with the linux device through a serial port
# and connected to my LAN with an ethernet cable. The switch serial port 1
is bound to tcp port 100.
alex at jazz:~$ telnet 10.1.1.1 100
Trying 10.1.1.1 ...
Connected to 10.1.1.1.
Escape character is '^]'.
# here, i can press "enter" to access to the linux prompt
# or i can enter "ctrl+]" to access the telnet prompt
# here, i enter "ctrl + ]" and get the switch prompt
telnet>
# now i enter "send brk" then "enter", this is the same thing than the linux
"magic sysrq"
telnet> send brk
# now i press "b" (without enter), then the device directly "hard reboot"
with a short message like "resetting system"

So, i want to do this with pexpect, here is my script :
-------------------------------
import
pexpect
import
sys

import
time

import
os



switch_ip_address = sys.argv[1]
switch_port = sys.argv[2]
sysrq_key =
sys.argv[3]


print('ip address is: ' +
switch_ip_address)
print('switch port number is: ' +
switch_port)
print('sysrq key is: ' +
sysrq_key)

telnet_child = pexpect.spawn('telnet ' + switch_ip_address + ' ' +
switch_port)
telnet_child.expect('Escape.*')
telnet_child.sendcontrol(']')
telnet_child.expect('telnet> ')
telnet_child.sendline('send brk')
#time.sleep(0.5)
telnet_child.send(sysrq_key)
-------------------------------
It looks like it is working almost fine, when i run my script with :
python send_brk.py 10.1.1.1 100 b
the script end without errors.
Then i connect to the switch to check if the device has rebooted but it has
not :
-------------------------------
athaveau at athaveau:~$ telnet 10.1.1.1 100
Trying 10.1.1.1...
Connected to 10.1.1.1.
Escape character is '^]'.
-------------------------------
Then when i press enter, i get that :
-------------------------------
SysRq : HELP : loglevel0-8 reBoot tErm Full kIll
show-backtrace-all-active-cpus(L) showMem Nice showPc Sync showTasks Unmount
showLocks
-------------------------------
this is the magic sysrq helper, so it looks like the last thing i sent: the
"b" (sysrq_key variable) letter is not sent to the
telnet command but i can't understand why.
I know this is a long and certainly a little bit boring :-) but if someone
has an idea ... it would very pleasant !

Thanks by advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091214/b3833ad9/attachment.htm>


More information about the Tutor mailing list