Skip to content Skip to sidebar Skip to footer

How I Can List Files From Remote Host Directory Using Python?

I need get the list of files from a remote host directory, running the code in my local machine. Is something like os.listdir() at remote host machine, NOT is os.lisdir() in the lo

Solution 1:

Your best option for running commands on a remote machine is via ssh with paramiko.

A couple of examples of how to use the library and issue a command to the remote system:

import base64
import paramiko

# Let's assign an RSA SSH key to the 'key' variable
key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))

# And create a client instance.
client = paramiko.SSHClient()

# Create an object to store our key  
host_keys = client.get_host_keys()
# Add our key to 'host_keys'
host_keys.add('ssh.example.com', 'ssh-rsa', key)

# Connect to our client; you will need # to know/use for the remote account:##   IP/Hostname of target#   A username #   A password
client.connect('IP_HOSTNAME', username='THE_USER', password='THE_PASSWORD')

# Assign our input, output and error variables to# to a command we will be issuing to the remote # system 
stdin, stdout, stderr = client.exec_command(
    'find /path/data/ -name "pattern*" -mmin -15'
)

# We iterate over stdoutfor line in stdout:
    print('... ' + line.strip('\n'))

# And finally we close the connection to our client
client.close()

As pointed out by the OP, if we already have a known hosts file locally we can do things slightly different:

import base64
import paramiko

# And create a client instance.
client = paramiko.SSHClient()

# Create a 'host_keys' object and load# our local known hosts  
host_keys = client.load_system_host_keys()

# Connect to our client; you will need # to know/use for the remote account:##   IP/Hostname of target#   A username #   A password
client.connect('IP_HOSTNAME', username='THE_USER', password='THE_PASSWORD')

# Assign our input, output and error variables to# to a command we will be issuing to the remote # system 
stdin, stdout, stderr = client.exec_command(
    'find /path/data/ -name "pattern*" -mmin -15'
)

# We iterate over stdoutfor line in stdout:
    print('... ' + line.strip('\n'))

# And finally we close the connection to our client
client.close()

Post a Comment for "How I Can List Files From Remote Host Directory Using Python?"