编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)
1.expect 形式:
[root@CentOS8 ~]# cat remote_expect.sh
!/usr/bin/expect
********************************************************************
Author: buyaoming
QQ: 123456789
Date: 2022-07-14
FileName: remote_expect.sh
URL: www.haha.cn
Description: The test script
Copyright (C): 2022 All rights reserved
********************************************************************
set ip 10.0.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
2.shell脚本形式
[root@CentOS8 ~]# cat remote_shell.sh
!/bin/bash
********************************************************************
Author: buyaoming
QQ: 123456789
Date: 2022-07-14
FileName: remote_shell.sh
URL: www.haha.cn
Description: The test script
Copyright (C): 2022 All rights reserved
********************************************************************
ip=10.0.0.6
user=root
password=123456
expect <<EOF
set timeout 10
spawn ssh $user@$ip
expect {
“yes/no” { send “yes/n”;exp_continue }
“password” { send “$password/n” }
}
expect eof
EOF
~
执行:
原创文章,作者:wdmbts,如若转载,请注明出处:https://blog.ytso.com/274804.html