Pages

Tuesday, November 1, 2011

Sample Script to reboot a telnet enabled device


This is a sample script for a device restart. You can run it in cron schedular. In my script my device is using 10.2.150.2 IP and its  password is:1234. To run this script I need a package named "expect"

Install expect:
apt-get install expect

Now create a file with your code.
vi /usr/local/bin/device-reboot


#!/usr/bin/expect -f

set timeout 5

# router password
set pass "1234"

# router IP address
set routerip "10.2.150.2"


# start telnet
spawn telnet $routerip

# send username & password
expect "Password:"  ### [the prompt should be like Password: in this step on your device]
send -- "$pass\r"


# execute command
expect ">"   ### [the prompt should be like > in this step on your device]
send -- "reboot\r"
# exit
expect ">"  ### [the prompt should be like > in this step on your device]
send -- "^D"

Save and close the file.

Now make it executable. 
#chmod 777 /usr/local/bin/device-reboot

You can include it in your cron schedular to reboot the device periodically.

No comments:

Post a Comment