#第一次
[root@node2 ~]# ssh root@192.168.0.129
The authenticity of host ‘192.168.0.129 (192.168.0.129)’ can’t be established.
ECDSA key fingerprint is SHA256:843LW+DwjjBNWQjNlv4MNcJ4+jkEA6n/PnuOqrbS5Fo.
ECDSA key fingerprint is MD5:ac:1b:e2:f1:7a:a8:e9:6f:37:ec:0c:46:e4:d4:4b:90.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.0.129’ (ECDSA) to the list of known hosts.
root@192.168.0.129’s password:
Last login: Sat Oct 16 06:02:43 2021 from 192.168.0.109
[root@node2 ~]# exit
logout
Connection to 192.168.0.129 closed.
#第二次进入后,就不用输入yes
[root@node2 ~]# ssh root@192.168.0.129
root@192.168.0.129’s password:
Last login: Sat Oct 16 06:05:00 2021 from node2
[root@node5 ~]# vim sshlogin #登录进对应的主机,然后不退出
set timeout 5
spawn ssh root@192.168.0.129 -p22
expect {
“*(yes/no)?” {send “yes\r”;exp_continue}
“password:" {send “1\r”}
}
expect "#”
send “df -h \r”
interact #保持住交互界面,不退出
[root@node5 ~]# expect -f sshlogin
spawn ssh root@192.168.0.129 -p22
root@192.168.0.129’s password:
Last login: Sat Oct 16 10:01:26 2021 from 192.168.0.109
[root@node2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.5G 16G 9% /
devtmpfs 899M 0 899M 0% /dev
tmpfs 911M 0 911M 0% /dev/shm
tmpfs 911M 9.6M 902M 2% /run
tmpfs 911M 0 911M 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
tmpfs 183M 0 183M 0% /run/user/0
[root@node2 ~]# ls
anaconda-ks.cfg
[root@node2 ~]# exit
logout
Connection to 192.168.0.129 closed.
[root@node5 ~]# cat sshlogin
set timeout 5
spawn ssh root@192.168.0.129 -p22
expect {
“*(yes/no)?” {send “yes\r”;exp_continue}
“password:" {send “1\r”}
}
expect "#”
send “df -h \r”
expect eof #完成后,自动退出登录
[root@node5 ~]# expect -f sshlogin
spawn ssh root@192.168.0.129 -p22
root@192.168.0.129’s password:
Last login: Sat Oct 16 10:46:15 2021 from 192.168.0.109
[root@node2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.5G 16G 9% /
devtmpfs 899M 0 899M 0% /dev
tmpfs 911M 0 911M 0% /dev/shm
tmpfs 911M 9.6M 902M 2% /run
tmpfs 911M 0 911M 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
tmpfs 183M 0 183M 0% /run/user/0
[root@node2 ~]# [root@node5 ~]#
[root@node5 ~]#