Skip to main content
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 ...
Kamil Maciorowski's user avatar
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 ...
D. SM's user avatar
  • 211
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.......
steeldriver's user avatar
  • 83.8k
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 ...
jdwolf's user avatar
  • 5,257
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 ...
Kusalananda's user avatar
  • 356k
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 &...
Astad's user avatar
  • 169
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" ...
Stéphane Chazelas's user avatar
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 -...
Mostafa Ghasemi's user avatar
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 && ...
Chris Davies's user avatar
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.
anonymous's user avatar
  • 253
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 ...
Martijn van Wezel's user avatar
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 ...
thomas's user avatar
  • 479
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 ...
Valentin Bajrami's user avatar
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 ...
Vlastimil Burián's user avatar
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 ...
Shritama Sengupta's user avatar
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 ...
Stephen Kitt's user avatar
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 ...
muru's user avatar
  • 77.9k
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 ...
Stephen Kitt's user avatar
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 ...
Stanley Yu's user avatar
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
Hauke Laging's user avatar
  • 94.5k
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 ...
Andy Dalton's user avatar
  • 14.7k
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
Ralph Rönnquist's user avatar
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 | ...
Dr.venom's user avatar
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 ...
DopeGhoti's user avatar
  • 79.2k
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.
Hermann's user avatar
  • 6,914
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.)
Hermann's user avatar
  • 6,914
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
RudiC's user avatar
  • 9,049
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
Ed Morton's user avatar
  • 35.8k
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 ...
Kusalananda's user avatar
  • 356k

Only top scored, non community-wiki answers of a minimum length are eligible