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

yipeiwu_com4年前服务器

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

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 的回显结果
    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)

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

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

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

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

if(port==0):

这里注意,如果是第一次执行 需要接受 author_key 缓存,还需要注意 是否有更新权限

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

下面的代码使用pexpect生成一个ssh进程,然后连接远程服务器,并执行命令。
在使用下面程序之前,需要先通过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 

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

相关文章

利用python实现对web服务器的目录探测的方法

利用python实现对web服务器的目录探测的方法

一、python Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。 python 是一门简单易学的语言,并且功能强大也很灵活,在渗透测试中的应用广泛,让我们一起打...

PHP实现检测客户端是否使用代理服务器及其匿名级别

要判断客户端是否使用代理服务器,可以从客户端所发送的环境变量信息来判断。 具体来说,就是看HTTP_VIA字段,如果这个字段设置了,说明客户端使用了代理服务器。 匿名级别可以参考下表来判...

Python基于select实现的socket服务器

本文实例讲述了Python基于select实现的socket服务器。分享给大家供大家参考,具体如下: 借鉴了asyncore模块中select.select的使用方法 import...

php实现Linux服务器木马排查及加固功能

网站频繁被挂马?做一些改进,基本上能把这个问题解决,因为discuz x等程序存在漏洞,被上传了websehll,每次被删除过段时间又出来了,最终查到所有的木马。 从以下几个方...

python实现linux服务器批量修改密码并生成execl

批量修改linux服务器密码,同时生成execl表格 复制代码 代码如下:#!/usr/bin/env python#coding:utf8#随机生成自定义长度密码from random...