paramiko安装
Paramiko is a Python (2.7, 3.4+) implementation of the SSHv2 protocol.
error: command ‘x86_64-linux-gnu-gcc’ failed with exit status 1
解决办法:sudo apt-get install build-essential libssl-dev libffi-dev python-dev
再不行就更新, 就不信安不上, pip install --upgrade setuptools
批量执行:
1 import paramiko 2 3 ssh = paramiko.SSHClient() 4 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 5 ssh.connect('192.168.189.900', 22, 'yangxl', '123456') 6 stdin, stdout, stderr = ssh.exec_command('ifconfig') 7 print(stdout.read().decode('utf-8')) 8 ssh.close()
上传、下载文件:
1 import paramiko 2 3 ssh = paramiko.Transport(('192.168.189.128', 22)) 4 ssh.connect(username='yangxl', password='123456') 5 sftp = paramiko.SFTPClient.from_transport(ssh) 6 # sftp.put('/home/yangxl/test.py', '/tmp/test2.py') # 上传 7 sftp.put('/tmp/tmpaddon', '/home/yangxl/test3.py') # 下载 8 ssh.close()
pip安装没有demo哦,
执行 `python demo.py` 就能登陆机器,
1 while True: 2 r, w, e = select.select([chan, sys.stdin], [], []) 3 if chan in r: 4 try: 5 x = u(chan.recv(1024)) 6 if len(x) == 0: 7 sys.stdout.write("\r\n*** EOF\r\n") 8 break 9 sys.stdout.write(x) # 输出到标准输出 10 sys.stdout.flush() # 刷新到屏幕 11 except socket.timeout: 12 pass 13 if sys.stdin in r: 14 x = sys.stdin.read(1) # 读入到标准输入, (稍后发送) 15 if len(x) == 0: 16 break 17 chan.send(x)