The best place to *find* answers to programming/development questions, imo, however it's the *worst* place to *ask* questions (if your first question/comment doesn't get any up-rating/response, then u can't ask anymore questions--ridiculously unrealistic), but again, a great reference for *finding* answers.

My Music (Nickleus)

20120615

automatically connect to remote server by ssh then perform remote commands using expect script


first you need to install expect:
sudo apt-get install expect
then create a new expect script file:
nano -w expect_ssh.exp
and give it this code:
#!/usr/bin/expect
eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no myuser@myremoteserver.example.com
# i know that the remote shell uses $ at the end of their prompt
set prompt "\\$"
expect "*?assword:*"
send -- "mySecretPassword\r"
interact -o -nobuffer -re $prompt return
send -- "rm -f remote_file_to_be_deleted.txt\r"
interact -o -nobuffer -re $prompt return
make the script executable:
chmod +x expect_ssh.exp
run the script like this:
./expect_ssh.exp

thanks to:
http://bash.cyberciti.biz/security/expect-ssh-login-script/
http://stackoverflow.com/questions/4780893/use-expect-in-bash-script-to-provide-password-to-ssh-command
man expect (huge man page, lots to read)

No comments:

Post a Comment