python如何利用paramiko執(zhí)行服務(wù)器命令
話不多說直接上代碼
封裝連接
@staticmethod
def connect(ip, server_user, server_port, server_path):
"""
連接服務(wù)器
: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__(),
}
在服務(wù)器執(zhí)行命令
def fabric_run_cmd(self, ip, server_user, server_port, server_path, cmd):
"""
批量在服務(wù)器執(zhí)行命令
: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': '執(zhí)行成功',
'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
}
將文件發(fā)送到服務(wù)器
def fabric_put_file(self, ip, src_file, dst_file, server_user, server_port, server_path):
"""
發(fā)送文件到服務(wù)器
: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) # 發(fā)送文件之前先判斷有沒有目標(biāo)文件夾 如果沒有現(xiàn)在服務(wù)器上創(chuàng)建文件夾
ftp.close() # 在此判斷文件是否發(fā)送成功
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
}
能力有限,如有不妥請(qǐng)留言指正
以上就是python如何利用paramiko執(zhí)行服務(wù)器命令的詳細(xì)內(nèi)容,更多關(guān)于python 執(zhí)行服務(wù)器命令的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一文詳解Python中Reduce函數(shù)輕松解決復(fù)雜數(shù)據(jù)聚合
這篇文章主要為大家介紹了Python中Reduce函數(shù)輕松解決復(fù)雜數(shù)據(jù)聚合示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Python unittest基本使用方法代碼實(shí)例
這篇文章主要介紹了Python unittest基本使用方法代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Python實(shí)現(xiàn)從訂閱源下載圖片的方法
這篇文章主要介紹了Python實(shí)現(xiàn)從訂閱源下載圖片的方法,涉及Python采集的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
pytorch?液態(tài)算法實(shí)現(xiàn)瘦臉效果
在PS中,我們可以利用液化工具對(duì)人像進(jìn)行形變處理,例如瘦臉、瘦腿、放大眼睛等一系列的常規(guī)操作。今天我們來了解一下這些操作的算法原理,并用pytorch來實(shí)現(xiàn)瘦臉效果2021-11-11
使用 python pyautogui實(shí)現(xiàn)鼠標(biāo)鍵盤控制功能
pyautogui是一個(gè)可以控制鼠標(biāo)和鍵盤的python庫(kù),類似的還有pywin32。這篇文章主要介紹了python中的pyautogui實(shí)現(xiàn)鼠標(biāo)鍵盤控制功能,需要的朋友可以參考下2019-08-08
用Python實(shí)現(xiàn)童年貪吃蛇小游戲功能的實(shí)例代碼
這篇文章主要介紹了用Python實(shí)現(xiàn)童年貪吃蛇小游戲功能的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Python自然語(yǔ)言處理停用詞過濾實(shí)例詳解
這篇文章主要為大家介紹了Python自然語(yǔ)言處理停用詞過濾實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的驗(yàn)證碼程序
這篇文章主要介紹了Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的驗(yàn)證碼程序,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Python與Appium實(shí)現(xiàn)手機(jī)APP自動(dòng)化測(cè)試的示例代碼
本文主要介紹了Python與Appium實(shí)現(xiàn)手機(jī)APP自動(dòng)化測(cè)試的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

