SSH Tunneling with Robot Framework-SSH LIbrary - robotframework

I am using Robot Framework SSH Library for my automation.
I need to establish SSH Tunnel to a remote machine using Robot Framework and then execute set of tests on the remote machine.
Looks like RF SSH Library do not have support for this.
Can someone point me to any other options available?
Thank you!

You could use Robot Framework to open SSH connection, copy test files, run them and copy results back.
You will need two files, first one is ssh.txt
*** Settings ***
Library SSHLibrary
Suite Setup Open Connection And Log In
Suite Teardown Copy And Delete Files And Close Connections
*** Variables ***
${HOST} your.server.here
${USERNAME} username
${PASSWORD} password
*** Testcases ***
Run Tests on remote server
Put File real-test.txt
Run Test In Server real-test.txt
Sleep 1s
*** Keywords ***
Open Connection And Log In
Open Connection ${HOST}
Login ${USERNAME} ${PASSWORD}
Copy And Delete Files And Close Connections
Copy Results From Server
Clean Up Server real-text.txt
Close All Connections
Run Test In Server
[Arguments] ${filename}
${rc}= Execute Command pybot ${filename} return_stdout=False return_rc=True
Should Be Equal As Numbers ${rc} 0
Copy Results From Server
GetFile log.html
GetFile output.xml
GetFile report.html
Clean Up Server
[Arguments] ${filename}
Write rm log.html
Write rm output.xml
Write rm report.html
Write rm ${filename}
the 2nd file is where real tests are, real-test.txt:
*** Settings ***
Library OperatingSystem
*** Testcases ***
Test 1
FileShouldExist real-test.txt
run this by specifying output files to avoid filename collisions when log files are copied back from server:
pybot -o conn.xml -r conn.html -l connlog.html ssh.txt

I recently did something like this but created the functionality in Python which I then imported to Robot as a keyword.
The jist of it was:
in SSHTunnel.py:
from sshtunnel import SSHTunnelForwarder
(other imports)
class SSHTunnel:
def create_ssh_tunnel(self):
ssh_server = SSHTunnelForwarder(
(host, int(port)),
ssh_username=username,
ssh_password=pass,
remote_bind_address=(remote_bind_host, int(remote_bind_port))
)
ssh_server.start()
def stop_ssh_tunnel(self):
ssh_server.stop()
Now you add the SSHTunnel.py file to the Robot Library and call the keyword Create SSH Tunnel in the beginning of the testcase (can also be in Test Setup or Suite Setup) and Stop SSH Tunnel at the end.
Hope that helps.

Library SSHLibrary
Open Connection host_server
Login With Public Key test /test.pem test12345
Create Local SSH Tunnel 9191 remote_host remote_port bind_address=127.0.0.1
#Once the connection is made you can directly access the resources in remote_host, for eg : i am trying to connect mysql db in remote host
Connect To Database Using Custom Params pymysql database='testDb', user='testuser', password='test123', host='127.0.0.1', port=9191

You can achieve this with the help of 'local port forwarding' feature.
Ref: https://help.ubuntu.com/community/SSH/OpenSSH/PortForwarding.
Use this command on the test server(where the robot is running) in an instance:
ssh -4 -L <listening_port/forwarding_port>:<destination_ip>:destination_port> username#jump_box.com
eg)
ssh -4 -L 50025:10.122.56.45:23 anishg#jumpbox.teski.com
Use the following command in another instance to work in the destination server:
telnet localhost <listening port>
eg)
telnet localhost 50025
Here you can execute the commands as like you are working on the destination server, as the session is opened.
In Robot, this can be achieved by this way also:
The Step (1) - Port forwarding is done in a shell script and this shell script is called in your robot framework.
Do Telnet/other in localhost with port specified
sample.sh:
ssh -4 -L 50025:10.122.56.45:23 anishg#jumpbox.teski.com
Robot Code:
*** Test Cases ***
Port Forwarding
Start Process ./sample.sh shell=True #Port is opened
Log To Console SHELL IS Executing
Telnet.Open Connection localhost port=50025 #Normal op as in destn server
Note: I used jump_box as I dont have access to destination server from my test instance.

Related

Parallel SSH connections to remote host with Robot

I have a simple test, using the SSHLibrary which I'd like to start and run in parallel on multiple hosts. Now I have statically set 'host1'. How would I run this on host1, host2 and host3 at the same time?
*** Settings ***
Documentation Demo
Library SSHLibrary
Suite Setup Open Connection And Log In
Suite Teardown Close All Connections
*** Variables ***
${HOST} host1
${USERNAME} user
*** Test Cases ***
Evaluate command status
[Documentation] Demo
${output} Execute Command net show version
Should Be True $output
*** Keywords ***
Open Connection And Log In
Open Connection ${HOST}
Login With Public Key ${USERNAME} /home/user/.ssh/id_rsa
You can set the variable on the command line with the --variable or -V option:
robot --variable host:host1
robot --variable host:host2
For more information see Setting variables in command line in the robot framework user guide.
Robot iteself doesn't provide any features to run tests in parallel. To run them in parallel you could write a script that runs robot in the background for each host, and then combine the reports from each run into a single unified report.

How to execute shell script with arguments in remote unix server using robot framework from local windows machine

I would appreciate sample test steps to achieve this.
I logged into the server and then cd into the directory where the script resides. Then tried to run the script with Execute Command keyword.
Install and import SSHLibrary, then use below code:
Keyword Name
[Arguments] ${server}=${host_url} ${user}=${USERNAME} ${pass}=${PASSWORD}
Open Connection ${server}
Login ${user} ${pass}
${out} ${err} ${rc}= Execute Command cd ${SH_SCRIPT_PATH};sh ${SH_file_name} return_stdout=True return_stderr=True return_rc=True
Should Be Equal ${rc} ${0}
Should Not Contain ${out} [main]ERROR

Execute command usage fails

*** Settings ***
Documentation Transfer a file from OCU to PC
Library SSHLibrary
Suite Setup Open Connection and Login With Public Key
Suite Teardown Close All Connections
*** Variables ***
${REMOTE HOST} 192.168.x.xxx
${USER} xxx
*** Keywords ***
Open Connection and Login With Public Key
Open Connection ${REMOTE HOST}
Login With Public Key ${USER} /home/bhushan/.ssh/id_rsa
*** Test Cases ***
Run the given command on the console
${pwd}= Execute Command pwd
Should Be Equal ${pwd} /home/root
${output}= Execute Command ls
${output}= Execute Command scp /home/root/myfile.txt bhushan#192.168.x.xxx:/home/bhushan/vdr-reports/
Log ${output}
I have Dropbear of the latest version installed on my target. I am trying to run the above SSH Library Robot framework keyword based test case which defines to transfer a file from remote to the local host. The execute command keyword works in the beginning i.e. ${output}= Execute Command ls, but i am getting an error while using the same keyword in copying the files, i.e i can't see anything happening on the console. It is hanging and i waited for long time to see the output but haven't seen anything. But this works when i copy the files to my local machine manually. Can anyone here try to help me in fixing this error.
KEYWORD ${output} = SSHLibrary . Execute Command scp
/home/root/myfile.txt bhushan#192.168.x.xxx:/home/bhushan/vdr-reports/
Documentation: Executes command on the remote machine and returns
its outputs. Start / End / Elapsed: 20170829 10:26:23.897 / 20170829
10:26:50.148 / 00:00:26.251 10:26:23.898 INFO Executing command 'scp
/home/root/myfile.txt
bhushan#192.168.x.xxx:/home/bhushan/vdr-reports/'...................>
This is what i see in the log

Unable to login to unix server using Plink in batch ifle

we are facing one issue with Plink while running the batch files, we are running batch files using autosys, the batch files are available in my windows client server and one of the batch file will call the plink to connect the unix server but we are facing the issue to connect the unix server, when I run the batch script using command prompt then the plink can be connected the unix server but it is not happening with autosys to run the batch scripts. below is the Plink command...
call %aScrDir%plink -l %hypSrvUser% -pw %hypSrvPwd% %essSvr% "sh /xxxxxxxxxxx"
when we see the error file which is generated by autosys there are some errors
"The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 f9:5e:2a:4a:11:ed:40:91:80:3a:13:04:08:05:e7:ac
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n) Connection abandoned."
could you please give the suggestion to avoid this situations and where do we add the host key in the server.
appreciate your action on this.
Option 1 : If allowed, first start a manual connection to the server and confirm the SSH key
Option 2 : If you are running the last version, there is a -hostkey switch to indicate in command line the expected host key
Option 3 : Use something like
echo N | %aScrDir%plink -l %hypSrvUser% -pw %hypSrvPwd% %essSvr% "sh /xxxxxxxxxxx"
That is, pipe the n character to the plink command to answer no to the query to save the host key.
Try this it would be working fine.
cd to the plink.exe file directory
echo Y | .\plink.exe -pw xxyyxxyy root#host_ip 'ls -lah'

How to open a command line terminal and execute some commands inside robot framework testcase?

I Want to do the following steps:
open a terminal of the same ubuntu machine from where my Robot testcase is running and execute some commands.
written a Robot framework testcase as shown below:
*** Settings ***
Library Telnet
*** Testcases ***
testcase1
open connection 127.0.0.1
write gnome terminal
write ifconfig -a eth0
But its throws "Errno 111 - connection refused" error.
Kindly guide me if anybody have idea on this.
Thanks for your help in advance.
If you don't actually need to open a terminal window, robot has a Process library that lets you run external commands via the Run process keyword. For example:
*** Settings ***
| Library | Process
*** Test cases ***
| Example
| | Run process | ifconfig | -a | eth0
The answer here is twofold
In most (all including Ubuntu) Linux distributions Telnet is closed by default. This is probably true for your case as well.
You could run the telnet server on the Ubuntu machine, or even configure it to run on startup (There are many threads on how to do that).
But as other people said before - running Telnet on your local machine is probably not really what you want. You can use the Process library to run processes on your local host,and even the built in library has a few keywords for that.
Create .bat file and write your commands in that. If your .bat file located in other folder then use cd commands and then your required commands
Example of bat file like
cd C:\robotFramework\runner
java abc.class
Use following syntex
Run xyz.bat : for this use Library OperatingSystem
Or
Run Process xyz.bat : for this use Library Process

Resources