How to send one byte symbol to server socket with netcat? - sockets

There's already a working server service via socket, and I would like to test it via netcat. I'm using Mac OS X Lion. Currently, I'm able to connect to server via port, to send packet, but the packet contains wrong value. Here are the details:
I need to send 'm' symbol to the server and the server will return 00000000, a zero byte as a response. Server guy told me, server receives 'A0' when I'm sending 'm', and server receives '313039A' when I'm sending '109'. How to define sending format or something, I just need to send 'm' (01101101)?

You can send just "m" with
echo -n 'm' | nc <server> <port>
You can easily check what you're sending on your local machine:
# in one Terminal start the listener:
$ nc -l 1234 | hexdump -C
00000000 6d |m|
00000001
# in other Terminal send the packet:
$ echo -n 'm' | nc 127.0.0.1 1234
nc will happily send/receive NUL bytes - there is no problem with that:
# sending side
$ echo -n X | tr X '\000' | nc 127.0.0.1 1234
# receiving side
$ nc -l 1234 | hexdump -C
00000000 00 |.|
00000001

Related

How does OpenCV handle TCP Connections?

I setup a NetCat Video Stream from my RPi and I am accessing it with OpenCV in the following way:
videoStream = cv2.VideoCapture("tcp://#<my_ip>:<my_port>/")
...
videoStream.release()
Unfortunately I cannot connect to the Stream multiple times without reinitializing it. How does OpenCV tread my tcp connection? Does .release() properly close the socket or what is the right way to close it?
I would comment but I do not have enough points. I had a similar issue. Ultimately, what worked for me is the run netcat with the -k option, which does allow reconnecting:
on RPI:
/opt/vc/bin/raspivid -n -t 0 -w 640 -h 360 -fps 30 -ih -fl -l -o - | /bin/nc -klvp 5000
for nc, the -k option keeps the port listening after the first client disconnects, thereby allowing you to reconnect. You won't need the -v option, it just adds some verbosity.
Another alternative is to
on receiver (Ubuntu, Win10):
nc x.x.x.x 5000 | mplayer -fps 200 -demuxer h264es -
or
gst-launch-1.0 -v tcpclientsrc host=10.60.66.237 port=5000 ! decodebin ! autovideosink
Python code with opencv:
import cv2
cap = cv2.VideoCapture("tcp://10.60.66.237:5000")
while(True):
ret, frame = cap.read()
cv2.imshow('frame', frame)
# the 'q' button is set as the
# quitting button you may use any
# desired button of your choice
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Disconnect and reconnect all you want :)

not getting any data from the listed triggers in zabbix

I am little confuse in zabbix triggers expresion, right now i am triggering an application count netstat -anp | grep 1433 |wc -l but I am getting no data from these trigger, can any one please help?
thanks in advance
item proc.num[1433] trigger {hostname:proc.num[1433].last()}>1500
Zabbix Agent doesn't have any key to count TCP/UDP connections. To collect the result of netstat you need a custom script, through user parameter. Also note that grep -c prints the number of lines, for example:
To count established connections on port 443:
netstat -an | egrep -c ":443 *ESTABLISHED"

Rotating per packets receiving by TCPDUMP

How can I use 'tcpdump' command to capture and save each received packets to separate files (having rotatation per packet without losing any packets).
How about saving dump to a file and then splitting that to separate files?
$ sudo tcpdump -c 10 -w mycap.pcap
tcpdump: data link type PKTAP
tcpdump: listening on pktap, link-type PKTAP (Packet Tap), capture size 65535 bytes
10 packets captured
you'll need to have wireshark installed for this to work (e.g. with brew install wireshark on Mac or apt-get on Ubuntu)
$ editcap -c 1 mycap.pcap output.pcap
10 packets captured -> 10 files created
$ ls -la output* | wc -l
10

tshark doesn't always print source ip

How can i get the tcp payload of packets with tshark, and also get the source IP that sent these packets?
This command works for most packets, but some packets are still printed WITHOUT a source IP (Why?) :
tshark -Y "tcp.dstport == 80" -T fields -d tcp.port==80,echo -e echo.data -e ip.src
*To test my command, run it and then browse to http://stackoverflow.com. Notice that usually the data chunks ("47:45:54:20:2f:61:64:73:...") have an IP after them, but not always.
I found the problem:
The packets with a missing source IP were IPv6, but my original command only prints IPv4.
This works:
tshark -Y "tcp.dstport == 80" -T fields -d tcp.port==80,echo -e echo.data -e ip.src -e ipv6.src

Whois query works with telnet but not netcat

I am trying to write an advanced whois client, so I have been experimenting with sending commands to whois servers using netcat (nc) in Arch Linux. For example, this works great:
$ echo domain google.com | nc whois.crsnic.net 43
# => nc outputs whois data for google.com
However, the whois server that handles suffixes like .br.com is whois.centralnic.net and that server seems to not work with netcat. When I give it any query, it seems to simply close the connection without a response:
$ echo foobar | nc whois.centralnic.net 43
# => No output from nc.
I successfully made the same query using telnet:
$ telnet whois.centralnic.net 43
Trying 193.105.170.136...
Connected to whois.centralnic.net.
Escape character is '^]'.
foobar
DOMAIN NOT FOUND
Connection closed by foreign host.
So what could possibly make a server behave differently for telnet than netcat?
I thought maybe it was a timing issue, so I unsuccessfully tried:
$ { sleep 4; echo foobar; sleep 4; } | nc whois.centralnic.net 43
# => No output from nc.
I saw that netcat has a -T option to make it behave more like telnet, so I unsuccessfully tried:
$ { sleep 4; echo foobar; sleep 4; } | nc -T whois.centralnic.net 43
# => No output from nc.
In my production system I will not be using netcat or telnet, but there seems to be some strange networking issue happening here and I would like to be aware of it. Can anyone shed some light on why netcat would work for all the whois servers but only telnet will work for whois.centralnic.net?
The service expects CRLF in its request, not just LF;
This works (on Ubuntu, there are multiple netcat versions, so can't speak for yours)
$ echo -e "foobar\r\n" | nc whois.centralnic.net 43
DOMAIN NOT FOUND