Python使用Paramiko控制liunx第三方庫
paramiko是一個基于SSH用于連接遠(yuǎn)程服務(wù)器并執(zhí)行相關(guān)操作(SSHClient和SFTPClinet,即一個是遠(yuǎn)程連接,一個是上傳下載服務(wù)),使用該模塊可以對遠(yuǎn)程服務(wù)器進(jìn)行命令或文件操作,值得一說的是,fabric和ansible內(nèi)部的遠(yuǎn)程管理就是使用的paramiko來現(xiàn)實(shí)。
Paramiko 是Python 用于控制liunx中文件的第三方庫,可創(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í)行指令無返回 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)’’’
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP.NET MVC遍歷驗(yàn)證ModelState的錯誤信息2. 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實(shí)現(xiàn)方法3. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明4. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. asp中response.write("中文")或者js中文亂碼問題8. PHP設(shè)計模式中工廠模式深入詳解9. CSS hack用法案例詳解10. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測可用)
