Skip to content

Instantly share code, notes, and snippets.

@XiGou
Created October 12, 2022 07:01

Revisions

  1. XiGou created this gist Oct 12, 2022.
    32 changes: 32 additions & 0 deletions upload_file.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #! /bin/python3
    # alternative for scp command

    import logging
    import paramiko


    class SSHClient:
    def __init__(self, host, user, psw) -> None:
    self.ssh = paramiko.SSHClient()
    self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    self.ssh.connect(
    host,
    username=user,
    password=psw,
    port=22,
    allow_agent=False,
    look_for_keys=False,
    )
    def upload_file_to_dir(self, file, dir):
    sftp = self.ssh.open_sftp()
    sftp.put(file, dir)
    sftp.close()

    remode_dir = "/data/xxx"
    host = "x.x.x.x"
    user = "root"
    psw = "password"
    logging.basicConfig(level=logging.DEBUG)
    ssh = SSHClient(host, user, psw)
    local_filename = "xxx.py"
    ssh.upload_file_to_dir(local_filename, f"{remode_dir}/{local_filename}")