shell学习之 使用expect实现远程访问主机
安装expect
yum install expect -y
expect简单使用说明




实现登录远程服务器
#!/bin/expect
set ip 172.16.*.* #主机地址
set user root
set passwd **** #密码
set timeout 30
#spawn 开启一个会话,开启一个指令
spawn ssh $user@$ip
expect "password:"
send "$passwd\r"
interact #保持在连接服务器终端界面
登录服务器并执行命令
#!/bin/expect
set ip 172.16.*.* #主机地址
set user root
set passwd **** #密码
set timeout 30
#spawn 开启一个会话,开启一个指令
spawn ssh $user@$ip
expect "password:"
send "$passwd\r"
expect "*#" #开始在连接服务器执行命令
send "cd /home/oritest\r"
send "touch ./1.txt\r"
send "exit\r"
expect eof #表示交互结束,退回到原用户

浙公网安备 33010602011771号