#batch_Ssh
利用此工具在多台机器上执行命令, shell 模式下进行多台主机终端切换,更方便的切换多台主机
##实现:
- paramiko: 执行命令 上传文件
- pexpect: 终端切换
- cmd: pyshell 实现
- getpass: 密码输入
- optparse: 长短参数识别
##作者信息(Author Information)
e-mail: jos666@qq.com
qq: 97074667
职业(Occupation): 系统运维 (SA)
git clone https://github.com/jos666/batch_ssh.git
cd batch_ssh
# ./batch_ssh.py -H "192.168.2.21 192.168.2.25" -u root -c id # 多个主机可以使用配置文件方式 -f 主机文件, 不想暴露密码可以不写 -p参数
Password: #输入密码不显示
[info] login in progress ....
Task execution time:0.221935033798 s
[info] exec_cmd in progress ....
192.168.2.25:
uid=0(root) gid=0(root) groups=0(root)
192.168.2.21:
uid=0(root) gid=0(root) groups=0(root)
Task execution time:0.0510380268097 s
# ./batch_ssh.py -H "192.168.2.21 192.168.2.25" -u root -o put -l /tmp/testfile -r /tmp/testfile
Password:
[info] login in progress ....
Task execution time:0.199142217636 s
[info] sftp in progress ....
192.168.2.25:
[Info] PUT /tmp/testfile file successfully,RemotePath:/tmp/testfile
192.168.2.21:
[Info] PUT /tmp/testfile file successfully,RemotePath:/tmp/testfile
Task execution time:0.0418720245361 s
支持多主机 会话保持 终端切换 文件上传 文件下载 自动补全
# ./batch_ssh.py -m shell
default:
Host:[]
User:root
Passwd:123456
change host command add_host host
change user command input user user
chage passwd command input passwd
view infomaintion use command "show"
e.g:
#add_host 192.168.1.1 #or add_host 192.168.1.1 192.168.1.2
#input user
Input for username:root
#input passwd
password:
#connect
[info] task in progress ....
Task execution time:0.28550195694
#use * #use all for host
#cmd id #send id command for all host
Control #
Control #add_host 192.168.2.21 192.168.2.57 #多个以空格分开 或者使用配置文件 -f config 加载多主机
#支持多个间隔 add_host 192.168.1.1-50 #1到50的主机增加
Control #input user
Input for username:root
Control #input passwd
Password: #输入不可见
Control #connect
[info] login in progress ....
Task execution time:0.271550893784 s
Control #use * #选择主机 * 为所有主机
ALL@Control#cmd id
[info] exec_cmd in progress ....
192.168.2.21:
uid=0(root) gid=0(root) groups=0(root)
192.168.2.57:
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Task execution time:0.0580770969391 s
ALL@Control#
Control#use *
ALL@Control#scp put /tmp/testfile /tmp/testfile
[info] sftp in progress ....
192.168.2.57:
[Info] PUT /tmp/testfile file successfully,RemotePath:/tmp/testfile
192.168.2.21:
[Info] PUT /tmp/testfile file successfully,RemotePath:/tmp/testfile
Task execution time:0.0517821311951 s
ALL@Control#use 192.168.2.21 # use 命令支持tab 补全
192.168.2.21@Control#cmd id
[info] exec_cmd in progress ....
192.168.2.21:
uid=0(root) gid=0(root) groups=0(root)
Task execution time:0.0113530158997 s
Control#use 192.168.2.21
192.168.2.21@Control#scp put /tmp/testfile /tmp/testfile
[info] sftp in progress ....
192.168.2.21:
[Info] PUT /tmp/testfile file successfully,RemotePath:/tmp/testfile
Task execution time:0.0151340961456 s
ALL@Control#terminal 192.168.2.21
Wed Feb 24 18:40:52 2016 from 192.168.2.25
[root@cms ~]#
[root@cms ~]# exit #退出终端 返回pyshell 界面