ssh操作

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: ssh
Description :
Author : Administrator
date: 2019/6/30 0030
-------------------------------------------------
"""

import paramiko
import uuid,socket


class SSHConnection(object):
def __init__(self, host='172.16.103.191', port=22, username='wupeiqi', pwd='123'):
self.host = host
self.port = port
self.username = username
self.pwd = pwd
self.__k = None

def create_file(self):
file_name = str(uuid.uuid4())
with open(file_name, 'w') as f:
f.write('sb')
return file_name

def run(self):
self.connect()
self.upload('/home/wupeiqi/tttttttttttt.py')
self.rename('/home/wupeiqi/tttttttttttt.py', '/home/wupeiqi/ooooooooo.py')
self.close()

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, target_path):
# 连接,上传
file_name = self.create_file()

sftp = paramiko.SFTPClient.from_transport(self.__transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put(file_name, target_path)

def rename(self, old_path, new_path):
ssh = paramiko.SSHClient()
ssh._transport = self.__transport
# 执行命令
cmd = "mv %s %s" % (old_path, new_path,)
stdin, stdout, stderr = ssh.exec_command(cmd)
# 获取命令结果
result = stdout.read()

def cmd(self, command):
ssh = paramiko.SSHClient()
ssh._transport = self.__transport
# 执行命令
stdin, stdout, stderr = ssh.exec_command(command)
# 获取命令结果
result = stdout.read()
return result

if __name__ == "__main__":
ha = SSHConnection()
ha.run()

转载于:https://www.cnblogs.com/tnyleyon/p/11109113.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值