Friday, February 24, 2012

Install expect on Ubuntu 11

I missed auto log in to remote ssh server or doing automation that I often do. like access ftp server and download file, log in ssh server, push file to FTP server and open a turnelling so on.
There is a way to do very easily.

You need to install 'expect' command.
"expect" command is simply expecting a text from terminal, and send it when it meets expectation.
sudo apt-get install expect
Installation is done.

Sample usage.

Let's make simple shell program
vi abc.sh

Type into shell shell program.
#!/usr/bin/expect -f
set timeout 100
spawn ssh userid@servername 
expect "/home/devtrigger/.ssh/id_rsa" 
send "password\r"
expect "servername:"
send "bash\r"
interact
you should not omit first line "#!/usr/bin/expect -f" it indicate that it will run by expect.
you can ommit "timeout 100" which is waiting for response from server. I used the timeout option for using long process like copy a big file from ftp or etc.
of course, you need to replace certain text like "devtrigger", "password"
save it.

let's make the file executable and run it
chmod 755 abc.sh
./abc.sh

No comments:

Post a Comment