python ssh链接工具类

网友投稿 247 2022-08-26


python ssh链接工具类

代码结构

# coding:utf-8import paramiko'''文件上传与下载'''class SSHConnection(object): def __init__(self, host='ip', port=22, username='root', pwd=''): 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): # 连接,上传 # file_name = self.create_file() sftp = paramiko.SFTPClient.from_transport(self.__transport) # 将location.py 上传至服务器 /tmp/test.py 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 resultif __name__ == '__main__': ssh = SSHConnection() ssh.connect() # ssh.cmd("ls") # SHOW INDEX FROM ums_member; # ALTER TABLE ums_member DROP INDEX phone; ssh.upload('/Users','/home/script') # ssh.upload('//saveLogin.py','/home/saveLogin.py') # path = '/root/data/categoryInfo.txt' # ssh.download(path, '/') ssh.close() print("完成!")


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python tensorflow框架(python是什么意思)
下一篇:浅析SpringBoot自动装配的实现
相关文章

 发表评论

暂时没有评论,来抢沙发吧~