主机A连接主机B
免密登陆 + 自动部署
expect实现自动的交互式任务
——— send
向进程发送字符串(输入)
——— expect
从进程接受字符串
——— spawn
启动新进程
——— interact
允许用户交互
SSH 实现自动登陆,并停留在登录服务器上
shell脚本
#!/usr/bin/expect -f
set ip [lindex $argv 0 ] //接收第一个参数,并设置IP
set password [lindex $argv 1 ] //接收第二个参数,并设置密码
set timeout 10 //设置超时时间
spawn ssh root@$ip //发送ssh请滶
expect { //返回信息匹配
"*yes/no" { send "yes\r"; exp_continue} //第一次ssh连接会提示yes/no,回车,继续
"*password:" { send "$password\r" } //出现密码提示,发送密码
}
interact //交互模式,用户会停留在远程服务器上面.
————创建临时文件
cat >> /etc/profile << EOF\
export ...
EOF
—————————————
创建临时文件,内容是exprot...
>> 重定向追加到目录文件中
<< 将EOF后的内容重定向追加到文件中
原文:https://www.cnblogs.com/xscc/p/10672685.html