Automating tasks using Python for sending files through SFTP

Scheduled background tasks are a staple of IT world. For example, some things (programs) should be done in a certain time in each day,  or some procedure should be run a few times in an hour. In this we will discuss how to send a file to another server using SFTP.

Crontab

Most of the time, background tasks are triggered using UNIX-like crontab, there are some alternatives as well but lets say it is out of current post's scope. The crontab entry usually calls a shell script that in its own triggers other program, such calling a  web site using wget or curl, or invoking application command (php yiic commandname). 
The existing crontab entry for sending the file to another server uses a shell script to prepare the files, create control files, and sending them across the network using sshpass with input redirection.

The Challenge

The problem we're facing is that sometimes the remote server doesn't respond normally. The cause is still unknown, and after getting incomplete files in the remote end usually we ends up resending them (which means there is some business consequences of late processing). There is a probability that network issue is the culprit. In the application side in other hand, the shell script doesn't do retry until about 2 hours later.

Transforming to Python

The idea is that the retry should be done as soon after error message is being detected. Because the author has limited capability in enhancing shell script logic, the author choose to create a python script to do exception handling and retry.
Before coding the solution in python, a due diligence is in order to find out which python library could be used to do the task at hand (i.e. sending a file using SFTP protocol). At first the author tried to use SCPClient  in scp package (https://github.com/jbardin/scp.pyhttps://pypi.org/project/scp/), but finds out that the remote server blocks SCP functionality and there are no function are provided to check existence a file in the remote or gather remote file size. Then finally the author check the Paramiko sFTP capability ( https://docs.paramiko.org/en/stable/api/sftp.html ) and decided to use it. To install the paramiko sftp use this command : 

The new automated task using Python

The new python code does the equivalent process of sending the file using SFTP, and do it 5 times in a loop, with the code detecting if the file already exists in the remote server with the equivalent size, and breaking the loop if that is the case. Exception handling wrapped the process, ensuring any error will result the python code still in command (and not exiting the python script). Please refer to this Gist URL : https://gist.github.com/yudhiwidyatama/f6cf9a00b6d2edd4a040ecc95098991a

Aftermath

The resulting information log lines from the python script shows that sometimes the remote server not responding, and after about 3x tries it would reconnect and proceed the process. The 5x retry somehow still suffice, and more thorough investigation will be needed because the failure rate is quite high.

Comments

Popular posts from this blog

Long running process in Linux using PHP

Reverse Engineering Reptile Kernel module to Extract Authentication code

Repair Your Windows Store and Metro Apps After Windows Update