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

    xiaoxiao2022-07-13  171

    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
    最新回复(0)