Rebooting access point via SSH with pexpect... hangs. Any ideas?

Posted by MiniQuark on Server Fault See other posts from Server Fault or by MiniQuark
Published on 2010-11-15T15:14:01Z Indexed on 2012/09/03 15:40 UTC
Read the original article Hit count: 526

Filed under:
|
|

When I want to reboot my D-Link DWL-3200-AP access point from my bash shell, I connect to the AP using ssh and I just type reboot in the CLI interface. After about 30 seconds, the AP is rebooted:

# ssh [email protected]
[email protected]'s password: ********

Welcome to Wireless SSH Console!! ['help' or '?' to see commands]


Wireless Driver Rev 4.0.0.167
D-Link Access Point wlan1 -> reboot

Sound's great? Well unfortunately the ssh client process never exits, for some reason (maybe the AP kills the ssh server a bit too fast, I don't know). My ssh client process is completely blocked (even if I wait for several minutes, nothing happens). I always have to wait for the AP to reboot, then open another shell, find the ssh client process ID (using ps aux | grep ssh) then kill the ssh process using kill <pid>. That's quite annoying.

So I decided to write a python script to reboot the AP. The script connects to the AP's CLI interface via ssh, using python-pexpect, and it tries to launch the "reboot" command. Here's what the script looks like:

#!/usr/bin/python
# usage: python reboot_ap.py {host} {user} {password}

import pexpect
import sys
import time

command = "ssh %(user)s@%(host)s"%{"user":sys.argv[2], "host":sys.argv[1]}
session = pexpect.spawn(command, timeout=30) # start ssh process
response = session.expect(r"password:") # wait for password prompt
session.sendline(sys.argv[3]) # send password
session.expect(" -> ")  # wait for D-Link CLI prompt
session.sendline("reboot") # send the reboot command
time.sleep(60) # make sure the reboot has time to actually take place
session.close(force=True) # kill the ssh process

The script connects properly to the AP (I tried running some other commands than reboot, they work fine), it sends the reboot command, waits for one minute, then kills the ssh process. The problem is: this time, the AP never reboots! I have no idea why.

Any solution, anyone?

© Server Fault or respective owner

Related posts about ssh

Related posts about command-line