How to reconnect automatically to a TCP Server? - bash

I'm having a TCP-Server which prints out some logging information. Normally I'm used to dump these logging to terminal by:
nc -v 192.168.0.42 7777
Or I dump the logging to a file using
nc -v 192.168.0.42 7777 >> log.log
Well, sometimes the server runs through a reboot. Thus the connection to the client is disconnected and the logging to the file log.log stops.
So I ask: How to reconnect automatically to the TCP Server?
I tried this:
while true; do nc -v 192.168.0.42 7777; done
But this does not work. If the server does its reboot nc does not notice that the connection has become inactive.
If you would like to reproduce this scenario just open a server-terminal with nc -l 7777 and then run the commands above within a second terminal. The server can then be terminated by ctrl-c.

Related

kill/close client connections to docker from host

I have a problem about connections to a docker containers from outside of my network. Iptables not worked yet for me (See this question).
The container open a connection on port 9010 which maps to its 443:
docker run -d [some other configs] --restart=always -p 9010:443 -p 9010:443/udp xxx/myImage
and I cannot see the client connections to this in my host:
root#ubuntu:~# netstat -anlp | grep ESTABLISHED
tcp 0 64 88.99.126.173:22 191.96.180.79:2543 ESTABLISHED 20855/sshd: root#pt
However I can see it inside container:
root#ubuntu:~# docker exec -it b7772c4d43dc /bin/bash -c 'netstat -anlp | grep ESTABLISHED'
tcp 0 0 ::ffff:172.17.0.2:443 ::ffff:191.96.180.79:3298 ESTABLISHED 7/python
Now I want to close this connection in that container, but since docker is from python:3.6-alpine image, there is not useful commands such as tcpkill command. How I can close this connections by bash inside my host.

How to check the message which server sends you after you connected to it via netcat (nc) or telnet?

For example, I am connecting to the server via netcat:
$ nc localhost 30002
When the connection is established, the host sends me the following message
$ nc localhost 30002
Hello! Please name yourself!
...and waiting for my response.
I need to write a Bash script which checks the message server sends me and then do something:
#!/bin/bash
nc localhost 30002
if [ "<the message which server sent me when I connected to it>" = "Hello! Please name yourself!" ]
then
...
fi

Auto SSH with OpenWRT Router to AWS EC2 Server

I've got a OpenWRT router that I'm trying to make a persistent reverse ssh tunnel to an Amazon AWS server. The issue is my ISP changes my public IP so in order to ssh to it, I have to use port knocking to prevent every IP from seeing the ssh port. I've written a bash script to make a connection. And no, I don't want to use autossh...it doesn't support input key path.
What works:
Anyway this script is located at /root/scripts/autosshtoaws.sh. I can run it fine from the terminal with the command ./root/scripts/autosshtoaws.sh. It goes in the background just fine. If I manually disconnect my internet line, sshd on the AWS server is configured to kill the socket AND my router is configured to kill the socket. It attempts like it's suppose to.
The problem:
I've got this running as an init.d service. It starts up well after network does. When I reboot the router and I look at the netstat command, it shows multiple attempts as if it is trying over and over and over.
Here is the script:
#!/bin/bash
PATH=/usr/sbin:/sbin:/usr/bin:/bin
ssh_command="/usr/bin/netcat -z MYMACHINE.amazonaws.com 777 611 501; sleep 2; /usr/bin/ssh -i /root/.ssh/aws_key.pem -R 8080:192.168.0.99:7070 ubuntu#MYMACHINE.amazonaws.com"
while true; do
if [[ -z $(ps | grep "$ssh_command" | sed '$ d') ]]
then eval $ssh_command
else sleep 60
fi
done
This is what the netstat command shows:
tcp 0 0 1.1.1.1:54926 2.2.2.2:22 ESTABLISHED
tcp 0 0 1.1.1.1:54312 2.2.2.2:22 TIME_WAIT
tcp 0 0 1.1.1.1:54760 2.2.2.2:22 TIME_WAIT
tcp 0 0 1.1.1.1:54700 2.2.2.2:22 TIME_WAIT
tcp 0 0 1.1.1.1:54636 2.2.2.2:22 TIME_WAIT
Here is the service configuration:
#!/bin/sh /etc/rc.common
USE_PROCD=1
START=95
STOP=01
start_service() {
procd_open_instance
procd_set_param command /bin/sh "/root/scripts/autostartsshtoaws.sh"
procd_close_instance
}

Why netcat port forwarding can just receive one time connection?

I have one http server open 8000 port like next:
orange#orange:~$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
As known to all, there are several versions netcat, but for some reasons, I can just use next versions:
root#orange:~# busybox nc
BusyBox v1.27.2 (Ubuntu 1:1.27.2-2ubuntu3.2) multi-call binary.
Usage: nc [-iN] [-wN] [-l] [-p PORT] [-f FILE|IPADDR PORT] [-e PROG]
Open a pipe to IP:PORT or FILE
-l Listen mode, for inbound connects
(use -ll with -e for persistent server)
-p PORT Local port
-w SEC Connect timeout
-i SEC Delay interval for lines sent
-f FILE Use file (ala /dev/ttyS0) instead of network
-e PROG Run PROG after connect
This means only above parameters should be used.
I did next:
root#orange:~# rm -f /tmp/backpipe && mkfifo /tmp/backpipe && cat /tmp/backpipe | busybox nc 127.0.0.1 8000 | busybox nc -l -p 80 > /tmp/backpipe
The aim is: when user visit http://127.0.0.1:80, it will automactically forward to http://127.0.0.1:8000, so the contents of python simplehttpserver will returned to user.
Finally, I launch test client:
orange#orange:~$ wget http://127.0.0.1
--2019-06-26 22:47:25-- http://127.0.0.1/
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1378 (1.3K) [text/html]
Saving to: ‘index.html’
index.html 100%
[==============================================>] 1.35K --.-KB/s in 0s
2019-06-26 22:47:25 (505 MB/s) - ‘index.html’ saved [1378/1378]
Above all ok, but back to the port forward command, I found it had been exit, so it nolonger receive the second time connect.
So, my question is, with above busybox netcat, how can I make this port forward command not exit after the first connection.
NOTE: I don't want the solution with for-loop, I just want to find the way to do port forward with above netcat, meanwhile it will continue serve after the first connection.
You may have been using an older version of busybox. Current versions come with the -lk flag, which allows you to persist the server beyond just a single connection.
In order to accomplish what you wanted, you can do something like this:
busybox nc -lk -p 80 -e busybox nc localhost 8000

vi over netcat session

Is it possible to use vi over netcat?
server:
mkfifo tun; sh tun | netcat -l 4444 > tun
client:
netcat SERVER_IP 4444
Will gave me remote shell, but it's a problem to send special hot-keys, for example I can't push ESC to enter "normal mode" in vi.
Or best choice will be sed ?
This command runs input from nc as a script, and it will fail for the same reason why this script won't edit a file:
#!/bin/sh
vi file
42G
dd
:wq
You can instead, ironically, use script to avoid running it as a script, and instead get a terminal session to interact with:
server$ mkfifo tun; script -q < tun | netcat -l 4444 > tun
(some netcats require -p before the port above)
Additionally, you should disable local echo and line buffering so that keys pass through the connection immediately rather than when pressing enter:
client$ stty -icanon -echo; nc localhost 4444
You should now be able to edit files in vi.
This is obviously a neat proof of concept only. Non-root users who want to provide robust shell access over the network should use sshd.

Resources