最近用天翼云服务器上了一个项目,给的弹性公网ip不够,导致多台服务器只能用内网ssh链接操作,发布维护比较麻烦。
这里记录几个批量脚本操作方法
部署发布什么的,就可以通过之前写的python脚本操作
python-script: 记录一些常用的python运维用的脚本
这里记录一下批量远程发送linux指令执行的方法
需要用到自动交互脚本工具
首先执行
yum install -y tcl
yum install -y expect
安装expect工具
然后可以通过一下脚本远程发送指令
比方说我想删除其他四台服务器上一些由于调整部署结构导致的无用文件。
expect -c "
spawn ssh -t root@192.168.0.161 rm -fr /www/sdd/*/nohup*.log
expect {
\"*assword\" {set timeout 300; send \"Safe@123\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
spawn ssh -t root@192.168.0.116 rm -fr /www/sdd/*/nohup*.log
expect {
\"*assword\" {set timeout 300; send \"Safe@123\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
spawn ssh -t root@192.168.0.95 rm -fr /www/sdd/*/nohup*.log
expect {
\"*assword\" {set timeout 300; send \"Safe@123\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
spawn ssh -t root@192.168.0.130 rm -fr /www/sdd/*/nohup*.log
expect {
\"*assword\" {set timeout 300; send \"Safe@123\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
spawn 调用ssh链接服务器 发送rm 命令删除文件,这里执行任何linux命令都可以
expect是发送交互信息,主要是链接时需要输入ssh用户密码
eof用来结束指令
剩下的就是复制粘贴修改了