欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用Paramiko控制linux第三方庫(kù)

 更新時(shí)間:2022年01月10日 09:19:54   作者:Mr_Wmn  
這篇文章主要介紹了Python使用Paramiko控制linux第三方庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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繪制心形曲線完整代碼實(shí)現(xiàn)

    Python繪制心形曲線完整代碼實(shí)現(xiàn)

    這篇文章主要介紹了Python繪制心形曲線的相關(guān)資料,通過(guò)numpy和matplotlib庫(kù)計(jì)算坐標(biāo)并繪圖,代碼包含導(dǎo)入庫(kù)、定義函數(shù)、生成參數(shù)、計(jì)算坐標(biāo)、繪圖和顯示圖形等步驟,展示了數(shù)學(xué)與編程的結(jié)合美感,需要的朋友可以參考下
    2024-10-10
  • 最新評(píng)論