Sometimes, we want to run scp in Python.
In this article, we’ll look at how to run scp in Python.
How to run scp in Python?
To run scp in Python, we can use paramiko
.
To install it, we run
pip install paramiko
Then we use it by writing
import paramiko
from scp import SCPClient
def create_ssh_client(server, port, user, password):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(server, port, user, password)
return client
ssh = create_ssh_client(server, port, user, password)
scp = SCPClient(ssh.get_transport())
to create the create_ssh_client
function.
In it, we create a SSHClient
object.
Then we load host keys on the system with load_system_host_keys
.
We call set_missing_host_key_policy
to set the policy to use when connecting to servers without a host key.
Then we call connect
to connect to the server
with the port
and credentials.
Next, we call create_ssh_client
create an SSH client.
And then we create a SCPClient
with the object returned by ssh.get_transport()
to create the SCP client.
Conclusion
To run scp in Python, we can use paramiko
.