自动化文件上传,版本发布

Purposes:

现在需要将一个文件发送到多个服务器上, 作为非运维的程序员, 我们应该如何解决这个问题呢?

答案是脚本啊

直接一点

一、人生苦短, 我选python

import paramiko, sys 
from collections import defaultdict

port="xx"
username="xxx"
Machines= {
    '1' : ['xx.xx.xx.xx', 'password']
}

if __name__ == '__main__':
    print("发布版本 例如:python send_version.py VERSION")
    print("本脚本环境依赖paramiko")
    print(Machines)
    tag = input("请输入服务器编号:")
    if isinstance(tag, str):
        destination= Machines[tag][0]
        des_password= Machines[tag][1]
    package= "path" + sys.argv[1] + ".tar.gz"

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.load_system_host_keys()
    ssh.connect(destination, port, username, des_password)
    cmd = "sudo su & /path/send_version.sh " + package 
    print(cmd)                                                                                                                           
    stdin, stdout, stderr = ssh.exec_command(cmd)
    result=stdout.readlines()
    print(result)

如何避免scp无尽的密码输入: 利用shell 的expect

下面两个文件是要放到终端主机(文件目的地)上的

send_version.sh 如下:
src_path='/path/'+$4 + '.tar.gz'
dest_path='/destination/'

main()
{
        echo "发送版本 " $3 " 到 ip:" $1
        expect /home/ubuntu/version/send_version.exp $1 $2 $4 $src_path $dest_path
}
#$1 ip
#$2 user
#$3 version
#$4 passwd
main $1 $2 $3 $4
send_version.exp 如下:
set timeout -1

set host [linux $argv 0]
set username [linux $argv 1]
set passwd [linux $argv 2]
set src_path [linux $argv 3]
set dest_path [linux $argv 4]

spawn scp -P 57522 $username@$host:$src_path $dest_path

expect {
        "(yes/no)?"
        {
                send "yes\n"
                expect "*assword:" {send "$passwd\n"}
        }
        "*assword:"
        {
                send "$passwd\n"
        }
}

expect eof
exit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值