Python (Scripting) - SCP using paramiko

by
Jeremy Canfield |
Updated: August 25 2024
| Python (Scripting) articles
There are a couple ways to go about downloading a file.
- using requests
- using paramiko Secure Copy Protocol (SCP) (this article)
- using urllib
paramiko can be used to:
- SSH connect to a target system
- SCP files on a target system (this article)
- SFTP connect to a target 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
Comments
June 26 2024 by GTPC
It seem like this is actually using SFTP, not SCP