17
votes
Accepted
Odd inconsistency between executing and sourcing Bash script
I have tested your script in Debian 12 (localhost to localhost, separate working directories) and I confirm the problem. My nc is from netcat-traditional 1.10-47 (i.e. not from netcat-openbsd).
The ...
13
votes
Accepted
What is `nc -z` used for?
It can be more useful to think of -z option as meaning "immediately close the connection". My version of nc has this to say about port scanning:
PORT SCANNING
It may be useful to know which ports ...
8
votes
Accepted
convert one line values to multiple lines with numbering order
With awk:
$ servers='server1,server2,server3,server4,server5'
$ awk -v RS=, '{print NR "........" $0}' <<<"$servers"
1........server1
2........server2
3........server3
4........server4
5.......
7
votes
Accepted
How does this command work? mkfifo /tmp/f; cat /tmp/f | /bin/sh -i 2>&1 | nc -l 1234 > /tmp/f
Such a command is taking advantage of IO redirection and and sh interactive mode which is on by default when attached to a TTY.
Note that cat stays open on a FIFO. Thats your first clue. When sh runs ...
7
votes
Conditional `if` with command that doesn't respond in else
You seem to only run the if statement if nc completes successfully:
nc ... && if ...
This explains why 0 is never outputted. Since the if statement is not executed when nc fails, the else ...
6
votes
Using netcat for port forwarding
I had a similar use case. Mine was to listen to an external port and forward it to an internal port which pointed at a reverse ssh tunnel.
External port: 8001
Internal port: 18820
nc -l -k -p 8001 -c &...
5
votes
'Reversed shell' - pull shell commands from remote host with human-controlled input?
If you run:
socat "unix-listen:$HOME/.shell-access,mode=600,fork" \
"exec:$SHELL,pty,stderr,setsid,ctty"
That allows you to do for instance:
socat -,raw,echo=0 "unix:$HOME/.shell-access"
...
5
votes
Using netcat for port forwarding
For port forwarding only by net cat you can use pipes:
mkfifo pip
nc -l -p port_to_listen < pip | nc target_ip port_to_be_forwarded > pip
Or:
nc target_ip port_to_be_forwarded < pip | nc -l -...
5
votes
Accepted
Conditional `if` with command that doesn't respond in else
The && only applies the second part if the first part is true. So what you have is this
nc succeeds, so run the if it suceeded (which it did) echo 1
nc fails, so don't go past the &&
...
4
votes
Accepted
Is nc (netcat) on MacOS missing the "-e" flag?
You don't have to use nc -l 1337 -e /bin/bash. Instead, an alternative that works exactly the same is nc -l 1337 | /bin/bash outputs everything it receives into /bin/bash.
4
votes
Brute-force 4 digit pin with pass using shell script
Here a working answere that is fast and working:
#!/bin/bash
passwd24=UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ
for i in {0000..9999}; do
echo "$passwd24 $i"
done | nc localhost 30002 | grep -v ...
4
votes
ncat command gives Idle timeout expired on using option "-i"
The problem most surely is the -i option. At least three different versions of nc do exist. openbsd-netcat, gnu-netcat and nmap-ncat.
You probably switched from one of the nc versions where -i option ...
4
votes
Shell Script in netcat listener to talk with client
Ok, so here I've set up a small example:
#!/bin/bash
while read -p 'Tell me your name: ' ln;
do
echo "I got $ln";
done
So you save the script make it executable and run it as follows:
On ...
4
votes
Accepted
Failing IF in a WHILE loop in BASH script that checks for open 22 ports
Notes, too long for a comment.
server.txt could better be named server-list or servers.txt if you wish.
Use lower case variable names like, avoid SERVER and alike, could better be named server_ip ...
4
votes
Shell Script in netcat listener to talk with client
It seems Open-BSD netcat does not support -e or --exec commands which helps us to execute a file after a connection has been made. Since it is might cause the remote machine to run potentially harmful ...
4
votes
Accepted
What is this code actually doing?
This sets up a remotely-controlled shell on the system. The inputs and outputs are as follows:
cat /tmp/m reads from the /tmp/m FIFO and writes it to ...
|/bin/sh -i 2>&1 which executes the ...
4
votes
Accepted
How to understand the action of nc -l
Being the target of an incoming connection doesn't prevent netcat from sending data. Once a client has connected, it can both send data to and receive data from the client. In this case, it's sending ...
4
votes
Accepted
How to use nc to send message consecutively per second
Pipe the whole loop into nc:
while true; do
echo 'lol'
sleep 1
done | nc -l 9000
This will start a single instance of nc, listening for connections on port 9000, and send “lol” once per ...
4
votes
Accepted
How to compare output of a program with a reference value in a shell script?
Your comparison test is not working as expected because of two things:
The actual response from the server (as I understand from your original post and comments) is the string +PONG followed by a ...
3
votes
Failing IF in a WHILE loop in BASH script that checks for open 22 ports
The most simple solution seems to be:
if nc -z -v -w5 $SERVER 22 >/dev/null 2>&1;
then
echo "Found SSH open on $SERVER"
else
echo "No open ports on $SERVER!"
fi
3
votes
Accepted
nc getting stuck unexpectedly
I'm using netcat and not gnu-netcat; I'm not sure what version you're using, but if it's gnu-netcat the options might be different.
I have a -q option:
-q seconds after EOF is detected, wait the ...
3
votes
nc to retry on connection refuse
If you are using bash or similar shell, you could do something like the following;
cat message | while ! nc 192.168.1.1 1234 ; do sleep 1 ; done
3
votes
Brute-force 4 digit pin with pass using shell script
you can pipe the loop into the netcat connection and write the output into a file as following:
#!/bin/bash
touch f.txt
for i in {0000..9999}
do
echo "UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ $i"
done | ...
3
votes
Accepted
Brute-force 4 digit pin with pass using shell script
That's because you're not telling your script to write anything to nc's standard input. Your script starts netcat, waits for it to terminate, and then sleeps for two seconds before executing the for ...
3
votes
Accepted
How to send new lines from file to a tcp port?
I do not know which version of netcat you are using, but mine does not have a -c parameter. However, tail -F /var/log/changes.log | nc 127.0.0.1 1234 works for me.
3
votes
Accepted
netcat with ssh
@muru's comment is very valid, but for the sake of academical purpose:
Computer A:
cat source | ssh user@ComputerB 'cat > destination'
(Assumes password-less authentication by public key.)
3
votes
convert one line values to multiple lines with numbering order
cat -n <<< ${SERVERS//,/$'\n'}
1 server1
2 server2
3 server3
4 server4
5 server5
3
votes
convert one line values to multiple lines with numbering order
$ awk -F',' '{for (i=1; i<=NF; i++) printf "%-2d ........ %s\n", i, $i}' <<<"$servers"
1 ........ server1
2 ........ server2
3 ........ server3
4 ........ server4
5 ........ server5
3
votes
How to understand the action of nc -l
nc -l 9000 in the example acts like a server. The Flink application connects to this server and waits for input. Note that Flink is started after running the nc command.
Now they are connected. You ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
nc × 88netcat × 30
networking × 15
bash × 14
shell-script × 10
shell × 8
linux × 6
tcp × 6
pipe × 5
telnet × 5
scripting × 4
macos × 4
socat × 4
unix-sockets × 4
daemon × 3
socket × 3
http × 3
udp × 3
centos × 2
ssh × 2
awk × 2
sed × 2
io-redirection × 2
php × 2
encryption × 2