python ssh download target image from server host

#!/usr/bin/env python3

# -*- coding: utf-8 -*-
import paramiko
import os
from datetime import date
from configparser import ConfigParser

g3_image_url     = 'http://192.168.65.235:18683/g3/'
g3_image_type    = 'g3-eng-major-image'
g3_gpt           = 'major-image-g3-eng-gpt-nandinfo.img'
ubootenv         = 'uboot-env.bin'
g3_ubootenv      = 'g3-uboot-env.bin'
g3_image         = 'major-image-g3-eng.mubi'
sanity_test_path = 'cd daily_image_sanity_test;'

# day = date.today()
y_m_d = date.today().strftime('%Y-%m-%d')
y_m = date.today().strftime('%Y-%m')
print(y_m_d)
print(y_m)

# 读取配置文件获取服务器的登录信息
def read_ini():
    info = dict()
    cf = ConfigParser()
    cf.read('./config/config.ini', encoding='utf-8')
    keys = cf.options('ssh')
    for each in keys:
        info[each] = cf.get('ssh', each)
    print(info)
    return info

# 连接服务区并执行shell命令返回输出结果
def ssh_test(host, port, username, password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接服务器
    try:
        ssh.connect(hostname=host, port=port, username=username, password=password)
    except Exception as e:
        print(e)
        return

    # 设置一个内部函数,执行shell命令并返回输出结果
    def run_shell(cmd):
        ssh_in, ssh_out, ssh_error = ssh.exec_command(cmd)
        result = ssh_out.read() or ssh_error.read()
        return result.decode().strip()

    # 获取指定文件夹的绝对地址
    cmd_get_path = sanity_test_path +' pwd'
    db_path = run_shell(cmd_get_path)

    # Removed old image files
    cmd_rm_image = sanity_test_path +' rm *g3*'
    rm_ret = run_shell(cmd_rm_image)

    # Get the lastest image files from Server IP
    cmd_wget_g3gpt = sanity_test_path +' wget '+g3_image_url+'/'+y_m+'/'+y_m_d+'/'+g3_image_type+'/'+g3_gpt
    print(cmd_wget_g3gpt)
    g3gpt_ret = run_shell(cmd_wget_g3gpt)

    cmd_wget_ubootenv = sanity_test_path +' wget '+g3_image_url+'/'+y_m+'/'+y_m_d+'/'+g3_image_type+'/'+ubootenv
    print(cmd_wget_ubootenv)
    ubootenv_ret = run_shell(cmd_wget_ubootenv)

    cmd_rename_g3ubootenv = sanity_test_path + ' mv '+ubootenv+' '+g3_ubootenv
    print(cmd_rename_g3ubootenv)
    g3ubootenv_ret = run_shell(cmd_rename_g3ubootenv)

    cmd_wget_g3image = sanity_test_path +' wget '+g3_image_url+'/'+y_m+'/'+y_m_d+'/'+g3_image_type+'/'+g3_image
    print(cmd_wget_g3image)
    g3image_ret = run_shell(cmd_wget_g3image)

    # 获取指定文件夹中文件的名称,并跟上面得到的文件夹绝对地址组合起来
    cmd_get_sqls = sanity_test_path +' ls'
    sqls = run_shell(cmd_get_sqls)
    # print(sqls)

    lis = ['{}/{}'.format(db_path, each) for each in sqls.split('\n')]
    # print(lis)

    # 关闭连接
    ssh.close()
    return lis

# 链接服务器进行文件传输
def sftp_test(host, port, username, password, from_file, to_file):
    transport = paramiko.Transport((host, port))
    try:
        transport.connect(username=username, password=password)
    except Exception as e:
        print(e)
        return
    sftp = paramiko.SFTPClient.from_transport(transport)

    # 将文件下载到本地,如果是上传使用 put
    sftp.get(from_file, to_file)
    transport.close()

if __name__ == '__main__':
    info = read_ini()
    h = info.get('host', None)
    p = int(info.get('port', None)) # 端口是int类型
    u = info.get('username', None)
    pw = info.get('password', None)
    files = ssh_test(h, p, u, pw)

    path = './g3_daily_image'
    if os.path.exists(path):
        pass
    else:
        os.makedirs(path)

    target = [g3_image, g3_gpt, g3_ubootenv]
    if files:
        for each in files:
            name = each.split('/')[-1]
            if name in target:
                print(name)
                ss = sftp_test(h, p, u, pw, each, os.path.join(path, name))


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值