Robot Framework SSH tutorial with example

Avi Mehenwal
4 min readAug 31, 2019

--

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.

  1. Gain full remote shell access (like already mentioned)
  2. Transfer Files between local and remote (SFTP is built into openSSH)
  3. Run GUI programs using X11 forwarding option
  4. Local port Forwarding and Reverse Tunneling
  5. 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 scriptwhich 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

  1. 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 ( hostnamein 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
Robot Framework SSH tutorial with example

Command to run robot script

robot test_ssh_rf_demo.robot

Sample report generated after executing the above test script

robot framework SSH connection test report

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 like network 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.

--

--