[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
原文地址:https://www.cnblogs.com/davidhhuan/p/1829329.html