【python】paramiko和pexpect运维批量脚本

batch_ssh功能介绍:

### 执行批量命令 调用方式
batch_iplist(“iplist.txt”,’who’,”root”,”/root/.ssh/id_rsa”,60,”command.txt”,True)
### 执行单个命令 调用方式
batch_iplist(“iplist.txt”,’who’,”root”,”/root/.ssh/id_rsa”,60,”command.txt”,False)
### iptable 功能模块
1、通过读取iptable_iplist_file.txt文件中ip列表,批量封禁IP
python2.6 batch_ssh.py -I –iptable_config_ini=iptable_config.ini –sub_domain=www.phpdba.com –iptable_iplist_file=iptable_iplist_file.txt
2、直接通过-s将要封禁IP,传入脚本
python2.6 batch_ssh.py -I –iptable_config_ini=iptable_config.ini –sub_domain=www.phpdba.com -s 123.123.123.123
3、同时封禁iptable_iplist_file.txt和-s指定IP
python2.6 batch_ssh.py -I –iptable_config_ini=iptable_config.ini –sub_domain=www.phpdba.com -s 123.123.123.123 –iptable_iplist_file=iptable_iplist_file.txt

### 批量执行命令模块
1、以dev 权限登录iplist.txt中ip,同时批量执行command.txt中定义系列命令及command定义命令
python2.6 batch_ssh.py -B –batch_iplist_file=iplist.txt –batch_commandlist_file=command.txt –command=”ps aux|grep python” –batch_user=dev –batch_auth_key=dev_id_rsa
2、以dev 权限登录iplist.txt中ip,执行command.txt中系列命令
python2.6 batch_ssh.py -B –batch_iplist_file=iplist.txt –batch_commandlist_file=command.txt –batch_user=dev –batch_auth_key=dev_id_rsa
3、以dev权限登录iplist.txt中ip,只执行command定义命令
python2.6 batch_ssh.py -B –batch_iplist_file=iplist.txt –command=”ps aux|grep python” –batch_user=dev –batch_auth_key=dev_id_rsa
4、以root权限登录iplist.txt中ip,批量执行command.txt文件中定义的系列命令
python2.6 batch_ssh.py -B –batch_iplist_file=iplist.txt –batch_commandlist_file=command.txt
5、以root权限登录iplist.txt中ip,执行command定义的命令
python2.6 batch_ssh.py -B –batch_iplist_file=iplist.txt –command=”ps aux|grep python”
6、以root权限登录iplist.txt中ip,同时批量执行command.txt中定义系列命令及command定义命令
python2.6 batch_ssh.py -B –batch_iplist_file=iplist.txt –batch_commandlist_file=command.txt –command=”ps aux|grep python”
### 批量获取mysql信息
1、以root权限登录iplist.txt中ip,批量执行command.txt文件中定义的系列命令
python2.6 batch_ssh.py -M –batch_iplist_file=iplist.txt –batch_commandlist_file=command.txt

源码:https://github.com/chen-123/phper/blob/master/运维工具/python/batch_ssh.py

此条目发表在技术生涯分类目录,贴了标签。将固定链接加入收藏夹。