How to Access SFTP File Server through a Jump Server using Python

Hardik Zinzuvadiya
3 min readMar 3, 2023

--

In this blog article, we’ll go over how to use a Jump server as a gateway to establish an SFTP connection and download files from a remote server. We’ll do this by using Python programming.

Let’s first understand the core concepts.

SFTP: What is it?

Secure File Transmission Protocol is known as SFTP. Data is encrypted with this private FTP (File Transfer Protocol) version to prevent unauthorised access. SFTP is a safe method of transferring files between servers because it makes use of SSH (Secure Shell) for authentication and encryption.

What exactly a Jump server is?

A server used as a gateway to link to other servers in a network is known as a jump server. It serves as a middleman, transferring data between computers.

Let’s proceed on to the code with these ideas in mind.

Making an SFTP Connection and Downloading Data

The Paramiko Python library will be used to establish an SFTP link and download files from a remote server using a Jump server. Paramiko is a Python implementation of SSHv2, making it an excellent option for SFTP connections.

To run Python code, install few libraries.

pip3 install paramiko # Install python Library
pip3 install sys

The below code explains how to set up an SFTP connection and download files using a Jump server from a remote server.

import paramiko
import sys

# Define the jump server details
JUMP_SERVER_HOSTNAME = 'JUMP_SERVER_HOSTNAME'
JUMP_SERVER_USERNAME = 'JUMP_SERVER_USERNAME'
JUMP_SERVER_PASSWORD = 'JUMP_SERVER_PASSWORD'

# Define the destination server details
DEST_SERVER_HOSTNAME = 'DEST_SERVER_HOSTNAME'
DEST_SERVER_USERNAME = 'DEST_SERVER_USERNAME'
DEST_SERVER_PASSWORD = 'DEST_SERVER_PASSWORD'
REMOTE_FILE_PATH = '/path/to/remote/directory/file.txt'
LOCAL_FILE_PATH = '/path/to/local/file.txt'

def main():
try:
# Create Connection to Jump Server
jump_transport = paramiko.Transport(JUMP_SERVER_HOSTNAME)
jump_transport.connect(username=JUMP_SERVER_USERNAME, password=JUMP_SERVER_PASSWORD)
print("\nConnected to the jump server...")
try:
# Setting up jump transport
jump_channel = jump_transport.open_channel("direct-tcpip", (DEST_SERVER_HOSTNAME, 22), ("127.0.0.1", 0))
jump_transport.name = "jump_transport"
except Exception as e:
print(f"\nError setting up jump transport: {e}")
jump_transport.close()
sys.exit()
# Create Connection to SFTP Server
destination_transport = paramiko.Transport(jump_channel)
destination_transport.connect(username=DEST_SERVER_USERNAME, password=DEST_SERVER_PASSWORD)
print("\nConnected to the destination server")
try:
sftp = paramiko.SFTPClient.from_transport(destination_transport)
print("\nReading Files...")
sftp.get(REMOTE_FILE_PATH, LOCAL_FILE_PATH)
print(f"\nDownloaded file at ({LOCAL_FILE_PATH}) location")
sftp.close()
print("\nClosed Connection")
except Exception as e:
print(f"Error downloading file: {e}")
destination_transport.close()
jump_transport.close()
sys.exit()

# Close the connections
destination_transport.close()
jump_transport.close()
except Exception as e:
print("\nError: ", e)

if __name__ == '__main__':
main()

The variables for the SFTP connection, such as the Jump server and the destination server, were first set up in the code above. Also, we indicate the path to the remote file we wish to download as well as the location in which we wish to save it locally.

Then, we use the Paramiko Transport class to establish a connection to the Jump server. Then, we use the transport object of the Jump server’s open channel method to establish a channel to the destination server. To make the jump server act as a gateway to the destination server, we pass in the direct-tcpip option.

Once the connection is established with the destination server, we create an SFTP client object using the from_transport method on the paramiko.SFTPClient class. We use this client object to download the remote file using the get method.

At the end, we will be cutting off the SFTP and SSH connections to the Jump server and the destination server.

Conclusion

In this blog post, we have learnt how to use a Jump server as a gateway to establish an SFTP connection and download files from a distant server. In Python, we have utilised the Paramiko library to accomplish.

--

--

Hardik Zinzuvadiya
Hardik Zinzuvadiya

Written by Hardik Zinzuvadiya

Innovative software engineer with a passion for exploring cutting-edge technologies and pushing the boundaries of what's possible.