Robot Framework MAC Address test and validation - linux

I run Robot Framework test and it fails me everytime, even I get 1 line or so.
Here is the terminal output for cat command:
RobotFramework:~$ cat macaddresses.txt | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | head -n 1
00:01:c0:24:a3:3b
Here is latest Example:
Check MAC Addresses
${macaddress}= Execute Command ifconfig | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' >> macaddresses.txt
${checkmac}= Execute Command cat macaddresses.txt | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | head -n 1
Should Contain ${checkmac} ([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}
Set Test Message ${checkmac}
If I run it straight from cmd when connected with SSH it works fine and it prints one line of MAC Addressess and that should be fine?
But still I get fail everytime
And here is fail results:
Test MAC Addresses | FAIL |
'00:01:c0:24:a3:3b' does not contain '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
Debugging Log:
20230124 20:08:16.397 : INFO : ${checkmac} = 00:01:c0:24:a3:3b
20230124 20:08:16.398 : FAIL : '00:01:c0:24:a3:3b' does not contain '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
Edit:
Also tried Keyword Should Match Regexp
Should Match Regexp ${checkmac} ([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}
And got error:
20230124 20:24:29.521 : FAIL : '00:01:c0:24:a3:3b' does not match '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'

Well, grep uses a different representation for hex numbers, as you did used :xdigit:. I did not researched, but this looks like a non-standard for Regular Expressions (and first time I see it).
However, I experimented in https://regex101.com/ using Python mode, because that is what Robot Framework uses, and got the following result:
([\d|[a-f]{1,2}:){5}([\d|[a-f]{1,2})
As you can see, each hex pair is constructed with any digit or a-f letters.
This can be validated, with the following Robot Framework test case:
*** Test Cases ***
Test RegEx
${input}= Set Variable 00:01:c0:24:a3:3b
${regex}= Set Variable ([\\d|[a-f]{1,2}:){5}([\\d|[a-f]{1,2})
${result}= Should Match Regexp ${input} ${regex}
Here is the execution log:
Starting test: Test Timeouts.Test RegEx
20230124 21:22:23.128 : INFO : ${input} = 00:01:c0:24:a3:3b
20230124 21:22:23.130 : INFO : ${regex} = ([\d|[a-f]{1,2}:){5}([\d|[a-f]{1,2})
20230124 21:22:23.132 : INFO : ${result} = ['00:01:c0:24:a3:3b', 'a3:', '3b']
Ending test: Test Timeouts.Test RegEx

Related

SNMP Traphandle not working

This is my first time working with SNMP, but after reading the SNMP pages I'm still having trouble getting a simple shell script to run when receiving a trap.
My /etc/snmp/snmptrapd.conf file looks like this:
# Example configuration file for snmptrapd
#
# No traps are handled by default, you must edit this file!
#
disableAuthorization yes
authCommunity log,execute,net public
# the generic traps
traphandle default /usr/local/bin/snmptrapd.sh
The snmptrapd.sh script just says "hello".
#!/bin/sh
echo "hello"
The script is executable and runs when executed independently:
> /usr/local/bin/snmptrapd.sh
hello
The snmptrapd is running as a background process:
> ps -ef | grep snmp
root 29477 1 0 14:49 ? 00:00:00 /usr/sbin/snmptrapd -Lsd -p /var/run/snmptrapd.pid -Cc /etc/snmp/snmptrapd.conf
And yet when I send a trap locally using snmptrap nothing happens:
> snmptrap -v 2c -c public localhost "" NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification netSnmpExampleHeartbeatRate i 123456
>
Now it seems that the trap does get logged, because the system log file (/var/log/messages) has the following entry:
Aug 8 15:46:10 <server_name> snmptrapd[29477]: 2017-08-08 15:46:10 localhost
[UDP: [127.0.0.1]:44928->[127.0.0.1]]:#012DISMAN-EVENT-MIB::sysUpTimeInstance =
Timeticks: (1338382434) 154 days, 21:43:44.34#011SNMPv2-MIB::snmpTrapOID.0 =
OID: NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification#011NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate
= INTEGER: 123456
As far as I can see everything is set up correctly. If so, why is the trap handle not working and how can one check why the trap doesn't trigger the script?
Thanks in advance.
EDIT: When I added the -Ci option to the snmptrapd command line options I got the following error:
No log handling enabled - turning on stderr logging
: Unknown Object Identifier (Sub-id not found: (top) -> )
OK, so after looking around some more I found the answer.
The reason that we are not seeing the output is because snmptrapd is being run as a daemon and doesn't send its standard output to the console. One can replace this with
echo "hello" > $HOME/output.txt
and the word 'hello' appears in the output.txt file.
See also http://www.linuxquestions.org/questions/linux-newbie-8/net-snmp-trap-handling-4175420577/
and
https://superuser.com/questions/823435/where-to-log-stdout-and-stderr-of-a-daemon

Read script file sh [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 4 years ago.
I have a shell script with a command that seems like it should work, but instead it fails with an odd wrapped/truncated/corrupted error message. Example:
$ ls -l myfile
-rw-r----- 1 me me 0 Aug 7 12:36 myfile
$ cat myscript
ls -l myfile
$ bash myscript
: No such file or directory
The file clearly exist, but even if I didn't, this is the kind of error message I would normally get:
$ ls -l idontexist
ls: cannot access idontexist: No such file or directory
Notice how it includes the tool name ls, a message string and the filename while mine does not.
Here's what I get if I try to use mysql instead. The error message looks like it's been wrapped, and now starts with a quote:
Command: mysql -h myhost.example.com
Expected: ERROR 2005 (HY000): Unknown MySQL server host 'myhost.example.com' (0)
Actual: ' (0) 2005 (HY000): Unknown MySQL server host 'myhost.example.com
And here's my trivial ssh command that should work, or at least give a normal error message, but which instead is wrapped to start with a colon and ends with strange clobbering:
Command: ssh myhost
Expected: ssh: Could not resolve hostname myhost: Name or service not known
Actual: : Name or service not knownname myhost
Why does this happen, and how do I fix it?
TL;DR: Your script or data has Windows style CRLF line endings.
Convert to Unix style by deleting the carriage returns.
How do I check if my script or data has carriage returns?
They're detectable as ^M in the output of cat -v yourscript:
$ cat -v myscript
ls -l myfile^M
If your script doesn't have them, your data might -- especially if reading from ini/csv files or curl:
hostname=$(curl https://example.com/loginhost.txt)
ssh "$hostname" # Shows strange error
echo "$hostname" | cat -v # Shows myhost^M
How do I remove them?
Set your editor to save the file with Unix line endings, aka "line terminators" or "end-of-line characters", and resave it.
You can also remove them from a command line with dos2unix yourscript or cat yourscript | tr -d '\r' > fixedscript.
If found in your data, you can pipe your source through tr -d '\r':
hostname=$(curl https://example.com/loginhost.txt | tr -d '\r')
Why do carriage returns cause strange error messages?
The "carriage return" character, aka CR or \r, causes the cursor to move to the start of the line, and continue printing from there. In other words, it starts overwriting the line from the start. This is why they wrap strangely:
Intended: ssh: Could not resolve hostname myhost\r: Name or service not known
Written: ssh: Could not resolve hostname myhost\r
Overwritten: : Name or service not known
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Result: : Name or service not knownname myhost

SSH inside perl script hangs

I am trying to execute the following command using perl script :
my $sum = `$sudo $ssh $server netstat -Aan | /bin/grep 1158 | /bin/egrep '#IP[0]' | wc -l`;
Where $sudo = full path to sudo command , same for $ssh , $server = the file server that I want to pull the data from , and #Ip[0] = ip address.
When I run the exact command from the shell (not as a script) I get the required result (which is an integer number). However , when I run the script it just gets hangs when it reaches to that point.
I cant see anything wrong with the syntax , I am using back ticks to save the returned output , could you please assist?

How to run a command in bash script from another directory [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 4 years ago.
I have a shell script with a command that seems like it should work, but instead it fails with an odd wrapped/truncated/corrupted error message. Example:
$ ls -l myfile
-rw-r----- 1 me me 0 Aug 7 12:36 myfile
$ cat myscript
ls -l myfile
$ bash myscript
: No such file or directory
The file clearly exist, but even if I didn't, this is the kind of error message I would normally get:
$ ls -l idontexist
ls: cannot access idontexist: No such file or directory
Notice how it includes the tool name ls, a message string and the filename while mine does not.
Here's what I get if I try to use mysql instead. The error message looks like it's been wrapped, and now starts with a quote:
Command: mysql -h myhost.example.com
Expected: ERROR 2005 (HY000): Unknown MySQL server host 'myhost.example.com' (0)
Actual: ' (0) 2005 (HY000): Unknown MySQL server host 'myhost.example.com
And here's my trivial ssh command that should work, or at least give a normal error message, but which instead is wrapped to start with a colon and ends with strange clobbering:
Command: ssh myhost
Expected: ssh: Could not resolve hostname myhost: Name or service not known
Actual: : Name or service not knownname myhost
Why does this happen, and how do I fix it?
TL;DR: Your script or data has Windows style CRLF line endings.
Convert to Unix style by deleting the carriage returns.
How do I check if my script or data has carriage returns?
They're detectable as ^M in the output of cat -v yourscript:
$ cat -v myscript
ls -l myfile^M
If your script doesn't have them, your data might -- especially if reading from ini/csv files or curl:
hostname=$(curl https://example.com/loginhost.txt)
ssh "$hostname" # Shows strange error
echo "$hostname" | cat -v # Shows myhost^M
How do I remove them?
Set your editor to save the file with Unix line endings, aka "line terminators" or "end-of-line characters", and resave it.
You can also remove them from a command line with dos2unix yourscript or cat yourscript | tr -d '\r' > fixedscript.
If found in your data, you can pipe your source through tr -d '\r':
hostname=$(curl https://example.com/loginhost.txt | tr -d '\r')
Why do carriage returns cause strange error messages?
The "carriage return" character, aka CR or \r, causes the cursor to move to the start of the line, and continue printing from there. In other words, it starts overwriting the line from the start. This is why they wrap strangely:
Intended: ssh: Could not resolve hostname myhost\r: Name or service not known
Written: ssh: Could not resolve hostname myhost\r
Overwritten: : Name or service not known
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Result: : Name or service not knownname myhost

Using vim-go, gocode and godep doesn't work together

I am using YCM, installing with this option --gocode-completer.
After having started Vim, I see the gocode daemon :
$ ps -ef | grep gocode
501 53255 1 0 4:36PM ?? 0:00.03 /Users/yamo/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/completers/go/../../../third_party/gocode/gocode -s -sock unix -addr localhost:37373
Completion works well on a built-ins (like fmt) but not on a dependency in GoDeps.
gocode displays the following message
: Gocode returned empty JSON response
Nevertheless, godep seems have been correctly detected
:GoPath displays
/Users/yamo/Projects/.go/src/github.com/YannMoisan/openshift-skeleton/Godeps/_workspace:/Users/yamo/Projects/.go/vendor/:/Users/yamo/Projects/.go
Any idea to fix that ?

Resources