Python paramik
本节内容
-
paramiko模块
1.paramiko模块
Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作
Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作
1.1 基于用户名密码连接
一下的代码中所有的输出是基于windows来输出的,编码方式为‘gbk’.
#!/usr/bin/env python # -*- coding:utf-8 -*- import paramiko # 创建ssh对象 ssh= paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname='192.168.111.128',port=22,username='root',password='123456') comm = input(":>>") while comm != 'q': comm = input(":>>") # 执行命令 stdin, stdout, stderr = ssh.exec_command(comm) err = stderr.read() if err: result = err else: result = stdout.read() print(result.decode('gbk')) ssh.close()
#!/usr/bin/env python # -*- coding:utf-8 -*- import paramiko transport = paramiko.Transport(('192.168.111.128',22)) transport.connect(username='root',password='123456') ssh = paramiko.SSHClient() ssh._transport = transport comm = input(":>>") while comm != 'q': stdin,stdout,stderr = ssh.exec_command(comm) err = stderr.read() if err: re_msg = err else: re_msg = stdout.read() print(re_msg.decode('gbk')) comm = input(":>>")
1.2 基于秘钥文件连接
#!/usr/bin/env python # -*- coding:utf-8 -*- import paramiko def para(filepath,hostname,port,username): private_key = paramiko.RSAKey.from_private_key_file(filepath) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=hostname,port=port,username=username,pkey=private_key) comm = input(":>>") stdin,stdout,stderr = ssh.exec_command(comm) err = stderr.read() if err: re_msg = err else: re_msg = stdout.read() return re_msg if __name__ == '__main__': filepath = r'id_rsa_2048' a = para(filepath,'192.168.111.128',22,'root') print(a.decode('gbk'))
#!/usr/bin/env python # -*- coding:utf-8 -*- import paramiko pkeyfile = paramiko.RSAKey.from_private_key_file(r'id_rsa_2048') transport = paramiko.Transport(('192.168.111.128',22)) transport.connect(username='root',pkey=pkeyfile) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh._transport = transport comm = input(":>>") while comm != 'q': stdin,stdout,stderr = ssh.exec_command(comm) err = stderr.read() if err: re_msg = err else: re_msg = stdout.read() print(re_msg.decode('gbk')) comm = input(":>>")
1.3 基于秘钥字符串的连接
#!/usr/bin/env python # -*- coding:utf-8 -*- import paramiko from io import StringIO # 秘钥以省略 key_str = '''XXXXXXXXXXXX''' pkey_str = paramiko.RSAKey(file_obj=StringIO(key_str)) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='192.168.111.128',port=22,username='root',pkey=pkey_str) comm = input(":>>") while comm != 'q': stdin,stdout,stderr = ssh.exec_command(comm) if stderr.read(): print(stderr.read().decode('gbk')) else: print(stdout.read().decode('gbk')) comm = input(":>>")
#!/usr/bin/env python # -*- coding:utf-8 -*- import paramiko from io import StringIO key_str = '''XXXXXXXXXXXX''' pkey_str = paramiko.RSAKey(file_obj=StringIO(key_str)) transport = paramiko.Transport(('192.168.111.128',22)) transport.connect(username='root',pkey=pkey_str) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh._transport = transport comm = input(":>>") while comm != 'q': stdin,stdout,stderr = ssh.exec_command(comm) if stderr.read(): print(stderr.read().decode('gbk')) else: print(stdout.read().decode('gbk')) comm = input(":>>")
1.4 文件上传和下载
#!/usr/bin/env python # -*- coding:utf-8 -*- # 上传 import paramiko transport = paramiko.Transport(('192.168.111.128',22)) transport.connect(username='root',password='123456') helei = paramiko.SFTPClient.from_transport(transport) localpath = r'C:\Users\user\Desktop\123.txt' remotepath = r'/tmp/helei.hahaha' helei.put(localpath,remotepath) transport.close() # 下载 import paramiko transport = paramiko.Transport(('192.168.111.128',22)) transport.connect(username='root',password='123456') sftp = paramiko.SFTPClient.from_transport(transport) localpath = r'C:\Users\user\Desktop\123.txt' remotepath = r'/helei.haha' sftp.get(localpath,remotepath) transport.close()
最新内容可以看我的blog: 40kuai