利用python 更新ssh 远程代码 操作远程服务器的实现代码

时间:2018-02-09关注公众号来源:网络

  用Python paramiko ssh 服务器,并pull对应目录代码的脚本

利用python 更新ssh 远程代码 操作远程服务器的实现代码

  pull.py

?

1

2
3
4

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Stream Vera Sans Mono', 'Courier New', Courier, monospace !important; float: none !important; border-top-width: 0px !important; border-bottom-width: 0px !important; height: auto !important; color: rgb(0, 102, 153) !important; vertical-align: baseline !important; overflow: visible !important; top: auto !important; right: auto !important; font-weight: bold !important; left: auto !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;" class="py keyword">importparamiko
importsys
 
defsshclient_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.
 
  printstdout.read()
 
  s.close()
 
 
defmain(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 的回显结果
    printinfo
 
  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)

  上面的是更新远程 服务器上 project 目录pull 的源码。

  /workspace/" + project + "/ && git pull比如运行 `python pull.py 2108 web ` 就会 用 paramiko.SSHClient, 来连接 配置于 main 函数中的 server_list list 中的 2108 的 hostname、username、 password 、port 参数,连接服务器后,执行 execmd中配置好的命令。这里我用了argv 获取输入的参数,来控制要更新的项目路径。这样一个利用python ssh 远程服务器,并更新对应目录代码的脚本就完成了。

  这里我配置了两个服务器,11这个服务器,没有使用到 port ,所以我做了判断,来控制连接中是否带 port参数,不然会报错。

  if(port==0):这里注意,如果是第一次执行 需要接受 author_key 缓存,还需要注意 是否有更新权限

  python使用ssh连接远程服务器,并执行命令代码

  下面的代码使用pexpect生成一个ssh进程,然后连接远程服务器,并执行命令。

  在使用下面程序之前,需要先通过easy_install pexpect安装pexpect程序。

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
importpexpect
 
defssh_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)
    ifi ==0:
      ssh.sendline(passwd)
    elifi ==1:
      ssh.sendline('yes')
      ssh.expect('password: ')
      ssh.sendline(passwd)
    ssh.sendline(cmd)
    r =ssh.read()
    printr
    ret =0
  exceptpexpect.EOF:
    print"EOF"
    ssh.close()
    ret =-1
  exceptpexpect.TIMEOUT:
    print"TIMEOUT"
    ssh.close()
    ret =-2
  returnret

  到这里就结束了,大家可以参考一下,方法有很多种

阅读全文
扫码关注“ 多特资源库
更多更全的软件资源下载
文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站)
玩家热搜

相关攻略

正在加载中
版权
版权说明

文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站)

电话:13918309914

QQ:1967830372

邮箱:[email protected]

toast