gpt4 book ai didi

python 调用scp命令 实践

转载 作者:知者 更新时间:2024-03-12 20:00:10 29 4
gpt4 key购买 nike

pip install scp

pip install pexpect

测试代码:

import os
import stat

import paramiko  # 用于调用scp命令

def scp_demo():
    from scp import SCPClient

    # 将指定目录的图片文件上传到服务器指定目录
    # remote_path远程服务器目录
    # file_path本地文件夹路径
    # img_name是file_path本地文件夹路径下面的文件名称
    def upload_img(img_name, remote_path="/var/www/html/public/thinkfont/competitor/data/font/oppo/preview_pic", file_path="D:\PythonProject\img"):
        # img_name示例:07670ff76fc14ab496b0dd411a33ac95-6.webp
        host = "***.***.***.***"  # 服务器ip地址
        port = 22  # 端口号
        username = "duanyuanjin"  # ssh 用户名
        password = "duanVlife"  # 密码

        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        ssh_client.connect(host, port, username, password)
        scpclient = SCPClient(ssh_client.get_transport(), socket_timeout=15.0)
        local_path = file_path + "\\" + img_name
        try:
            scpclient.put(local_path, remote_path)
        except FileNotFoundError as e:
            print(e)
            print("系统找不到指定文件" + local_path)
        else:
            print("文件上传成功")
        ssh_client.close()

def pex():
    import pexpect
    localpath=''

    sqldb = localpath +database                    #获取database名字
    if os.path.exists(sqldb):
        os.chmod(sqldb ,stat.S_IRWXU)              #如果database存在,直接改为可读写格式
    else:                                         #
        #/如果不存在,则远程登录服务器去提取
        child = pexpect.spawn("scp severA@localpath:/path/ " +database +"  " +localpath)   #/ /拷贝到本地目录
        child.expect("serverA@ip's password:")
        child.sendline("password")
        child.interact()
    os.chmod(sqldb ,stat.S_IRWXU)  #把复制到本地的database改为可读写格式

if __name__ == '__main__':
    scp_demo()

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com