1.编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)

编写脚本实现登陆远程主机。(使用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
~
执行:

posted @ 2022-07-16 15:19  惊起千层浪  阅读(312)  评论(0编辑  收藏  举报