LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (http://www.linuxquestions.org/questions/linux-networking-3/)
-   -   remote command execute telnet with 1 command (http://www.linuxquestions.org/questions/linux-networking-3/remote-command-execute-telnet-with-1-command-338670/)

MPowers 06-30-2005 06:51 AM

remote command execute telnet with 1 command
 
Hello,
How I can make a script that connects to a remote computer via telnet and executes a single command (reboot).
The reason that I want to do that is because I want to make a simple script that pings some host and if it is down the linux box will connect via telnet to gateway and reboots it.

stefan_nicolau 06-30-2005 07:04 AM

Create a script with expect that will be invoked from a shell script if ping fails:
Code:

#!/usr/bin/expect
spawn telnet mybox
expect {*login:}
send "XXXXXXXX\r"
expect {*password:}
send "XXXXXXXX\r"
expect {*me@mybox# }
send "sudo /sbin/reboot\r"

This code might be slightly wrong. Look at the expect documentation.

WARNING! The username/password combination can be viewed by anyone who reads the script! I use sudo because reboot requires root access, and don't even think about putting the root in a script.

sind 06-30-2005 07:31 AM

If security is an issue, use ssh with a public key setup. Then you could do a remote reboot by running:

$ ssh root@remote.host.com /sbin/reboot

or

$ ssh user@remote.host.com sudo /sbin/reboot

Or even add one of these to your .bashrc file to make it really easy:

alias remote_reboot='ssh root@remote.host.com /sbin/reboot'

alias remote_reboot='ssh user@remote.host.com sudo /sbin/reboot'

There should be heaps of info about setting up sshd properly here, or through Google.

~sind


All times are GMT -5. The time now is 11:04 PM.