S27.shell脚本每日一练

53.expect实现ssh自动登录

[root@rocky8 ~]# vim expect1
#!/usr/bin/expect
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 ~]# chmod +x expect1 
[root@rocky8 ~]# ./expect1 
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 ~]# ll /data
total 4
-rw-r--r-- 1 root root 808 Oct 22 15:44 fstab

54.expect变量实现ssh自动登录

[root@rocky8 ~]# vim expect2
#!/usr/bin/expect
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 ~]# chmod +x expect2
[root@rocky8 ~]# ./expect2
spawn ssh [email protected]
[email protected]'s password: 
Last login: Fri Oct 22 14:01:56 2021 from 172.31.0.1

[root@centos7 ~]# pwd
/root
[root@centos7 ~]# exit
logout
Connection to 172.31.0.7 closed.

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/126772761