系统相关
首页 > 系统相关> > 1.编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)

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
~
执行:

标签:脚本,shell,ip,set,expect,yes,password
来源: https://www.cnblogs.com/biaoming534/p/16484316.html