A simple test connection with the "nc" tool using Robotframework? - frameworks

I'm trying to execute a simple Test Connection test in Robotframework, between two IP addresses in the same network. What I'm trying to achieve is:
On one side
Execute Command echo Hello | nc -l 51111
to catch the "Hello" message on the other:
Execute Command 'nc ip 51111'
, using the standard netcat tool.
For this, I run the Test Script.robot:
Library Process
Library SSHLibrary
Suite Setup Open Connection And Log In
Suite Teardown Close All Connections
*** Variables ***
${HOST} ip.my.dev.vm
${USERNAME} user
${PASSWORD} password
${HOST_PDG} ip.server1
${HOST_IDPF} ip.server2
*** Test Cases ***
Step 1 Connect Server1 and send Hello to space
Open Connection And Log In Server1
${output}= Execute Command 'echo Hello | nc -l 51111'
Should Not Be Equal ${output} Hello
Step 2 Connect to Server2 and retrieve Hello
Open Connection And Log In Server2
${output}= Execute Command 'nc ip.server2 51111'
Should Be Equal ${output} Hello
*** Keywords ***
Open Connection And Log In
Open Connection ${HOST}
Login ${USERNAME} ${PASSWORD}
Open Connection And Log In Server1
Open Connection ${HOST_PDG}
Login ${USERNAME} ${PASSWORD}
Open Connection And Log In Server2
Open Connection ${HOST_IDPF}
Login ${USERNAME} ${PASSWORD}
This script stops after the first passed test step, and from the Server2 I never able to catch this Hello message.
The same test over command line with the nc tool works perfectly.
ssh Server1
echo Hello | nc -l 51111
ssh Server2
nc ip.server2 51111
(see the Hello message)
What is the problem to do it over the ROBOT Framework?
Thank you in advance!

The issue you're facing is because the robotframework execution is serial, while your aim implies parallelism - after running the netcat server on Server1, in parallel you want to connect to it from Server2.
This is what you're doing when you did it manually, but this is not what happened in the RF cases: in the framework, you've ran test 1, it did its steps, and finished (with some pass/fail status, but that's not important now). When it was done, the service was not running; so when in test 2 you tried to connect to it - it rightfully failed.
Here's what you can do - make the two steps (bringing up the service, and connecting to it) be executed in parallel. The easiest way is for that to be in the same case; and for the service to be running in the background you have to use not the Execute Command - which waits for its target to finish, but Start Command which starts it in the background immediately returns.
Its usage is a little bit different, in terms of getting the output and communicating with it, you can read how in the doc.

...and I found that...
Without changing the original script much, if the quotes around the both command are deleted, and Start Command is used instead of Execute Command, at the listener side (as Todor suggested, thank you!), the script will works as it is, giving the right results.

Related

Busybox wget strange behaviour

I need some help with a really strange problem.
I use wget (busybox) to obtain the the IP address of some remote clients, to use it on a DNS (a sort of "homemade ddns"). Those clients run a script that every 5 mins calls
wget -O /dev/null "https://my_dns.org/poll.php?user=User_N&pwd=password_N"
Everything was fine, until I updated my http server to remove TLS1.0/TLS1.1
After updating: running the above command on the clients' console it still works OK, while running it automatically (launching it from a script in /etc/init.d) I get this error:
Connecting to my_dns.org (www.xxx.yyy.zzz:443)
wget: error getting response: Connection reset by peer
...Any idea about why does this happen, and how to fix...?
(The shell on the clients runs as root...)
Thank you in advance for your help
Regards

How to run ZAP scan in command line?

I am running pen test on asp.net core web app using the tool OWASP ZAP. When I am running the test using the windows app of Owasp ZAP, the tests are running fine and giving results but when I am trying to run the tests using command line I am seeing this exception.
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x000001CCBD907D60>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
Why is this happening and how to correct this?
I changed the ZAP_PATH environment variable to the folder where zap.sh is located. Now I am getting a different exception:
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response
Following the documentation here and here I managed to run the basic scan from Windows command line.
From the directory where the ZAP is installed, in my case C:\Program Files\OWASP\Zed Attack Proxy run the following command:
PS C:\Program Files\OWASP\Zed Attack Proxy> java -jar zap-2.10.0.jar -cmd -quickurl http://example.com/ -quickprogress
You can use zap docker image run run the test
Baseline Scan
docker run -t owasp/zap2docker-stable zap-baseline.py -t http://google.com
Full Scan
docker run -t owasp/zap2docker-stable zap-full-scan.py -t http://google.com

Secure Socket Agent Proxies listener Error

I try to run the server for ssa by using ssaserver& command but I face this error on my server machine
from yesterday.
Error creating listening socket at [host name : port number] the network address may be in use.
I really don't know why it is showing this error and how can be the address in use.
My OS is RedHat7.
That is really happen because you maybe closed your terminal or logged out from current user account in RedHat.
To solve this issue you can use this command here:
ps -ef|grep ssaserver
Which will show you if the service ssaserver already in use and from this you can get the service id and child ids.
You can then kill the service id by using kill command:
kill #(serviceid)
Here is a link to show you how to use kill command: Link_1
But you can face this problem again and again so I think it will be fine if you will use nohup command to run the service you need.
Note: nohup is a command used to run a process(job) on a server and have it continue after you have logged out or otherwise lost connection to the server
Such as:
nohup ssaserver&
Here is extra link for nohup examples: Link_2

Rye::Box commands failing on remote server

Firstly, I can ssh into the remote server and execute the following commands
cd public_html
du -sh
each successful & exiting with code 0.
Automating the process with Rye::Box & with option safe: false
rbox.cd :public_html
does change directory but also returns exit code -1
rbox.execute 'du -sh'
fails with error message "SocketError::getaddrinfo: Name or service not known"
Would appreciate an explanation if possible.
Check your hosts entry for 127.0.0.1
You might have to add a hostname in /etc/hosts for 127.0.0.1.
A similar question addresses this issue on SO.
See also
SocketError (getaddrinfo: Name or service not known) - Sunspot/Solr Rails development

stop / start windows service using findstr

I need to stop and start a service when certain parameters are met in a .txt file.
When my app can't connect to a database in our test environment (regular occurence) it generates the following file type in the server folder (file date changes with each occurence)
SelfTest-20141126181000-RED.txt
Within this file is the phrase
RED: Server failed to start: Unable to open database: Unable to connect to database
Every time this is generated, along with this phrase I need to stop and start a specific service.
I have never used findstr command but I think it could be useful here. Textfile needs wildcarding obviously, and i would like a results file generating with the data and time service was stopped and started.
Would the following work?
findstr "RED: Server failed to start: Unable to open database: Unable to connect to databaseā€ *RED.txt
if %errorlevel%==0 (
sc stop "my service"
sc start "my service"
echo %DATE% %TIME% database >> results.txt
)
Plan is to then have this running through scheduled tasks every 30mins or so
Well, obviously once you have one such file, your script will continue to find it and restart your service every 30 minutes. You'd need to delete or move the RED file.
The second problem is that the sc start command may fail while the service is still being stopped - sc stop returns as soon as the request is delivered, and does not wait. You'd need a repeated sc query "ServiceName" | find "RUNNING" check. And while the service is still running, give it some more time by running ping -n 2 127.0.0.1 > nul. Busy-waiting isn't nice, but there's no clean sleep command so ping is the common hack for that.