标签:exe div log missing stdin 上传 环境 stderr pyc
paramiko
一、安装
pip3.5 install paramiko pip3.5 install pycrypto
1 # pycrypto,由于 paramiko 模块内部依赖pycrypto,所以先下载安装pycrypto 2 3 # 下载安装 pycrypto 4 wget http://xxxxxx/pycrypto-2.6.1.tar.gz 5 tar -xvf pycrypto-2.6.1.tar.gz 6 cd pycrypto-2.6.1 7 python setup.py build 8 python setup.py install 9 10 # 进入python环境,导入Crypto检查是否安装成功 11 12 # 下载安装 paramiko 13 wget http://xxxxxx/paramiko-1.10.1.tar.gz 14 tar -xvf paramiko-1.10.1.tar.gz 15 cd paramiko-1.10.1 16 python setup.py build 17 python setup.py install 18 19 # 进入python环境,导入paramiko检查是否安装成功
二、说明
paramiko功能强大可命令、可文件
三、实例:
1、命令版: 密码、KEY
1 import paramiko 2 3 ssh = paramiko.SSHClient() 4 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 5 ssh.connect(‘hostname‘, 22, ‘username‘, ‘password‘) 6 stdin, stdout, stderr = ssh.exec_command(‘df‘) 7 print stdout.read() 8 ssh.close(); 9 10 ???????????????????????????????????? 11 ###基于key 12 #-*- encoding:utf8 -*- 13 14 import paramiko 15 private_key_path="/Users/tf/.ssh/id_rsa" 16 17 key = paramiko.RSAKey.from_private_key_file(private_key_path) 18 ssh = paramiko.SSHClient() 19 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 20 ssh.connect("192.168.1.1", 22, ‘username‘, key) 21 stdin, stdout, stderr = ssh.exec_command("df") 22 result = ssh.exec_command("ifconfig") 23 print(stdout.read()) 24 print(result[1].read()) 25 26 ssh.close()
2、文件get\put
密码:
1 import paramiko 2 3 t = paramiko.Transport(("ipaddr",22)) 4 t.connect(username=‘uuu‘,password=‘123‘) 5 sftp = paramiko.SFTPClient.from_transport(t) 6 sftp.put(‘/tmp/test.py‘,‘/tmp/test.py‘) 7 ##下载 8 sftp.get(‘file‘, ‘file‘) 9 t.close()
生成key: ssh-key-gen
利用Key
1 #-*- encoding:utf8 -*- 2 3 import paramiko 4 5 pravie_key_path = "/Users/xx/.ssh/id_rsa" 6 key = paramiko.RSAKey.from_private_key_file(pravie_key_path) 7 8 t = paramiko.Transport((‘192.168.1.111‘, 22)) 9 t.connect(username=‘xx‘,pkey=key) 10 11 sftp = paramiko.SFTPClient.from_transport(t) 12 sftp.get(‘/tmp/ubuntu.txt‘,‘/tmp/test4.py‘) # 下载 13 sftp.put("/tmp/learn_corde_05.rtf", "/tmp/learn_corde.rtf") ##上传 14 t.close()
标签:exe div log missing stdin 上传 环境 stderr pyc
原文地址:http://www.cnblogs.com/otcsnow/p/6358487.html