利用expect实现自动交互ssh/scp

自动ssh

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/expect
set host 192.168.10.210
set user protest2
set password 1qazXDR%
set port 22

spawn ssh $user@$host -p $port
expect {
"(yes/no)?" { send "yes\r";exp_continue }
"*assword:*" { send "$password\r" }
}
interact

自动scp

1
2
3
4
5
6
7
8
9
#!/usr/bin/expect
set password 1qazXDR%
spawn scp /tmp/1.txt protest2@192.168.10.210:/tmp/
expect {
"(yes/no)?" { send "yes\n";exp_continue}
"*assword:" { send "$password\n" }
}
expect "100%"
expect eof