源代码:
https://github.com/jy1779/be.git
环境需求:
1、python3
2、paramiko
pip install --upgrade pip
apt-get install libssl-dev
pip3 install paramiko
3、执行权限
chmod +x becmd.py
ln -s /root/be/bin/becmd.py /usr/local/sbin/becmd
chmod +x besync.py
ln -s /root/be/bin/becmd.py /usr/local/sbin/besync
4、导入路径设置
cd /usr/lib/python3.5/dist-packages/
touch be.pth
vim be.pth
/root/be #be.pth文件内容
5、因为是从windows开发所以会出现以下问题:
windows 上传的文件,可以用这个指令格式化成Unix文件。
apt install dos2unix
root@db3:~/be/bin# ./besync.py
/usr/bin/env: ‘python3\r’: No such file or directory
root@db3:~/be/bin# dos2unix besync.py
dos2unix: converting file besync.py to Unix format ...
root@db3:~/be/bin# ./besync.py
Reminder: The source and destination addresses do not exist
Usage: ./besync.py
解决
apt install dos2unix
dos2unix becmd.py
dos2unix besync.py
6、日志路径的问题。最好是绝对路径,不然只能在be目录下执行becmd 和besync
f = open("/root/be/logs/besync.log",'a')
f = open("/root/be/logs/becmd.log",'a')
程序目录:
.
├── app
│ ├── __init__.py
│ ├── pwd_connect_cmd.py
│ ├── pwd_connect_sync.py
│ ├── __pycache__
│ │ ├── __init__.cpython-35.pyc
│ │ ├── pwd_connect_cmd.cpython-35.pyc
│ │ ├── pwd_connect_sync.cpython-35.pyc
│ │ ├── ssh_be_cmd.cpython-35.pyc
│ │ └── ssh_be_sync.cpython-35.pyc
│ ├── ssh_be_cmd.py
│ └── ssh_be_sync.py
├── bin
│ ├── becmd.py
│ ├── besync.py
│ └── __init__.py
├── conf
│ ├── config.py
│ ├── __init__.py
│ └── __pycache__
│ ├── config.cpython-35.pyc
│ └── __init__.cpython-35.pyc
├── __init__.py
└── logs
├── becmd.log
├── besync.log
└── __init__.py
app/ssh_be_cmd.py
import paramiko import threading import datetime class MyThread(threading.Thread): def __init__(self,ip,port,username,password,cmd): self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) super(MyThread,self).__init__() self.ip = ip self.port = port self.username = username self.password = password self.cmd = cmd def run(self): port = int(self.port) self.ssh.connect(hostname=self.ip, port=port, username=self.username, password=self.password) stdin, stdout, stderr = self.ssh.exec_command(self.cmd) res, err = stdout.read(), stderr.read() result = res if res else err f = open("/root/be/logs/becmd.log",'a') f.write(str(datetime.datetime.now())+" ") f.write(self.ip+" ") f.write(self.username+ " ") f.write(self.cmd+ "\n") f.close() print("\033[1;32;40m" + self.ip.rjust(33,'=')+ "\033[0m","\033[1;32;40m" + "Command result: ".ljust(37,'=')+ "\033[0m") print(result.decode()) self.ssh.close()
app/pwd_connect_cmd.py
import sys from app.ssh_be_cmd import MyThread from conf.config import account def pwd_con(host): ip = account[host]["ip"] port = account[host]["port"] username = account[host]["username"] password = account[host]["password"] a=sys.argv[1:100] cmd = " ".join(a) if len(a) >=1: M = MyThread(ip,port,username,password,cmd) M.start() else: print("Reminder: The command does not exist") exit() def connect(): for host in account.keys(): pwd_con(host)
app/ssh_be_sync.py
import paramiko import threading import datetime,time import os from os.path import getsize class MyThread(threading.Thread): def __init__(self,ip,port,username,password,cmd): super(MyThread,self).__init__() self.ip = ip self.port = port self.username = username self.password = password self.cmd = cmd def run(self): port = int(self.port) self.transport = paramiko.Transport((self.ip, port)) self.transport.connect(username=self.username, password=self.password) self.sftp = paramiko.SFTPClient.from_transport(self.transport) help=""" -f send file to remote host. %s -f
app/pwd_connect_sync.py
import sys from app.ssh_be_sync import MyThread from conf.config import account def pwd_con(host): ip = account[host]["ip"] port = account[host]["port"] username = account[host]["username"] password = account[host]["password"] cmd=sys.argv[0:100] M = MyThread(ip, port, username, password, cmd) M.start() def connect(): for host in account.keys(): pwd_con(host)
conf/config.py
account = { "192.168.1.57":{ "ip":"192.168.1.57", "port":"22", "username":"root", "password":"123456" }, "192.168.1.75":{ "ip": "192.168.1.75", "port": "22", "username": "root", "password": "123456" } }
bin/becmd.py
#!/usr/bin/env python3 from app.pwd_connect_cmd import connect connect()
bin/besync.py
#!/usr/bin/env python3 from app.pwd_connect_sync import connect connect()
使用例子:
1、批量执行指令:
2、批量上传文件
3、批量上传目录