import threadingimport paramikoimport subprocessdef ssh_command(ip,user,passwd,command): client=paramiko.SSHClient() #client.load_host_keys("filename")使用密钥验证 #设置自动添加和保存ssh服务器的ssh密钥 client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(ip,username=user,password=passwd) ssh_session=client.get_transport().open_session() if ssh_session.active: #调用ssh_comand函数运行command ssh_session.exec_command(command) a=ssh_session.recv(1024) return aa=ssh_command("192.168.6.128",'root','123456','ls -lh')print(a.decode("gbk"))
基于用户名密码
import paramiko # 创建SSH对象ssh = paramiko.SSHClient()# 允许连接不在know_hosts文件中的主机ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 连接服务器ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', password='123') # 执行命令stdin, stdout, stderr = ssh.exec_command('df')# 获取命令结果result = stdout.read() # 关闭连接ssh.close()
封装transport
import paramikotransport = paramiko.Transport(('hostname', 22))transport.connect(username='wupeiqi', password='123')ssh = paramiko.SSHClient()ssh._transport = transportstdin, stdout, stderr = ssh.exec_command('df')print stdout.read()transport.close()
基于公钥密钥
import paramiko private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa') # 创建SSH对象ssh = paramiko.SSHClient()# 允许连接不在know_hosts文件中的主机ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 连接服务器ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', key=private_key) # 执行命令stdin, stdout, stderr = ssh.exec_command('df')# 获取命令结果result = stdout.read() # 关闭连接ssh.close()
封装
import paramikoprivate_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')transport = paramiko.Transport(('hostname', 22))transport.connect(username='wupeiqi', pkey=private_key)ssh = paramiko.SSHClient()ssh._transport = transportstdin, stdout, stderr = ssh.exec_command('df')transport.close()
基于用户名密码上传下载
import paramiko transport = paramiko.Transport(('hostname',22))transport.connect(username='wupeiqi',password='123') sftp = paramiko.SFTPClient.from_transport(transport)# 将location.py 上传至服务器 /tmp/test.pysftp.put('/tmp/location.py', '/tmp/test.py')# 将remove_path 下载到本地 local_pathsftp.get('remove_path', 'local_path') transport.close()
基于公钥密钥
import paramiko private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa') transport = paramiko.Transport(('hostname', 22))transport.connect(username='wupeiqi', pkey=private_key ) sftp = paramiko.SFTPClient.from_transport(transport)# 将location.py 上传至服务器 /tmp/test.pysftp.put('/tmp/location.py', '/tmp/test.py')# 将remove_path 下载到本地 local_pathsftp.get('remove_path', 'local_path') transport.close()
paramiko封装
import paramikoclass SshHelper(object): def __init__(self,host,port,username,pwd): self.host = host self.port = port self.username = username self.pwd = pwd self.transport = None def connect(self): transport = paramiko.Transport((self.host, self.port,)) transport.connect(username=self.username, password=self.pwd) self.transport = transport def upload(self,local,target): sftp = paramiko.SFTPClient.from_transport(self.transport) # 将location.py 上传至服务器 /tmp/test.py sftp.put(local, target) # 将remove_path 下载到本地 local_path # sftp.get('remove_path', 'local_path') def cmd(self,shell): ssh = paramiko.SSHClient() ssh._transport = self.transport stdin, stdout, stderr = ssh.exec_command(shell) stdout.read() def close(self): self.transport.close()if __name__ == '__main__': obj = SshHelper('...') obj.connect() obj.close()