Robot Framework SSH tutorial with example
If you are planning to write System or Acceptance tests for your applications, Robot Framework has a lot to offer right out of box, including super sane HTML test reports and beautiful logging. All this goodness only gets better with External Libraries like SSH Library which makes integration and system tests even more fun to write.
Let's explore it little further.
Just a quick look at SSH offerings
Often times we have our applications being tested, developed and being deployed on remote cloud-based systems. A common way to conduct remote operations is usually by using the fantastic SSH Protocol. Its the goto tool to gain remote shell access for most mortal human beings.
But SSH hardy stops there, there are other amazing stuff which you can do with SSH, some of them are being briefly touched upon in the points below.
- Gain full remote shell access (like already mentioned)
- Transfer Files between local and remote (SFTP is built into openSSH)
- Run GUI programs using
X11 forwarding
option - Local port Forwarding and Reverse Tunneling
- Execute commands on remote hosts using Pseudo terminal etc.
Now lets come to the coolest part, shall we!
How do I let my automation script do all the SSH work for me?
Nobody likes to do repetitive tasks and for good reasons, they all should be Automated. Or at least we should try to do so. That's where Robot Framework amazing SSH library comes for rescue. SSH Libraries Keywords will do all the heavy lifting with underlying SSH protocol and makes it easy for us to use those keywords directly in our automation script as we usually would.
Having covered up some groundwork on SSH, let's get down into the coding part. In this part we are going to write a robot script
which will take an SSH connection on a remote host using username, password remote login authentication method and then execute a command (like hostname in our case, but practically could be anything) on the remote host and then dump the output in robot test report file.
Let's get the things rolling!
Automating our first SSH Connection
- Import external SSH library so that Keywords are available to
robot script
*** Settings ***Documentation Robot Framework test script
Library SSHLibrary
2. Declaring the remote host information in the test script file.
As prerequisites to establish a successful SSH tunnel, we should know the hostname or IP Address of remote host along with username and password. My remote device doesn’t have a password so I have left it empty. Update the password variable value as per your environment.
Alias is just a user-friendly name of the connection which you can use later in your scripts to switch the connection (when using multi SSH connections)
*** Variables ***${host} 192.168.120.141
${username} root
${password} ${EMPTY}
${alias} remote_host_1
3. Create a test case table to execute a command ( hostname
in our case) on the remote host by first establishing an SSH tunnel and then connecting to it.
*** Test Cases ***Test SSH Connection
Open Connection ${host} alias=${alias}
Login ${username} ${password} delay=1
Execute Command hostname
4. We might be interested in reading the output provided by the command we executed on remote host above. This code snippet dumps the output received from remote host on robot test report.
${stdout}= Execute Command hostname
Log ${stdout}
Putting it all together
Lets combine everything into a runnable robot test script file.
Contents of test_ssh_rf_demo.robot
File. You can name it whatever you like :)
*** Settings ***
Documentation Robot Framework test script
Library SSHLibrary
*** Variables ***
${host} 192.168.120.141
${username} root
${password} ${EMPTY}
${alias} remote_host_1*** Test Cases ***
Test SSH Connection
Open Connection ${host} alias=${alias}
Login ${username} ${password} delay=1
Execute Command hostname
Close All Connections
Command to run robot script
robot test_ssh_rf_demo.robot
Sample report generated after executing the above test script
MAKE SURE: You already have robot framework and python installed on the system where you are trying to run the script. Else you might witness some annoying pip
and python
error messages.
Interesting use cases
Common remote tasks which you can automate using Robot Framework SSH Library.
- Status check or manage remote service like, is SQL server is running or not
- process checking,
- remote file copy/paste to your automation script.
- publisher-subscriber
- client-server applications
- remote databases or app servers
- or work with any remote machine in general
- remote system service and processes start/stop/check
- Get information from remote machines like
hostname
, or other management information likenetwork configuration
etc. - Periodically collecting monitoring information etc.
Conclusion
With this blog post, we just demonstrate how easy it is to Automate out existing SSH tasks using robo framework
and ssh library.
It takes only 15 lines of robot script to manage most of our general-purpose SSH automation needs.
I will continue writing aboutRobot Framework
and its awesomeness in the upcoming posts, so please stay tuned.