Python使用Paramiko控制linux第三方庫(kù)
paramiko是一個(gè)基于SSH用于連接遠(yuǎn)程服務(wù)器并執(zhí)行相關(guān)操作(SSHClient和SFTPClinet,即一個(gè)是遠(yuǎn)程連接,一個(gè)是上傳下載服務(wù)),使用該模塊可以對(duì)遠(yuǎn)程服務(wù)器進(jìn)行命令或文件操作,值得一說(shuō)的是,fabric和ansible內(nèi)部的遠(yuǎn)程管理就是使用的paramiko來(lái)現(xiàn)實(shí)。
Paramiko 是Python 用于控制liunx中文件的第三方庫(kù),可創(chuàng)建文件,修改,刪除文件的內(nèi)容等;
代碼實(shí)例:
# -*- coding:utf-8 -*- import paramiko class ssh(object): def __init__(self,host,port,user,password): self.host = host self.port = port self.user = user self.password = password self.ssh_client = paramiko.SSHClient() self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh_client.connect(self.host, self.port, self.user, self.password) #執(zhí)行指令返回文本字符串 def sftp_exec_command(self,command): arrconfiglist = [""] try: std_in, std_out, std_err = self.ssh_client.exec_command(command) for line in std_out: arrconfiglist.append(line.strip("\n")) del arrconfiglist[0] self.ssh_client.close() return arrconfiglist except Exception as e: print(e,"ssh ERROR") finally: self.ssh_client.close() #執(zhí)行指令無(wú)返回 def sftp_exec_norecommand(self,command): try: self.ssh_client.exec_command(command) self.ssh_client.close() except Exception as e: print(e,"ssh ERROR") finally: self.ssh_client.close() ''' 在別的項(xiàng)目中被調(diào)用使用如下方法 import ssh as ssh if __name__ == '__main__': ssh.ssh().sftp_exec_command("--command information--") ''' ''' if __name__ == '__main__': rect = ssh().sftp_exec_command("") print(rect) '''
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python通過(guò)get,post方式發(fā)送http請(qǐng)求和接收http響應(yīng)的方法
這篇文章主要介紹了python通過(guò)get,post方式發(fā)送http請(qǐng)求和接收http響應(yīng)的方法,涉及Python使用urllib模塊與urllib2模塊實(shí)現(xiàn)get與post發(fā)送數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-05-05filter使用python3代碼進(jìn)行迭代元素的實(shí)例詳解
在本篇文章里小編給大家整理了關(guān)于filter使用python3代碼進(jìn)行迭代元素的實(shí)例詳解內(nèi)容,有興趣的朋友們可以參考下。2020-12-12Python 網(wǎng)絡(luò)編程之UDP發(fā)送接收數(shù)據(jù)功能示例【基于socket套接字】
這篇文章主要介紹了Python 網(wǎng)絡(luò)編程之UDP發(fā)送接收數(shù)據(jù)功能,結(jié)合實(shí)例形式分析了Python使用socket套接字實(shí)現(xiàn)基于UDP協(xié)議的數(shù)據(jù)發(fā)送端與接收端相關(guān)操作技巧,需要的朋友可以參考下2019-10-10Python一行代碼實(shí)現(xiàn)自動(dòng)發(fā)郵件功能
最近在自己學(xué)習(xí)Python爬蟲(chóng),學(xué)到了用Python發(fā)送郵件,覺(jué)得這個(gè)可能以后比較實(shí)用。所以這篇文章主要給大家介紹了如何通過(guò)Python一行代碼實(shí)現(xiàn)自動(dòng)發(fā)郵件功能的相關(guān)資料,需要的朋友可以參考下2021-05-05女神相冊(cè)密碼忘記了 我只用Python寫(xiě)了20行代碼
這篇文章主要介紹了怎么樣用Python寫(xiě)了20行代碼來(lái)破解找回已經(jīng)忘記的密碼,密碼的排列組合是有限的,遍歷嘗試就能夠試出正確的密碼,但這個(gè)工作要怎么用python實(shí)現(xiàn),請(qǐng)看下文2021-08-08