利用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 ` 就會 用 paramiko.SSHClient, 來連接 配置于 main 函數(shù)中的 server_list list 中的 2108 的 hostname、username、 password 、port 參數(shù),連接服務(wù)器后,執(zhí)行 execmd 中配置好的命令。這里我用了argv 獲取輸入的參數(shù),來控制要更新的項(xiàng)目路徑。這樣一個利用python ssh 遠(yuǎn)程服務(wù)器,并更新對應(yīng)目錄代碼的腳本就完成了。
這里我配置了兩個服務(wù)器,11這個服務(wù)器,沒有使用到 port ,所以我做了判斷,來控制連接中是否帶 port 參數(shù),不然會報(bào)錯。
if(port==0):
這里注意,如果是第一次執(zhí)行 需要接受 author_key 緩存,還需要注意 是否有更新權(quán)限
python使用ssh連接遠(yuǎn)程服務(wù)器,并執(zhí)行命令代碼
下面的代碼使用pexpect生成一個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)文章
python目標(biāo)檢測yolo2詳解及預(yù)測代碼復(fù)現(xiàn)
這篇文章主要為大家介紹了python目標(biāo)檢測yolo2詳解及其預(yù)測代碼復(fù)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
python識別圖像并提取文字的實(shí)現(xiàn)方法
這篇文章主要介紹了python識別圖像并提取文字的實(shí)現(xiàn)方法,2019-06-06
python中的單引號雙引號區(qū)別知識點(diǎn)總結(jié)
在本篇文章中小編給大家整理了關(guān)于python中的單引號雙引號有什么區(qū)別的相關(guān)知識點(diǎn)以及實(shí)例代碼,需要的朋友們參考下。2019-06-06
Python實(shí)現(xiàn)批量檢測HTTP服務(wù)的狀態(tài)
本文給大家分享的是一個使用python實(shí)現(xiàn)的批量檢測web服務(wù)可用性的腳本代碼,主要功能有測試一組url的可用性(可以包括HTTP狀態(tài)、響應(yīng)時(shí)間等)并統(tǒng)計(jì)出現(xiàn)不可用情況的次數(shù)和頻率等。2016-10-10
python Django里CSRF 對應(yīng)策略詳解
這篇文章主要介紹了python Django里CSRF 對應(yīng)策略詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08

