1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import paramiko 4 import uuid 5 class Ha(object): 6 def __init__(self): 7 self.host = '192.168.1.101' 8 self.port = 22 9 self.username = 'root' 10 self.pwd = '123456' 11 12 def crate_file(self): 13 file_name = str(uuid.uuid4()) 14 with open(file_name ,'w') as f: 15 f.write('11111') 16 return file_name 17 def run(self): 18 self.connect() 19 self.Upload() 20 self.rname() 21 self.close() 22 def connect(self): 23 transport = paramiko.Transport((self.host, self.port)) 24 transport.connect(username=self.username, password=self.pwd) 25 self.__transport = transport 26 def close(self): 27 self.__transport.close() 28 def Upload(self): 29 file_name = self.crate_file() 30 sftp = paramiko.SFTPClient.from_transport(self.__transport) 31 sftp.put(file_name, '/home/tttttttttttt.py') 32 def rname(self): 33 ssh = paramiko.SSHClient() 34 ssh._transport = self.__transport 35 stdin,stdout,stderr = ssh.exec_command('mv /home/tttttttttttt.py /home/1111111.py') 36 result = stdout.read() 37 38 ha = Ha() 39 ha.run()