sending TCP message via command line on macosx - sockets

For testing purposes I am sending tcp messages to a local server as follows:
echo -e "some message" | netcat localhost 1234
With netcat installed using brew install netcat.
This works fine except for that this blocks for quite a long time (about a minute). I tried to use the options "-w 1" for specifying the timeout, but this does not change anything.
The process listening on the other end is a spring-xd tcp source.
Is there any other way of sending a tcp message that does not block as long?

I've not seen such a delay on linux; haven't tried on OS X (it comes with nc instead).
What is your stream definition? The default tcp source expects data to be terminated with CRLF - e.g telnet localhost 1234. You need a RAW decoder for netcat.
EDIT:
I just tested
xd:>stream create foo --definition "tcp --decoder=RAW | log" --deploy
with
$ echo "foo" | nc localhost 1234
and had no problems.

Related

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

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.

How do I execute a python file when I receive a specific msg from Mosquitto broker

I have made an android app that publishes topics to Cloudmqtt(powered by AWS ) using Mqtt protocol. My raspberrypi has a local Mosquitto broker which is bridged with remote Cloudmqtt. So as soon as I publish a topic via my android app it is received on the raspberrypi console.
Here comes the problem. As soon as I receive the msg on my raspberrypi console it should execute a python file. How can it be done?
I write this following command to receive the topic
mosquitto_sub -h host_name -p port_no -u remote_user_name -P remote_password -t hello/today/world
Now as soon as I receive a msg through the topic hello/today/world I have to run a python file that makes decisions according to the msg received from the topic(if the msg received is on, the led turns on)
But the main problem is that how can we read the msg and execute the file accordingly?
This morning I saw someone posted a similar question and I took up the challenge to find out the solution but I am not getting any way to do so.
I tried to write in shell but it didn't work
c=mosquitto_sub -h host_name -p port_no -u remote_user_name -P remote_password -t hello/today/world
if ( c == "rushabh welcome to garden assistant" )
then
GPIO mode 12 out
GPIO mode 12 1
GPIO mode 12 0
else
echo "not matched"
fi
Since I want to turn on the led at gpio 12 as soon as I receive rushabh welcome to garden assistant from the mosquitto broker
Can we use $SYS/broker/messages/stored

Reading syslog-ng logs from the unix socket diretly

I have nginx configured to use syslog and unix sockets:
access_log syslog:server=unix:/var/run/syslog-ng.sock,tag=ngw_access,facility=local7,severity=info;
The syslog service then sends the logs to a remote location.
I would like to read the logs from the socket file directly rather than changing the configuration of nginx to put logs into a text file each time I have to debug the message.
I tried using NC (netcat) both traditional and BSD versions:
$ nc -luD /var/run/syslog-ng.sock
nc: getaddrinfo: Servname not supported for ai_socktype
Let me know if this is possible to do.
P.S.
$ sudo update-alternatives --config nc
There are 2 choices for the alternative nc (providing /bin/nc).
Selection Path Priority Status
------------------------------------------------------------
* 0 /bin/nc.openbsd 50 auto mode
1 /bin/nc.openbsd 50 manual mode
2 /bin/nc.traditional 10 manual mode

Netcat: using nc -l port_number instead of nc -l -p port_number

This question is following this one: Sockets working in openSUSE do not work in Debian?
When working with sockets on my Debian system, I have to use nc -l -p port_number to simulate the server I want to talk with. If I'm using nc -l port_number, it will fail when using the socket connect function and strerror(errno) will say "Connection refused".
Netcat without -p option is working great on other Linux distributions, what should I change on my configuration?
Do not adjust your set. There are multiple implementations of netcat out there; not all of them behave the same.
In particular, the "traditional" version of netcat, which is probably what you have installed on your Debian system, will end up doing something totally unexpected if you omit the -p ("port") flag: it will end up treating the last argument as a hostname, pass it to inet_aton(), which will convert it to a nonsensical IP address (e.g, 1234 will become 0.0.4.210), and will then proceed to ignore that IP address and listen on a socket with an automatically assigned (probably random) port number.
This behavior is obviously silly, so some other implementations of netcat will assume you meant -p. The one you're using doesn't, though, so pass the -p option.
I agree with duskwuff that it is better to just use the -p option everywhere, but to answer your question:
The one thing you have to do is install a netcat that supports the syntax you want. I know the netcat-openbsd package supports it. I know the netcat-traditional package does not. There's also a netcat6 package, which also doesn't. You can then explicitly request the OpenBSD version of netcat like so:
nc.openbsd -l 4242
Optionally you may use the alternatives system to set this version of netcat to run when you issue the nc command:
update-alternatives --set nc /bin/nc.openbsd
This will be done automatically for you if this is the only netcat you've installed.
Finally, you may, again optionally, remove the netcat you don't like (netcat-traditional or netcat6).

How to send a notification to another user with notify-send

notify-send displays a notification box with the message that you want to display on your own machine.
Is there a way to use notify-send to send a notification message to another user and display the message on his machine?
Bash can write to network sockets but can't listen/read. You could use GNU Netcat for this functionality.
A network notify-reader listening on port 10000 (no security):
#!/bin/bash
# no multiple connections: needs to improve
while true; do
line="$(netcat -l -p 10000)"
notify-send -- "Received Message" "$line"
done
And a corresponding client:
#!/bin/bash
host="$1"
echo "$#" >/dev/tcp/$host/10000
So you can send messages using
notify-sender.sh your-host message