1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import paramiko
class SSHConnection(object):
def __init__(self, host='127.0.0.1', port=33, username='test', pwd='111'): self.host = host self.port = port self.username = username self.pwd = pwd self.__k = None
def connect(self): transport = paramiko.Transport((self.host, self.port)) transport.connect(username=self.username, password=self.pwd) self.__transport = transport
def close(self): self.__transport.close()
def upload(self, local_path, target_path): sftp = paramiko.SFTPClient.from_transport(self.__transport) sftp.put(local_path, target_path)
def download(self, remote_path, local_path): sftp = paramiko.SFTPClient.from_transport(self.__transport) sftp.get(remote_path, local_path)
def cmd(self, command): ssh = paramiko.SSHClient() ssh._transport = self.__transport stdin, stdout, stderr = ssh.exec_command(command) result = stdout.read() print(str(result, encoding='utf-8')) return result
if __name__ == "__main__": ssh = SSHConnection() ssh.connect() ssh.upload('SSP.py', 'SSP.py') ssh.download('SSP.py', 'SSP.py2', )
|