Bootstrap FreeKB - Python (Scripting) - SCP using paramiko
Python (Scripting) - SCP using paramiko

Updated:   |  Python (Scripting) articles

There are a couple ways to go about downloading a file.

paramiko can be used to make an SSH connection onto a target system and to then use scp.put to transfer a file from the local system to the remote system or scp.get to get a file from the remote system.

At the bare minimum, the following is all that should be needed to PUT a file.

#!/usr/bin/python
import paramiko

ssh = paramiko.SSHClient()
ssh.connect("<hostname or IP address of target system>", username="<username>",password="<password>")

scp = ssh.open_sftp()
scp.put('/path/to/local.file', '/path/to/remote.file')

scp.close()
ssh.close()

 

Or to GET a file.

#!/usr/bin/python
import paramiko

ssh = paramiko.SSHClient()
ssh.connect("<hostname or IP address of target system>", username="<username>",password="<password>")

scp = ssh.open_sftp()
scp.get('/path/to/remote.file', '/path/to/local.file')

scp.close()
ssh.close()

 

Here is a more complete PUT example.

#!/usr/bin/python3
import paramiko

def ssh_connect(server, user, pw):
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(server, username=user, password=pw, look_for_keys=False)
  return ssh

def ssh_exec(ssh, command):
  ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)
  stdout = ssh_stdout.read().splitlines()
  return stdout

server = "server1.example.com"

try:
  ssh = ssh_connect(server, "john.doe", "itsasecret")
except Exception as exception:
  print(f"Failed to make SSH connection to {server} due to the following exception: {exception}")
  sys.exit(1)
else:
  stdout = ssh_exec(ssh, "the command you want to issue on the target system")
  print(f"stdout = {stdout}")
  ssh.close()

try:
  scp = ssh.open_sftp()
except Exception as exception:
  print(f"ssh.open_sftp() raised the following exception: {exception}")
else:
  print("successfully opened SFTP")

try:
  scp.put('/path/to/local.file', '/path/to/remote.file')
except Exception as exception:
  print(f"scp.put raised the following exception: {exception}")
else:
  print("successfully scp put")

try:
  scp.close()
except Exception as exception:
  print(f"scp.close() raised the following exception: {exception}")
else:
  print("successfully closed scp")

 

And here is a more complete GET example.

#!/usr/bin/python3
import paramiko

def ssh_connect(server, user, pw):
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(server, username=user, password=pw, look_for_keys=False)
  return ssh

def ssh_exec(ssh, command):
  ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)
  stdout = ssh_stdout.read().splitlines()
  return stdout

try:
  ssh = ssh_connect("server1.example.com", "john.doe", "itsasecret")
except Exception as exception:
  print(f"Failed to make SSH connection to {server} due to the following exception: {exception}")
  sys.exit(1)
else:
  stdout = ssh_exec(ssh, "the command you want to issue on the target system")
  print(f"stdout = {stdout}")
  ssh.close()

try:
  scp = ssh.open_sftp()
except Exception as exception:
  print(f"ssh.open_sftp() raised the following exception: {exception}")
else:
  print("successfully opened SFTP")

try:
  scp.get('/path/to/remote.file', '/path/to/local.file')
except Exception as exception:
  print(f"scp.get raised the following exception: {exception}")
else:
  print("successfully scp get")

try:
  scp.close()
except Exception as exception:
  print(f"scp.close() raised the following exception: {exception}")
else:
  print("successfully closed scp")

 

Be aware that paramiko may append unwanted events to your log file. If you are using logging, you can set paramiko to only log events at a certain log level, such as WARNING.

logging.getLogger("paramiko").setLevel(logging.WARNING)

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 30aa4f in the box below so that we can be sure you are a human.