53.expect实现ssh自动登录
[root@rocky8 ~]
spawn scp /etc/fstab 172.31.0.7:/data
expect {
"yes/no" {
send "yes\n";exp_continue }
"password" {
send "123456\n" }
}
expect eof
[root@rocky8 ~]
[root@rocky8 ~]
spawn scp /etc/fstab 172.31.0.7:/data
The authenticity of host '172.31.0.7 (172.31.0.7)' can't be established.
ECDSA key fingerprint is SHA256:zb/8mo/ptS0h8eHVY1FDRuvh6aQj1opzpsD7khnYjSo.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.31.0.7' (ECDSA) to the list of known hosts.
[email protected]'s password:
fstab 100% 808 461.5KB/s 00:00
[root@centos7 ~]
total 4
-rw-r--r-- 1 root root 808 Oct 22 15:44 fstab
54.expect变量实现ssh自动登录
[root@rocky8 ~]
set ip 172.31.0.7
set user root
set password 123456
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" {
send "yes\n";exp_continue }
"password" {
send "$password\n" }
}
interact
[root@rocky8 ~]
[root@rocky8 ~]
spawn ssh [email protected]
[email protected]'s password:
Last login: Fri Oct 22 14:01:56 2021 from 172.31.0.1
[root@centos7 ~]
/root
[root@centos7 ~]
logout
Connection to 172.31.0.7 closed.