Blog
In telecommunications, the Secure File Transfer Protocol (SFTP) serves as a critical mechanism for secure and reliable file exchange between different network components devices, and systems, whether it is updating configurations, network monitoring, exchanging customer data, or facilitating software updates. Conversely, Python is an ideal tool for the automation of telecommunications networks thanks to its readability and versatility.
Let's dive into how to employ the two effectively and efficiently using the Zato integration and automation platform.
The first step is to define a new SFTP connection in your Dashboard, as in the screenshots below.
The form lets you provide all the default options that apply to each SFTP connection - remote host, what protocol to use, whether file metadata should be preserved during transfer, logging level and other details that you would typically provide.
Simply fill it out with the same details that you would use if it were command line-based SFTP connections.
The next thing, right after the creation of a new connection, is to ping it to check if the server is responding.
Pinging opens a new SFTP connection and runs the ping command - in the screenshot above it was ls . - a practically no-op command whose sole purpose is to let the connection confirm that commands in fact can be executed, which proves the correctness of the configuration.
This will either returns details of why a connection could not be established or the response time if it was successful.
Having validated the configuration by pinging it, we can now execute SFTP commands straight in Dashboard from a command console:
Any SFTP command, or even a series of commands, can be sent and responses retrieved immediately. It is also possible to increase the logging level for additional SFTP protocol-level details.
This makes it possible to rapidly prototype file transfer functionality as a series of scripts that can be next moved as they are to Python-based services.
Now, in Python, your API automation services have access to an extensive array of capabilities - from executing transfer commands individually or in batches to the usage of SFTP scripts previously created in your Dashboard.
Here is how Python can be used in practice:
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class MySFTPService(Service):
def handle(self):
# Connection to use
conn_name = 'My SFTP Connection'
# Get a handle to the connection object
conn = self.out.sftp[conn_name].conn
# Execute an arbitrary script with one or more SFTP commands, like in web-admin
my_script = 'ls -la /remote/path'
conn.execute(my_script)
# Ping a remote server to check if it responds
conn.ping()
# Download an entry, possibly recursively
conn.download('/remote/path', '/local/path')
# Like .download but remote path must point to a file (exception otherwise)
conn.download_file('/remote/path', '/local/path')
# Makes the contents of a remote file available on output
out = conn.read('/remote/path')
# Uploads a local file or directory to remote path
conn.upload('/local/path', '/remote/path')
# Writes input data out to a remote file
data = 'My data'
conn.write(data, '/remote/path')
# Create a new directory
conn.create_directory('/path/to/new/directory')
# Create a new symlink
conn.create_symlink('/path/to/new/symlink')
# Create a new hard-link
conn.create_hardlink('/path/to/new/hardlink')
# Delete an entry, possibly recursively, no matter what kind it is
conn.delete('/path/to/delete')
# Like .delete but path must be a directory
conn.delete_directory('/path/to/delete')
# Like .delete but path must be a file
conn.delete_file('/path/to/delete')
# Like .delete but path must be a symlink
conn.delete_symlink('/path/to/delete')
# Get information about an entry, e.g. modification time, owner, size and more
info = conn.get_info('/remote/path')
self.logger.info(info.last_modified)
self.logger.info(info.owner)
self.logger.info(info.size)
self.logger.info(info.size_human)
self.logger.info(info.permissions_oct)
# A boolean flag indicating if path is a directory
result = conn.is_directory('/remote/path')
# A boolean flag indicating if path is a file
result = conn.is_file('/remote/path')
# A boolean flag indicating if path is a symlink
result = conn.is_symlink('/remote/path')
# List contents of a directory - items are in the same format that .get_info uses
items = conn.list('/remote/path')
# Move (rename) remote files or directories
conn.move('/from/path', '/to/path')
# An alias to .move
conn.rename('/from/path', '/to/path')
# Change mode of entry at path
conn.chmod('600', '/path/to/entry')
# Change owner of entry at path
conn.chown('myuser', '/path/to/entry')
# Change group of entry at path
conn.chgrp('mygroup', '/path/to/entry')
Given how important SFTP is in telecommunications, having a convenient and easy way to automate it using Python is an essential ability in a network engineer's skill-set.
Thanks to the SFTP connections in Zato, you can prototype SFTP scripts in Dashboard and employ them in API services right after that. To complement it, a full Python API is available for programmatic access to remote file servers.
Combined, the features make it possible to create scalable and reusable file transfer services in a quick and efficient manner using the most convenient programming language, Python.
➤ Click here to read more about using Python and Zato in telecommunications
➤ What is a Network Packet Broker? How to automate networks in Python?
➤ What is an integration platform?
➤ Python Integration platform as a Service (iPaaS)
➤ What is an Enterprise Service Bus (ESB)? What is SOA?