[Ubuntu] Simple method to login SSH without insert user name and password via expect in linux

All I test the code below is under Ubuntu.

 

1. Install expect

 

sudo apt-get install expect

 

 

2. Create a .sh file, here is login.sh, and insert the code as below

 

#!/usr/bin/expect -f
spawn ssh username@hostname
expect
"password:"
send
"password\r"
interact

Please note that you have to change to codes what is marked with red.

 

 

3. Run it

 

./login.sh

 

 

 

 

By the way, there is an other method that can switch via expect to login many website

 

代码
#!/usr/bin/expect -f
set project [lindex $argv 0]
switch $project {
website1 {
set path user1
@website1.com
set password password1
}
website2 {
set path user2
@website2.com
set password password2
}
default {
exit
}
}
spawn ssh
$path
expect
"password:"
send
$password
send
"\r"
interact

 

Run it

 

./login.sh website1

 

 

posted @ 2010-09-17 14:58  DavidHHuan  阅读(288)  评论(0编辑  收藏  举报