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

利用python 更新ssh 遠(yuǎn)程代碼 操作遠(yuǎn)程服務(wù)器的實(shí)現(xiàn)代碼

 更新時(shí)間:2018年02月08日 23:42:40   作者:Leon  
這篇文章主要介紹了利用python 更新ssh 遠(yuǎn)程代碼 操作遠(yuǎn)程服務(wù)器的實(shí)現(xiàn)代碼,需要的朋友可以參考下

用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 ` 就會(huì) 用 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ù),不然會(huì)報(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 

到這里就結(jié)束了,大家可以參考一下,方法有很多種

相關(guān)文章

最新評論