溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何利用python更新ssh遠(yuǎn)程代碼

發(fā)布時(shí)間:2021-04-14 10:12:48 來源:億速云 閱讀:277 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)如何利用python更新ssh遠(yuǎn)程代碼,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

用python paramiko ssh 服務(wù)器,并pull對應(yīng)目錄代碼的腳本

pull.py

import paramiko
import sys

def sshclient_execmd(hostname, port, username, password, execmd):
  paramiko.util.log_to_file("paramiko.log")

  s = paramiko.SSHClient()
  s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  if(port==0):
    s.connect(hostname=hostname, username=username, password=password)
  else:
    s.connect(hostname=hostname, port=port, username=username, password=password)
  stdin, stdout, stderr = s.exec_command(execmd)
  stdin.write("Y") # Generally speaking, the first connection, need a simple interaction.

  print stdout.read()

  s.close()


def main(server,project):
# def main():
  server_list = {'2108':{'hostname':'112.22.22.22','username':'root','password':'123456','port':2108},
         '11':{'hostname':'192.168.1.11','username':'root','password':'123456','port':0}
          }

  if(server == '118'):
    execmd = "cd /workspace/" + project + "/ && git pull"
    info = os.popen(execmd).read()  # 這里是更新本地的,可以返回打印出cmd 的回顯結(jié)果
    print info

  up_list = server_list[server]
  hostname = up_list['hostname']
  port = up_list['port']
  username = up_list['username']
  password = up_list['password']

  execmd = "cd /workspace/" + project +  "/ && git pull"
  sshclient_execmd(hostname, port, username, password, execmd)


if __name__ == "__main__":
  server = str(sys.argv[1])
  project = str(sys.argv[2])
  main(server,project)

上面的是更新遠(yuǎn)程 服務(wù)器上 project 目錄pull 的源碼。

/workspace/" + project + "/ && git pull

比如運(yùn)行 `python pull.py 2108 web ` 就會 用 paramiko.SSHClient, 來連接 配置于 main 函數(shù)中的 server_list list 中的 2108 的 hostname、username、 password 、port 參數(shù),連接服務(wù)器后,執(zhí)行 execmd 中配置好的命令。這里我用了argv 獲取輸入的參數(shù),來控制要更新的項(xiàng)目路徑。這樣一個(gè)利用python ssh 遠(yuǎn)程服務(wù)器,并更新對應(yīng)目錄代碼的腳本就完成了。

這里我配置了兩個(gè)服務(wù)器,11這個(gè)服務(wù)器,沒有使用到 port ,所以我做了判斷,來控制連接中是否帶 port 參數(shù),不然會報(bào)錯(cuò)。

if(port==0):

這里注意,如果是第一次執(zhí)行 需要接受 author_key 緩存,還需要注意 是否有更新權(quán)限

python使用ssh連接遠(yuǎn)程服務(wù)器,并執(zhí)行命令代碼

下面的代碼使用pexpect生成一個(gè)ssh進(jìn)程,然后連接遠(yuǎn)程服務(wù)器,并執(zhí)行命令。
在使用下面程序之前,需要先通過easy_install pexpect安裝pexpect程序。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pexpect

def ssh_cmd(ip, passwd, cmd):
  ret = -1
  ssh = pexpect.spawn('ssh root@%s "%s"' % (ip, cmd))
  try:
    i = ssh.expect(['password:', 'continue connecting (yes/no)?'], timeout=5)
    if i == 0 :
      ssh.sendline(passwd)
    elif i == 1:
      ssh.sendline('yes\n')
      ssh.expect('password: ')
      ssh.sendline(passwd)
    ssh.sendline(cmd)
    r = ssh.read()
    print r
    ret = 0
  except pexpect.EOF:
    print "EOF"
    ssh.close()
    ret = -1
  except pexpect.TIMEOUT:
    print "TIMEOUT"
    ssh.close()
    ret = -2
  return ret

關(guān)于“如何利用python更新ssh遠(yuǎn)程代碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI