本地上传代码到远端覆盖,并重启服务

环境: python2.7

内容: sshpass 搭配ssh,自动跳过指纹验证及密码输入。

 

# -*- coding: utf8 -*-
# /usr/bin/python

import subprocess, sys
import os, commands
afb = "/opt/xdfs-mgnt/mgnt-agent/agent/"
alfb = "xdfs-restful-xfile/xdfs-mgnt-agent/src/agent/"
agent_file = ["hardwares/scsi.py", "xfile.py", "glusterfs_shares.py", "hardwares/logic_disk_01.py"]

mfb = "/opt/xdfs-mgnt/mgnt-master/"
mlfb = "xdfs-restful-xfile/xdfs-mgnt-master/src/"
master_file = ["agent", "shares", "server"]


def scp_method(ip, agent=''):
    # 当ssh 使用-o stricthostkeychecking=no后,会注册进host_knowns文件,所以scp 不需要写-o stricthostkeychecking=no了, 即使写也会报错。
    cmd = "sshpass -p 111111 ssh -o stricthostkeychecking=no root@%s ls" % ip
    ret = commands.getstatusoutput(cmd)
    print ret
    local_files = os.listdir('.')
    if alfb.split('/')[0] not in local_files:
        return
    if agent == 'agent':
        for el in agent_file:
            #cmd = "sshpass -p 111111 ssh -P 22 root@%s: rm -rf %s" % (ip, el)
            #os.system(cmd)
            print el
            if 'hardwares' in el:
                cmd = "sshpass -p 111111 scp -P 22 -r %s root@%s:%s" % (alfb + el, ip, afb + "hardwares")
            else:
                cmd = "sshpass -p 111111 scp -P 22 -r %s root@%s:%s" % (alfb + el, ip, afb)
            os.system(cmd)
        cmd = "sshpass -p 111111 ssh root@%s systemctl restart xdfs-mgnt-agent" % ip
        os.system(cmd)
    else:
        for el in master_file:
            # 因为xfiles 中的migrations文件,所以不能移除。
            #cmd = "sshpass -p sysadmin ssh -P 2222 root@%s: rm -rf %s" % (ip, el)
            #os.system(cmd)
            print el
            cmd = "sshpass -p sysadmin scp -P 2222 -r %s root@%s:%s" % (mlfb + el, ip, mfb )
            os.system(cmd)
        # 不生效
        #cmd = "sshpass -p sysadmin ssh -P 2222 root@%s systemctl restart httpd" % ip
        #os.system(cmd)
            
def main():
    if len(sys.argv) < 3:
        print "need input params"
    scp_method(sys.argv[1], sys.argv[2])
    print 'transaction success.'

if __name__ == "__main__":
    # python scp_file.py ip agent/master
    main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值