python如何利用paramiko执行服务器命令
作者:JanWong 时间:2022-09-29 03:39:34
话不多说直接上代码
封装连接
@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
标签:python,服务器命令,paramiko
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java使用dbcp2数据库连接池
2024-01-29 05:33:42
![](https://img.aspxhome.com/file/2023/8/97798_0s.jpg)
Python获取女友聊天记录详细流程
2022-06-29 21:38:19
![](https://img.aspxhome.com/file/2023/9/134929_0s.png)
关于mysql 的时间类型选择
2024-01-17 11:44:20
基于python检查矩阵计算结果
2021-02-22 22:56:32
python 获取计算机的网卡信息
2023-08-11 21:12:06
MySQL 视图,第1349号错误
2008-05-18 13:04:00
JavaScript 实现 Konami Code
2009-05-26 12:44:00
![](https://img.aspxhome.com/file/UploadPic/20095/26/facebook-55s.jpg)
利用python实现JSON文档与Python对象互相转换
2023-01-02 01:30:46
![](https://img.aspxhome.com/file/2023/1/87811_0s.png)
python接口自动化如何封装获取常量的类
2023-04-17 07:44:22
pip 20.3 新版本发布!即将抛弃 Python 2.x(推荐)
2022-04-06 05:10:06
![](https://img.aspxhome.com/file/2023/1/127681_0s.png)
JavaScript 中定义函数用 var foo = function () {} 和 function foo()区别介绍
2024-04-16 09:26:51
asp如何让用户也能修改密码?
2010-05-13 16:41:00
使用Python可设置抽奖者权重的抽奖脚本代码
2023-08-27 09:17:19
MySQL表设计优化与索引 (七)
2010-10-25 20:06:00
python使用numpy读取、保存txt数据的实例
2021-04-05 22:38:49
![](https://img.aspxhome.com/file/2023/0/72950_0s.jpg)
详细解读python操作json文件的详细
2021-01-31 10:41:42
PowerBI和Python关于数据分析的对比
2023-07-08 15:52:45
![](https://img.aspxhome.com/file/2023/8/117988_0s.gif)
JS获取对象代码总结
2011-03-07 16:14:00
mysql root密码的重设方法(亲测可用)
2024-01-25 13:37:18
详解MySql基本查询、连接查询、子查询、正则表达查询
2024-01-25 18:20:55
![](https://img.aspxhome.com/file/2023/5/100345_0s.png)