Python3 paramiko 实现ssh远程自动文件更新

1. 使用paramiko 库

2. 实现sftp 文件上传

3. 实现ssh 通过root 权限执行命令

import os
import paramiko
import time


# hostname = "10.0.0.99"
# username = "liantong"
# password = "liantong123"


def upload_files(hostname, username, password, files):
    # http_daemon_file = "http-server-haifan-daemon"
    # deepstream_file = "deepstream-haifan-multistream"
    # rc_local = "rc.local"
    # 1 创建transport通道
    tran = paramiko.Transport((hostname, 22))
    tran.connect(username=username, password=password)
    # 2 创建sftp实例
    sftp = paramiko.SFTPClient.from_transport(tran)

    # 3 执行上传功能
    remote_path = "/home/liantong/"  # 远程路径
    for f in files:
        f = f.strip('\n')
        print("upload file {}".format(f))
        put_info = sftp.put(f, remote_path + f, confirm=True)
        print(put_info)

    # 5 关闭通道
    tran.close()

def exec_root_cmd(hostname, username, password, cmd):
    s = paramiko.SSHClient()
    s.load_system_host_keys()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname=hostname, port=int(22), username=username, password=password)
    ssh = s.invoke_shell()
    time.sleep(1)
    ssh.send('sudo su')
    ssh.send('\n')
    buff = ''
    while not buff.endswith('password for liantong: '):
        resp = ssh.recv(9999)
        buff += resp.decode('utf-8')
        # print(resp.decode('utf-8'))
    # print(buff)
    ssh.send(password)
    ssh.send('\n')
    buff = ''
    while not buff.endswith('# '):
        resp = ssh.recv(9999)
        buff += resp.decode('utf-8')
        print(resp.decode('utf-8'))

    # stdin, stdout, stderr = s.exec_command(cmd)
    # result = stdout.read()
    # print(result)
    ssh.send(cmd)
    ssh.send('\n')
    buff = ''
    while not buff.endswith('# '):
        resp = ssh.recv(9999)
        buff += resp.decode('utf-8')
        print(resp.decode('utf-8'))
    print("exec cmd {} done ...".format(cmd))
    s.close()


def update_server_files(hostname, username, password, files):
    exec_root_cmd(hostname, username, password, "ps -ef |grep http-server-haifan-daemon|awk '{print $2}' |xargs kill -9")
    exec_root_cmd(hostname, username, password, "ps -ef |grep deepstream-haifan-multistream|awk '{print $2}' |xargs kill -9")
    time.sleep(1.2)
    http_daemon_file = "http-server-haifan-daemon"
    deepstream_file = "deepstream-haifan-multistream"
    rc_local = "rc.local"
    jar_file = "video-analyse-0.0.1-SNAPSHOT.jar"
    for f in files:
        f = f.strip('\n')
        if f == http_daemon_file:
            exec_root_cmd(hostname, username, password, "cp /home/liantong/" + http_daemon_file + " /opt/multistream_server/core_server/" + http_daemon_file)
        elif f == deepstream_file:
            exec_root_cmd(hostname, username, password, "cp /home/liantong/" + deepstream_file + " /opt/multistream_server/core_server/" + deepstream_file)
        elif f == rc_local:
            exec_root_cmd(hostname, username, password, "cp /home/liantong/" + rc_local + " /etc/" + rc_local)
        elif f == jar_file:
            exec_root_cmd(hostname, username, password, "cp /home/liantong/" + jar_file + " /opt/multistream_server/web_server/" + rc_local)


def clean_server_files(hostname, username, password, files):
    for f in files:
        f = f.strip('\n')
        exec_root_cmd(hostname, username, password, "rm /home/liantong/" + f)

# 所有需要更新的IP列表
ip_file = "ip_config.txt"
# 需要更新的文件列表
update_file = "update_file.txt"

with open(ip_file) as f:
    ips = f.readlines()
with open(update_file) as f:
    update_files = f.readlines()


for ip in ips:
    ip = ip.strip('\n')
    print("try to update {} ...".format(ip))
    upload_files(ip, "root", "123", update_files)
    update_server_files(ip, "root", "123", update_files)
    clean_server_files(ip, "root", "123", update_files)

    print("update {} done ...".format(ip))

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值