INTRO

First of all, I am not a professional in python or in any language. reality is I don’t know any other language except for Python. but I can create some simple scripts. first, I heard about Expect script for Bash. but I have very little idea about bash scripting. so, I tried to search for something like expect in Python. and that’s how found the pexpect lib.

Have you ever noticed that you can’t interact with the child process directly with a script? A child process occurs while running some tools and utilities in Linux and sometimes in windows. for example, when you run SSH or FTP. you will find that you get separated immediately.

Screenshot:

You can see that we are being asked for a password. well, you can’t input the password without Pexpect. and it is a kind of child process.

Pexpect is a pure Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect works like Don Libes’ Expect. Pexpect allows your script to spawn a child application and control it as if a human were typing commands. read more…

 

INSTAllATION

You can install this with pip and easy. I have pip like everyone else. so, I can just type this command to install it:

pip install pexpect

Remember that pip is for python2. but to install Pexpect for python3, you have to use pip3 or python3 -m pip.

To install it with easy type this command:

easy install pexpect

 

¶ SCRIPT

A few days ago, I made my own python script with the Pexpect library. I named it SFFD. It can download any file from SSH and FTP. It is very easy to use. It takes arguments. it is fast, simple, and reliable.

Go to My GitHub page: github.com/luckythandel/SFFD

I would like to explain this script a little. so, you may understand the Pexpect library better.

Explanation

In the stating lines, I have imported some modules.

Screenshot:

Then, I have written some lines for arguments which are not necessary. you can use user input instead of these arguments. you can also call it positional parameters. but I like to call it arguments. I used argparse lib to make my script fast. so, it won’t ask for user input every time.

 

screenshot:

After that, I have specified a function that exits the script with a message if any given argument has no value.

Screenshot:

Now, I have defined some variables whose value comes from the arguments.

Screenshot:

And now the main functions come where I have used the Pexpect module.

Screenshot:

child variable holds the all ssh child processes.we have defined the pexpect.spawn and ssh command in it to start a parent process and to work with its child processes. For example, we can run any command in a system if we have the correct SSH creds.

The child.expect expects output from the service SSH and the sendline is to send the command. until the string that we have given as argument in except doesn’t come in the output, it will wait. and once it comes it will go to the next line which is sendline.

 

In Pexpect, we use if condition as shown below.

Screenshot:

So, i variable is used here to define conditions. it works with if condition and tells if the result includes username then what would be the next commands. But if you type wrong credentials in the command. then it will print a message and exit the program.

My program tries to get the file via the Netcat tool. but if it doesn’t work. then it will use the SCP command. I have also defined the Netcat listener in my script. so, won’t need to create one.

Screenshot:

 

The next function is for the FTP service. we are using expect, sendline, and if condition same as before. in line 80 and 87, there are two if conditions. The second one is to give us a message when we are entered in FTP with provided creds.

Screenshot:

If the username or password is incorrect then the script will exit with a message. and it will download the file from the server easily.

The first if statement is to tell the user if the there is an FTP server is running or not.

In the end lines, I have called these two functions(FTP/SSH) upon user input.

Screenshot:

if the user enters ssh in command line then it will run ssh function and if the user input is ftp then it will run the ftp connection.

 

 

Thanks For Visiting.

LEAVE A REPLY

Please enter your comment!
Please enter your name here