久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python如何利用paramiko執(zhí)行服務(wù)器命令

瀏覽:4日期:2022-07-06 09:35:35

話(huà)不多說(shuō)直接上代碼

封裝連接

@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.channelstatus = 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ā)送文件之前先判斷有沒(méi)有目標(biāo)文件夾 如果沒(méi)有現(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)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 泗阳县| 修文县| 横山县| 建平县| 河东区| 巴东县| 吉木萨尔县| 龙胜| 宜章县| 古田县| 文水县| 新源县| 伊川县| 射阳县| 精河县| 芒康县| 黎川县| 青铜峡市| 婺源县| 清镇市| 革吉县| 太谷县| 梁平县| 福海县| 阿拉善右旗| 灵台县| 南投市| 蚌埠市| 开封县| 虎林市| 呼伦贝尔市| 孟津县| 宁都县| 襄汾县| 彰化县| 新丰县| 宜君县| 衡南县| 扎兰屯市| 清水河县| 东乡族自治县|