python学习之路网络编程篇(第五篇)
paramiko简介
paramiko 是基于Python实现的SSH2远程安装连接,支持认证及秘钥方式。可以实现远程命令执行、文件传输、中间SSH代理等功能。
paramiko安装
#!/bin/bash #install indepence package cd /data/soft wget https://www.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz yum -y install gcc python-devel tar zxf pycrypto-2.6.tar.gz -C /usr/local cd /usr/local/pycrypto-2.6 python setup.py build python setup.py install wget https://pypi.python.org/packages/source/e/ecdsa/ecdsa-0.13.tar.gz#md5=1f60eda9cb5c46722856db41a3ae6670 tar zxf ecdsa-0.13.tar.gz -C /usr/local cd /usr/local/ecdsa-0.13 python setup.py build python setup.py install #install paramiko package cd /data/soft wget https://pypi.python.org/packages/source/p/paramiko/paramiko-1.16.0.tar.gz#md5=7e1203f5ffeb7d2bc2bffc4feb804216 tar zxvf paramiko-1.16.0.tar.gz -C /usr/local cd /usr/local/paramiko-1.16.0 python setup.py build python setup.py install
导入paramiko模块:import paramiko
[root@bogon ~]# python Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import paramiko >>>
paramiko API安装:pip install paramiko
paramiko基于用户名和密码登陆远程主机
SSHClient
用于连接远程服务器并执行基本命令
import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname='172.16.2.100', port=22, username='root', password='123345') # 执行命令 stdin, stdout, stderr = ssh.exec_command('ls') # 获取命令结果 result = stdout.read() # 关闭连接 ssh.close()
具体实现脚本如下:
#!/usr/bin/env python import paramiko transport = paramiko.Transport(('172.20.2.234',22)) transport.connect(username='root',password='000000') ssh = paramiko.SSHClient() ssh._transport = transport stdin,stdout,stderr = ssh.exec_command('uname -a') print(stdout.read()) transport.close()
paramiko基于秘钥登陆远程主机
import paramiko private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa') # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname='172.16.2.234', port=22, username='root', key=private_key) # 执行命令 stdin, stdout, stderr = ssh.exec_command('df') # 获取命令结果 result = stdout.read() # 关闭连接 ssh.close()
具体实现脚本如下:
[root@bogon paramiko]# cat para_key.py #!/usr/bin/env python import paramiko private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa') transport = paramiko.Transport(('172.20.2.234',22)) transport.connect(username='root',pkey=private_key) ssh = paramiko.SSHClient() ssh._transport = transport stdin,stdout,stderr = ssh.exec_command('df -hT') print(stdout.read()) transport.close()
paramiko实现远程文件的上传下载
SFTPClient
用于连接远程服务器并执行上传下载
基于用户名密码上传下载:
import paramiko transport = paramiko.Transport(('172.16.2.234',22)) transport.connect(username='root',password='123345') sftp = paramiko.SFTPClient.from_transport(transport) # 将location.py 上传至服务器 /tmp/test.py sftp.put('/data/local/location.py', '/data/remote/test.py') # 将remove_path 下载到本地 local_path sftp.get('remove_path', 'local_path') transport.close()
具体实现代码如下:
#!/usr/bin/env python import paramiko username = 'root' password = '000000' hostname = '172.20.2.234' port = 22 try: t = paramiko.Transport((hostname,port)) t.connect(username=username,password=password) sftp = paramiko.SFTPClient.from_transport(t) sftp.put('/data/local/localfile','/data/remote/remotefile') sftp.get('/data/remote/remotefile1','/data/local/localfile1') sftp.mkdir('/data/aaa',0755) sftp.rmdir('/data/aaa') sftp.rename('/data/remote/test.sh','/data/remote/testfile.sh') print(sftp.stat('/data/remote/testfile.sh')) print(sftp.listdir('/data/remote')) except Exception as e: print(e)