Best way to monitor for a server on a TCP port - linux

I have a remote Music Player Daemon(MPD) server running on a linux machine. I have a client listening to this stream on another linux machine.
When the MPD server is asked to pause or stop the stream, it disconnects all clients connected on the TCP port. Consequently, when the server starts streaming again, the clients have to be manually reconnected.
I want to write a program that will monitor the TCP port for a server accepting connections, and then automatically restart the clients. Can I do better than running connect() and sleep() in a loop? Are there any command-line utilities to do this?
I can run the client on the machine running the MPD server, if it will help. The following will tell me if a process is listening on a local port, but they do not block if a process isn't, so I still need to wrap them in a loop.
$ sudo fuser -n tcp 8000
8000/tcp: 9677
$ sudo netstat -nlp | grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 9677/mpd
I can try any solution that does not involve changing the behaviour of the MPD server.

There is always the possibility of writing a relay server that proxies for MPD.
It sits there listening on a different port for your clients and makes connections to MPD in their stead. When MPD disconnects, the relay just attempts to reconnect every few seconds without disconnecting its clients.

Here you go:
echo -n "" | nc -q 0 localhost 8000 && echo "made a connection" || echo "server was down"
echo -n "" puts an EOF immediately on stdin; nc -q 0 returns immediately after seeing that EOF on stdin. nc (netcat) tries to make a connection to localhost on port 8000. If it connects successfully, then it returns a successful error code and we echo "made a connection"; otherwise, if the connection was refused, we echo "server was down".
If you want to test it out, then in another terminal run
nc -lvvp 8000
which will start an instance of netcat listening on port 8000, with verbose output. In your other terminal, run the first command. The first time you run it, it will say made a connection. Then the server/listener will close, so the next time you run it, it will say server was down.

Related

Error: listen EADDRINUSE :::9000, killall && kill -9 are not working [duplicate]

After starting an HTTP server process a couple of times, I get this error like an instance of Go has not stopped!?
listen tcp :9000: bind: address already in use
I have experienced something like this with Node.js too, but I was able to kill the process.. Unfortunately it seems like I can't find the process id and kill it..
How can I "free" the TCP port?
If you are on Unix-like system, you can use netstat to find out which process is listening on a port:
sudo netstat -nlp | grep 9000
It turns out the -p option is not available on OS X. If you are using OS X, you can do this:
lsof -n -i4TCP:$PORT | grep LISTEN
Who is listening on a given TCP port on Mac OS X?

How can I automate sending and receiving TCP traffic using netcat in Bash?

I'm trying to create some test network traffic by sending TCP traffic using netcat.
The issue is that the netcat command to receive TCP data doesn't end on its own, which makes perfect sense.
I'm wondering how I can have a script that continues running to the send portion of the TCP traffic. Something similar to:
#!/bin/bash
#Prepare to receive traffic
nc -vv -l -p 5000 > receiveData.txt
#Send traffic
nc -nvv localhost 5000 < sendData.txt
Obviously the script will never get to the #Send traffic line because the receive traffic never ends. I've been looking at methods for running functions in bash asynchronously, but nothing I've read seems to solve this problem.
How about just running receive traffic process in the background so that the other commands can be executed to?
# Prepare to receive traffic
# Running this process in the background so that the sending TCP traffic
# happens
nc -vv -l -p 5000 > receiveData.txt &
#Send traffic
nc -nvv localhost 5000 < sendData.txt

How can I find a process using a TCP port?

After starting an HTTP server process a couple of times, I get this error like an instance of Go has not stopped!?
listen tcp :9000: bind: address already in use
I have experienced something like this with Node.js too, but I was able to kill the process.. Unfortunately it seems like I can't find the process id and kill it..
How can I "free" the TCP port?
If you are on Unix-like system, you can use netstat to find out which process is listening on a port:
sudo netstat -nlp | grep 9000
It turns out the -p option is not available on OS X. If you are using OS X, you can do this:
lsof -n -i4TCP:$PORT | grep LISTEN
Who is listening on a given TCP port on Mac OS X?

Linux; How do I find logs if a program I'm running uses certain ports?

I am running CentOS 5 with csf firewall. I'm running a program that can't connect to another server (using some port that is blocked by csf I presume). Where is the log file for 'ports'?
Netstat is the command to use to get ports and network activity. To diagonise server processes I usually use:
netstat -tln
This yields port numbers in tcp mode listening. To identify associated processes you can also use -p to grab the pid. Here is the IANA ports list.
I found my answer right after searching a few more threads.
# tail -f /var/log/messages
Shows the UDP message but not the port.... Hmm....

Redirecting TCP-traffic to a UNIX domain socket under Linux

Assume a legacy Linux application listening on a UNIX domain socket /tmp/foo.
In addition to communicating with this legacy application over the UNIX domain socket mechanism I want to be able to connect to it via a TCP-connection on port say 1234.
What is the easiest way to bind to TCP port 1234 and then redirect all incoming connections to the UNIX domain socket /tmp/foo?
Turns out socat can be used to achieve this:
socat TCP-LISTEN:1234,reuseaddr,fork UNIX-CLIENT:/tmp/foo
And with a bit of added security:
socat TCP-LISTEN:1234,bind=127.0.0.1,reuseaddr,fork,su=nobody,range=127.0.0.0/8 UNIX-CLIENT:/tmp/foo
These examples have been tested and work as expected.
Easiest? Probably Netcat (aka nc):
nc -l 1234 | nc -U /tmp/foo
The first command listens on port 1234 for incoming connections, and pipes the resulting data to the second command. The second connects to the Unix domain socket /tmp/foo, and writes its input to that socket. Note that this will only accept a single connection, and exit as soon as that connection is dropped. If you want to keep listening for more connections, use the -k option:
nc -lk 1234 | nc -U /tmp/foo
You can test that this is working by setting up a listener for that socket in one terminal:
nc -lUk /tmp/foo
And writing to it in another:
nc localhost 1234
socat, as recommended by knorv, is more capable, but more complicated to use.
You should be able to bind to TCP 1234, get a socket fd for /tmp/foo and use the select call to 'listen' for data on both 1234, and /tmp/foo. Any data written to 1234, you rewrite to /tmp/foo and vice-versa.
You now act as a proxy and transfer data back and forth.
And here is a web-page which might help: http://osr507doc.sco.com/en/netguide/dusockC.io_multiplexing.html
In additons to #knorv's answer: with xinetd it can work like a daemon
# cat /etc/xined.d/mysrv
service mysrv
{
disable = no
type = UNLISTED
socket_type = stream
protocol = tcp
wait = no
server = /usr/bin/socat
server_args = STDIN UNIX-CLIENT:/tmp/mysocket.sock
bind = 127.0.0.1
port = 1234
}
Not tried it : but it looks like 'lighttpd' can do this for you:
http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModProxyCore

Resources