前提需要有python和pip环境,一下只介绍python与ssh命令执行
下载paramiko库
命令框安装
paramiko使用代码
在一个无限循环中,依次执行commands列表中的命令。使用exec_command()方法执行命令,并获取标准输出和标准错误输出。然后打印标准错误输出和标准输出的内容。接下来,使用range()函数实现倒计时功能,打印剩余秒数并进行延迟。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import paramiko import time
hostname = '192.168.1.51' port = 22 username = 'root' password = 'root'
commands = [ 'us -m \'{"ctrl":{"mac":"DC8E95FFFE0C77FF","endpoint":1,"OnOff":1}}\'', 'us -m \'{"ctrl":{"mac":"DC8E95FFFE0C77FF","endpoint":2,"OnOff":1}}\'' ]
ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname, port, username, password)
time.sleep(2) try: while True: for command in commands: print(command) stdin, stdout, stderr = ssh.exec_command(command) print('stderr', stderr.readlines()) for line in stdout.readlines(): print(line.strip()) for i in range(3, 0, -1): print("\r倒计时{}秒!".format(i), end="", flush=True) time.sleep(1)
except KeyboardInterrupt: pass
ssh.close()
|
这是一个简单的示例。
paramiko结合多线程的使用使用代码示例如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| import threading import paramiko class paramikoThreading(threading.Thread): def __init__(self,command,host,username,password,port=22): self.command = command self.host = host self.username = username self.password = password self.port = port super(paramikoThreading,self).__init__() def run(self): ssh = paramiko.SSHClient() know_host = paramiko.AutoAddPolicy() ssh.set_missing_host_key_policy(know_host) ssh.connect( hostname=self.host, port=self.port, username=self.username, password=self.password, ) stdin, stdout, stderr = ssh.exec_command(self.command) print("*"*60) print("ip:%s,\ncommand:%s,\n"%(self.host,self.command)) print(stdout.read().decode()) print("*"*60) ssh.close() if __name__ == '__main__': from settings import pool command = "ls" t_pool = [] for host in pool: t = paramikoThreading( host=host.get("host","localhost"), username=host.get("username","root"), password=host.get("password","123"), command=command ) t_pool.append(t) for t in t_pool: t.start() for t in t_pool: t.join()
|
上文使用的settings.py
文件如下
1 2 3 4 5 6 7 8 9 10
| pool = [ dict(host="10.10.21.177", username="root", password="123"), dict(host="10.10.21.177", username="root", password="123"), dict(host="10.10.21.177", username="root", password="123"), dict(host="10.10.21.177", username="root", password="123"), dict(host="10.10.21.177", username="root", password="123"), dict(host="10.10.21.177", username="root", password="123"), dict(host="10.10.21.177", username="root", password="123"), dict(host="10.10.21.177", username="root", password="123"), ]
|
文件的上传与下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import paramiko trans = paramiko.Transport( sock=("10.10.21.177",22) ) trans.connect( username="root", password="12345" ) sftp = paramiko.SFTPClient.from_transport(trans)
sftp.put("settings.py","/root/Desktop/settings.py")
sftp.close()
|