Created
October 11, 2023 17:09
-
-
Save quantumcore/e111ef66a0c19f6cfaa078db2f3033ea to your computer and use it in GitHub Desktop.
Simple code to upload a file to server via ssh.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import paramiko | |
from scp import SCPClient | |
import argparse | |
def main(): | |
parser = argparse.ArgumentParser(description="Securely transfer files over SSH using SCP") | |
parser.add_argument("ip", help="Remote host IP address") | |
parser.add_argument("lfile", help="Local file path") | |
parser.add_argument("rfile", help="Remote file path") | |
parser.add_argument("ppkey", help="Path to private key file") | |
parser.add_argument("keypass", help="Password for the private key") | |
parser.add_argument("username", help="SSH username") | |
args = parser.parse_args() | |
try: | |
ssh = paramiko.SSHClient() | |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
key = paramiko.RSAKey.from_private_key_file(args.ppkey, password=args.keypass) | |
ssh.connect(args.ip, pkey=key, username=args.username) | |
with SCPClient(ssh.get_transport()) as scp: | |
print(f"uploading {args.lfile} to {args.ip} -> {args.rfile} ... ") | |
scp.put(args.lfile, args.rfile) | |
except Exception as e: | |
print("Error: " + str(e)) | |
print("USAGE: ./script.py host local_file remote_file private_key private_keypass username") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment