Since your server is connecting to a certain IP, I presume it will be to a port that you also have knowledge of?
In any case, netstat
or ss
are designed to do what you want. You can do the same with either command:
netstat -n -t | awk '{print $5}' | grep A.B.C.D:n
ss -n -t | awk '{print $5}' | grep A.B.C.D:n
where A.B.C.D
represents an IPv4 address, and n
represents a port number that your server is connecting to on the remote side. For example:
ss -n -t | awk '{print $5}' | grep 10.137.54.22:3389
Or, if you'd just like to know that the connection is made:
ss -n -t | awk '{print $5}' | grep -q 10.137.54.22:3389 && echo "CONNECTION MADE"
If you don't know the port number that you are attempting to connect to, then the job would be more difficult as TCP will open a port on both sides of the conversation for the data and ACK packets. In that case you could simply grep for the IP address to show that any connection has been made, whether that's to or from.
Finally, you can loop this to your heart's content to use as your monitoring tool:
while true; do
ss -n -t | awk '{print $5}' | grep -q A.B.C.D:n && \
echo "CONNECTION MADE" && \
exit 0
sleep 1
done