Tuesday, April 29, 2008

ssh over shell script, expect the unexpected ;))

wondering about the limitation of ftp.

i was thinking on doing something more than just getting or putting files beetween server when connected, such as running apps or doing some testing on remote system using ssh.

at first i was annoyed by the interactive session of ssh authentification,in which preventing my script from iterating,so i googled and came up with 'expect'.

this is my simple script for testing running ftp on remote server and turn it on if it was turned off.

ps : the key is on what will show up on your terminal and you'll have to grab it with the expect command.

#!/bin/bash
user='safar' #username
host='localhost' #hostname
pass='xxxxxxx' # password
cmd='ftpstart' #your apps or script to run

expect <<> ssh.log
spawn ssh $user@$host
expect "Password:"
send "$pass\r"
expect "#"
send "ps -ef |grep ftp| grep -v grep| wc -l\r"
expect {
0 {
send_user "nyalain ftp\n"
send "$cmd\r"
exp_continue
} 1 {
send_user "ftp udah nyala\n"
exit
} -re #
}
send "exit\r"
eof
exit 0


my output ssh.log file is like this.

spawn ssh root@localhost
Password:
Welcome to Syafar Computer ;))
=============================

Please jangan yang aneh2 y kalo masuk ke komputerku, cape' ngebenerinnya tau !!!

You have mail.
root@Z:~# ps -ef |grep ftp| grep -v grep| wc -l
1
root@Z:~# ftp udah nyala

0 Comment :