python如何利用paramiko执行服务器命令
作者:JanWong 发布时间:2022-09-29 03:39:34
标签:python,服务器命令,paramiko
话不多说直接上代码
封装连接
@staticmethod
def connect(ip, server_user, server_port, server_path):
"""
连接服务器
:param :
:return:
"""
ssh = paramiko.SSHClient()
private_key = paramiko.RSAKey.from_private_key_file('{}.ssh/id_rsa'.format(server_path))
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(ip, port=server_port, username=server_user, pkey=private_key, timeout=5, allow_agent=True)
return ssh
except Exception as e:
app.logger.info('connect_error info is {}'.format(e.__repr__()))
return {
'result': 1,
'ip': ip,
'msg': e.__repr__(),
}
在服务器执行命令
def fabric_run_cmd(self, ip, server_user, server_port, server_path, cmd):
"""
批量在服务器执行命令
:return:
"""
conn = self.connect(ip, server_user, server_port, server_path)
try:
if type(conn) == dict:
return conn
else:
stdin, stdout, stderr = conn.exec_command(cmd)
app.logger.info('fabric_run_cmd_stdout info is {}'.format(stdout.readlines()))
app.logger.info('fabric_run_cmd_stderr info is {}'.format(stderr.readlines()))
channel = stdout.channel
status = channel.recv_exit_status()
conn.close()
app.logger.info('fabric_run_cmd_status info is {}'.format(status))
if status == 0:
return {
'ip': ip,
'msg': '执行成功',
'result': 0
}
else:
return {
'ip': ip,
'msg': stderr.readlines(),
'result': 1
}
except Exception as e:
app.logger.info('fabric_run_cmd_error info is {}'.format(e.__repr__()))
return {
'ip': ip,
'msg': e.__repr__(),
'result': 1
}
将文件发送到服务器
def fabric_put_file(self, ip, src_file, dst_file, server_user, server_port, server_path):
"""
发送文件到服务器
:return:
"""
conn = self.connect(ip, server_user, server_port, server_path)
if type(conn) == dict:
return conn
else:
try:
ftp = conn.open_sftp()
ftp.put(dst_file, src_file) # 发送文件之前先判断有没有目标文件夹 如果没有现在服务器上创建文件夹
ftp.close() # 在此判断文件是否发送成功
return {
'ip': ip,
'msg': '上传成功',
'result': 0
}
except Exception as e:
app.logger.info('fabric_put_file_error info is {}'.format(e.__repr__()))
return {
'ip': ip,
'msg': e.__repr__(),
'result': 1
}
能力有限,如有不妥请留言指正
来源:https://www.cnblogs.com/wangjian941118/p/11810578.html


猜你喜欢
- 第一部分:判断两张图片是否相同要查找重复的图片,必然绕不开判断两张图片是否相同。判断两张图片简单呀!图片可以看成数组,比较两个数组是否相等不
- 一、MySQL数据库的实例管理器概述:1、MySQL数据库的实例管理器(IM)是通过TCP/IP端口运行的后台程序,用来监视和管理MySQL
- 使用MySQLdb连接数据库执行sql语句时,有以下几种传递参数的方法。1.不传递参数conn = MySQLdb.connect(user
- 网上关于Python的音视频播放示例都集中在简单的多媒体库或者PyGame这样的游戏库,有些库使用简单,但功能单一,有些库功能丰富,支持的格
- 目录前言连接管理额外连接管理端口总结前言下面这个报错,相信大多数童鞋都遇见过;那么碰到这个问题,我们应该怎么办呢?在MySQL 5.7及之前
- 而str.replace(/\-/g,"!")则可以全部替换掉匹配的字符(g为全局标志)。replace()The re
- 我们知道在国内使用 Docker,无论是 Pull、Build 还是 Push 镜像都十分慢,因为毕竟很多源都是国外的源,下载和上传慢是必然
- 最近需要爬取某网站,无奈页面都是JS渲染后生成的,普通的爬虫框架搞不定,于是想到用Phantomjs搭一个代理。Python调用Phanto
- 本文实例讲述了Python实现合并同一个文件夹下所有PDF文件的方法。分享给大家供大家参考,具体如下:一、需求说明下载了网易云课堂的吴恩达免
- 首先我们看公式:这个是要拟合的函数然后我们求出它的损失函数, 注意:这里的n和m均为数据集的长度,写的时候忘了注意,前面的theta0-th
- 一、torch.rand():构造均匀分布张量的方法torch.rand是用于生成均匀随机分布张量的函数,从区间[0,1)的均匀分布中随机抽
- 一、问题描述一段 Python 代码在本地的 IDE 上运行正常,部署到服务器运行后,出现了 ModuleNotFoundError: No
- 1.安装下载网址:http://phantomjs.org/download.html选择合适的版本。然后解压即可。环境变量的配置:进入解压
- 1、Select元素2、定位select方法一:二次定位先定位 select 框,再定位 select 里的选项但有时候选项是无法定位的,所
- 阅读上一章:chapter 5 表单Chapter 6 <strong>,<em>与其他短语元素在引言和前面的章节中
- MySQL使用环境变量TMPDIR的值作为保存临时文件的目录的路径名。如果未设置TMPDIR,MySQL将使用系统的默认值,通常为/tmp、
- 内存管理:概述在Python中,内存管理涉及到一个包含所有Python对象和数据结构的私有堆(heap). 这个私有堆的管理由内部的Pyth
- 我就废话不多说了,大家还是直接看代码吧~#! /usr/bin/env python# -*- coding:utf-8 -*-import
- 作者:AngelGavin 出处:CSDNInternet Explorer 5.0 对 XML 提供哪个级别的支持?Inter
- 正则表达式正则表达用来匹配字符串正则表达式匹配过程依次拿出表达式和文本中的字符串进行比价如果每个字符都能匹配,则匹配成功;一旦有匹配不成功的