标签:return ret private test pass shc from known param
1.基于密码的ssh
1 import paramiko 2 3 #创建SSH对象 4 ssh = paramiko.SSHClient() 5 6 #把要连接的机器添加到known_hosts文件中 7 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 8 9 #连接服务器 10 ssh.connect(hostname=‘192.168.1.96‘, port=22, username=‘morra‘, password=‘123456‘) 11 12 cmd = ‘ps‘ 13 #cmd = ‘ls -l;ifconfig‘ #多个命令用;隔开 14 stdin, stdout, stderr = ssh.exec_command(cmd) 15 16 result = stdout.read() 17 18 if not result: 19 result = stderr.read() 20 ssh.close() 21 22 print(result.decode())
2.基于密码的Sftp传输
1 import paramiko 2 3 transport = paramiko.Transport((‘192.168.1.96‘, 22)) 4 5 transport.connect(username=‘morra‘, password=‘357447218‘) 6 7 sftp = paramiko.SFTPClient.from_transport(transport) 8 9 sftp.put(‘123.py‘, ‘/tmp/test.py‘) # 将123.py 上传至服务器 /tmp下并改名为test.py 10 11 sftp.get(‘remove_path‘, ‘local_path‘) # 将remove_path 下载到本地 local_path
3.使用密钥对ssh
1 import paramiko 2 3 private_key = paramiko.RSAKey.from_private_key_file(‘id_rsa96‘) #使用目标的私钥来登录 4 5 ssh = paramiko.SSHClient() 6 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 7 8 ssh.connect(hostname=‘192.168.1.96‘,port=22,username=‘morra‘,pkey=private_key) 9 10 cmd = ‘ps‘ 11 stdin, stdout, stderr = ssh.exec_command(cmd) 12 13 result = stdout.read() 14 15 if not result: 16 result = stderr.read() 17 ssh.close() 18 19 print(result.decode())
4.使用密钥对Sftp
1 import paramiko 2 3 private_key = paramiko.RSAKey.from_private_key_file(‘id_rsa96‘) #使用目标的私钥来登录 4 5 ssh = paramiko.SSHClient() 6 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 7 8 ssh.connect(hostname=‘192.168.1.96‘,port=22,username=‘morra‘,pkey=private_key) 9 10 cmd = ‘ps‘ 11 stdin, stdout, stderr = ssh.exec_command(cmd) 12 13 result = stdout.read() 14 15 if not result: 16 result = stderr.read() 17 ssh.close() 18 19 print(result.decode())
5小总结
1 import paramiko 2 import uuid 3 4 class Haproxy(object): 5 6 def __init__(self): 7 self.host = ‘192.168.1.96‘ 8 self.port = 22 9 self.username = ‘morra‘ 10 self.pwd = ‘123456‘ 11 self.__k = None 12 13 def create_file(self): 14 file_name = str(uuid.uuid4()) 15 with open(file_name,‘w‘) as f: 16 f.write(‘sb‘) 17 return file_name 18 19 def run(self): 20 self.connect() 21 self.upload() 22 self.rename() 23 self.close() 24 25 def connect(self): 26 transport = paramiko.Transport((self.host,self.port)) 27 transport.connect(username=self.username,password=self.pwd) 28 self.__transport = transport 29 30 def close(self): 31 32 self.__transport.close() 33 34 def upload(self): 35 # 连接,上传 36 file_name = self.create_file() 37 38 sftp = paramiko.SFTPClient.from_transport(self.__transport) 39 sftp.put(file_name, ‘/home/test.py‘) # 将location.py 上传至服务器 /tmp/test.pydef rename(self): 40 41 ssh = paramiko.SSHClient() 42 ssh._transport = self.__transport # 执行命令 43 stdin, stdout, stderr = ssh.exec_command(‘mv /home/test.py /home/test2.py‘) 44 result = stdout.read() 45 46 47 ha = Haproxy() 48 ha.run()
标签:return ret private test pass shc from known param
原文地址:https://www.cnblogs.com/LyShark/p/9066628.html