paramiko模块使用
作者:互联网
该模块基于ssh用于连接远程服务器并执行相关操作。 SSHClient 用于连接远程服务器并执行基本命令 pip3 install paramiko
# 基于用户名和密码 import paramiko # 创建ssh对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname='10.0.0.12', port=22, username='root', password='mysql') # 执行命令 stdin, stdout, stderr = ssh.exec_command('df -Th') # 获取命令结果 result = stdout.read().decode('utf8') # 关闭连接 ssh.close() stdout.close() print(result)
# SSHClient封装Transport import paramiko transport = paramiko.Transport(('10.0.0.12', 22)) transport.connect(username='root', password='mysql') ssh = paramiko.SSHClient() ssh._transport = transport stdin, stdout, stderr = ssh.exec_command('df') result = stdout.read().decode() transport.close() print(result)
# 基于公钥和私钥,请提前执行ssh-copy-id # ssh-copy-id -i 公钥 root@10.0.0.12 # ssh-copy-id -i id_rsa.pub root@10.0.0.12 # ssh root@10.0.0.12 import paramiko # 私钥的地址 private_key = paramiko.RSAKey.from_private_key_file('/Users/zhouwanchun/.ssh/id_rsa') # 创建ssh对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname='10.0.0.12', port=22, username='root', pkey=private_key) # 执行命令 stdin, stdout, stderr = ssh.exec_command('df -Th') # 获取命令结果 result = stdout.read().decode('utf8') # 关闭连接 ssh.close() stdout.close() print(result)
# SSHClient封装Transport import paramiko private_key = paramiko.RSAKey.from_private_key_file('/Users/zhouwanchun/.ssh/id_rsa') transport = paramiko.Transport(('10.0.0.12', 22)) transport.connect(username='root', pkey=private_key) ssh = paramiko.SSHClient() ssh._transport = transport stdin, stdout, stderr = ssh.exec_command('df -Th') result = stdout.read().decode() transport.close() print(result)
# 基于私钥字符串进行连接 import paramiko from io import StringIO key_str = """-----BEGIN OPENSSH PRIVATE KEY----- b2BlbnNzaC1rZXktdjEAA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f2zhp8b5 D1NrrSm6WLMAgcLm+LDqr3+uBwZrH+VQAAAMEA6sTafjrZ28nlgcWz/twOl2R1xmQncIKa AYNoVdlfdDoK9wsOYhZvWaoth1UC2aK8zNzp1VfPAAwHfeqVv2zRR3WEUFU4gXo7D9NnNx CgJC0IYadxjF+KUW+3OZeinbbJkuFTS8DemyHp9JvVNBepCnFRvfD+GH3d06f+FxU00cfA cJOtk7+5+MEmKA8gXk+U0zezLkgu53PUEFs7+UeNWuA80vmkHFn+b+wmcsKNRwWTYE/4Kc D5BK0x/MySktPfAAAAH3pob3V3YW5jaHVuQGxvdmVteXNxbDMzMDYubG9jYWwBAgM= -----END OPENSSH PRIVATE KEY-----""" private_key = paramiko.RSAKey(file_obj=StringIO(key_str)) transport = paramiko.Transport(('10.0.0.12', 22)) transport.connect(username='root', pkey=private_key) ssh = paramiko.SSHClient() ssh._transport = transport stdin, stdout, stderr = ssh.exec_command('df') result = stdout.read().decode() transport.close() print(result)
# SFTPClient 用于连接远程服务器并执行上传下载文件 # 基于用户名和密码 import paramiko transport = paramiko.Transport(('10.0.0.12', 22)) transport.connect(username='root', password='mysql') sftp = paramiko.SFTPClient.from_transport(transport) # 将local_path上传到remove_path sftp.put('/tmp/1.py', '/tmp/test.py') # 将remove_path下载到local_path sftp.get('/tmp/test.py', '/tmp/2.py') transport.close() # 基于公钥和私钥 import paramiko private_key = paramiko.RSAKey.from_private_key_file('/Users/zhouwanchun/.ssh/id_rsa') transport = paramiko.Transport(('10.0.0.12', 22)) transport.connect(username='root', pkey=private_key) sftp = paramiko.SFTPClient.from_transport(transport) # 将local_path上传到remove_path sftp.put('/tmp/1.py', '/tmp/test.py') # 将remove_path下载到local_path sftp.get('/tmp/test.py', '/tmp/2.py') transport.close()
标签:模块,paramiko,stdout,private,ssh,key,使用,transport 来源: https://www.cnblogs.com/zhouwanchun/p/16650811.html