Hot answers tagged

22

I used the -r and -p switch to xxd: $ echo '0006303030304e43' | xxd -r -p | nc -l localhost 8181 Thanks to inspiration from @Gilles answer, here's a perl version: $ echo '0006303030304e43' | perl -e 'print pack "H*", <STDIN>' | nc -l localhost 8181


19

Unfortunately it's impossible to do with just bash. /dev/tcp/<ip>/<port> virtual files are implemented in the way that bash tries to connect to the specified <ip>:<port> using connect(2) function. In order to create listening socket, it would have to call bind(2) function. You can check this by downloading bash sources and looking at ...


15

You're complicating your life needlessly. Use scp. To transfer a file myfile from your local directory to directory /foo/bar on machine otherhost as user user, here's the syntax: scp myfile user@otherhost:/foo/bar. EDIT: It is worth noting that transfer via scp/SSH is encrypted while transfer via netcat or HTTP isn't. So if you are transferring sensitive ...


12

As perl will be installed. perl -MIO::Socket::INET -ne 'BEGIN{$l=IO::Socket::INET->new( LocalPort=>1234,Proto=>"tcp",Listen=>5,ReuseAddr=>1); $l=$l->accept}print $l $_' < ~/.bashrc would work, unless a local firewall doesn't allow incoming connections to 1234. If socat is installed: socat -u - tcp-listen:1234,reuseaddr < ...


11

Here a solution without xxd or perl: If the echo builtin of your shell supports it (bash and zsh do, but not dash), you just need to use the right backslash escapes: echo -ne '\x00\x06\x30\x30\x30\x30\x4e\x43' | nc -l localhost 8181 If you have /bin/echo from GNU coreutils (nearly standard on Linux systems) or from busybox you can use it, too. With sed ...


8

Bash provides pseudo devices that you're likely familiar with such as /dev/null. However there are other devices such as /dev/tcp and /dev/udp for testing network connections, which you may use from within Bash scripts too. excerpt from Bash's man page Bash handles several filenames specially when they are used in redirections, as described in the ...


7

you want tcpdump not nc, and the syntax would be tcpdump -i eth0 netcat is only for basic TCP/IP testing. tcpdump utilizes the libpcap library which allows for lowlevel interactions with packets and the likes


7

Background When you're attempting to use nc in this manner it's continuing to keep the TCP port open, waiting for the destination to acknowledge the receiving of the done request. This is highlighted in the TCP article on Wikipedia. TIME-WAIT (either server or client) represents waiting for enough time to pass to be sure the remote TCP received the ...


7

If you have xxd, that's easy: it can convert to and from hexadecimal. echo '0006303030304e43' | xxd -r -p | nc -l localhost 8181 I don't think there's a reasonable (and reasonably fast) way to convert hexadecimal to binary using only POSIX tools. It can be done fairly easy in Perl. The following script converts hexadecimal to binary, ignoring any input ...


7

nc writes its output to standard error, you need: nc -zvv localhost 31000-32000 2>&1 | grep succeeded The 2>&1 will redirect standard error to standard output so you can then pipe it to grep.


7

Judging by the specific output Connection to Connection to 10.1.0.100 53 port [udp/domain] succeeded! you are using openbsd-netcat. Looking at the code for that the test is to bind to the UDP socket, i.e. there is an open connection: if (vflag || zflag) { /* For UDP, make sure we are connected. */ ...


6

It's not that much that there's not output as it's coming in chunks. Like many programs, when its output is no longer a terminal, cut buffers its output. That is, it only writes data when it has accumulated a buffer-full of it. Typically, something like 4 or 8 kiB though YMMV. You can easily verify it by comparing: (echo foo; sleep 1; echo bar) | cut -c2- ...


6

You could use something like this: while true; do nc -lvp 1337 -c "echo -n 'Your IP is: '; grep connect my.ip | cut -d'[' -f 3 | cut -d']' -f 1" 2> my.ip; done nc will be executed in endless loop listening on port 1337 with verbose option that will write information about remote host to stderr. stderr is redirected to file my.ip. Option -c for nc ...


6

pv, which is available as a NetBSD package, lets you limit the rate of a pipe. <large_file pv -L 1k | netcat …


6

Netcat is not a specialized HTTP client. Connecting through a proxy server for Netcat thus means creating a TCP connection through the server, which is why it expects a SOCKS or HTTPS proxy with the -x argument, specified by -X: -X proxy_protocol Requests that nc should use the specified protocol when talking to the proxy server. ...


6

If you're happy with netcat you can work around the file name issue by intruducing tar. This also simplifies sending multiple files at once as well as sending directories. On the sending side use: tar cf - <files> | nc <host> <port> And on the receiving side: nc -l <port> | tar x Another solution would be to use rsync or scp.


5

When connecting to an IPv6 link-local address you need to specify through which link to reach it, as seen from the client system. The same link-local addresses are used on every link, so the client needs to be told explicitly which link (interface) to use. So if the system you want to reach is connected through eth0 of the client system, you need to connect ...


5

The program on the left side of a pipe does not receive an EOF (which is not a signal) when the right side of the pipeline ends. It gets a SIGPIPE which tells it to terminate. The problem is that you are not reading anything from the pipeline or reading any data from the pipeline. You probably want a while read loop which will read from stdin as long as it's ...


5

There is an ICMP message to signalize that a port, even an UDP one, is closed. So if a host sends this message then the port can be assumed to be closed. https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol#Destination_unreachable


4

gawk -v 'RS=\03' -v cmd=' socat -u - udp-datagram:239.255.0.1:30001,bind=127.0.0.1' ' {print $0 RT| cmd; close(cmd)}' < file should work as long as there's not more than 8k in between two ^Cs. That runs one socat command per record (records being ^C delimited via the record separator variable), with the record plus the record terminator fed to ...


4

Your original script requires that the connection comes from a host named localhost, but for some reason that filtering is failing. Unusual, because it matches exactly the name listed in the error: invalid connection to [127.0.0.1] from localhost [127.0.0.1] 60038 This command will listen on the localhost network interface (and will ignore requests from ...


4

With: socat tcp-listen:12345,reuseaddr,fork,bind=127.1 socks:218.62.97.105:11.11.11.11:3128,socksport=1080 you will have a socat waiting for TCP connections on port 12345 on the loopback interface, and forward them to 11.11.11.11:3128 by way of the socks server on 218.62.97.105:1080 You can then use that to connect to D: ssh -o ProxyCommand='socat - ...


4

Just put the redirection outside the loop. while true; do read_folder process_data sleep 10 done | netcat $ip $port If you need to output to the loop's standard output from inside the loop, divert it through another file descriptor. { while true; do read_folder echo tick >&3 process_data sleep 10 done | ...


3

I think I have an answer: when nc tries an IP address that has a server listening on port 22 (which is typically SSH server), it reads the rest of the input and passes it to the server on port 22. The SSH server I have running on my home machines just eats the input. The nc I have (Slackware 13.1 system) has a "-z" option, for "zero I/O". Try nc -z -w 3 ...


3

The problem is that you're using shell redirects to read from and write to the same file. Check p afterwards, it will be empty as well. The shell opens it in read mode, truncating the file, while it's setting up the pipeline before it runs the commands. However, using tee, since it opens the file itself, means that the file isn't truncated until after the ...


3

cat out | netcat foo.bar.org 4000 > in & I think the problem is that cat will exit as soon as it receives an EOF from the out pipe. And when cat exits, the rest of the pipeline (including netcat) gets terminated as well. Try something like this instead: while true; do cat out; done | netcat foo.bar.org 4000 > in & Thus, cat gets restarted ...


3

Can you try doing the second step without doing the nc? That is - do the VNC with just the -L and -R. I believe the issue is that your netcat session is connecting back to an already open. So when doing the VNC stuff don't use netcat.


3

Backgrounded commands that are executed in non-interactive shells with job control disabled (as it is the case in shell scripts) get their stdin implicitly redirected from /dev/null. sh -c 'nc -l 12345 1>nc_out & lsof -p $!' From POSIX.1-2008: 2. Shell Command Language [...] Asynchronous Lists If a command is terminated by the ...


3

You're on the right track using nc, but if you really want to just test whether you can establish the connection, use nc's -z switch: #!/bin/bash REMOTEHOST=10.11.12.13 REMOTEPORT=1234 TIMEOUT=1 if nc -w $TIMEOUT -z $REMOTEHOST $REMOTEPORT; then echo "I was able to connect to ${REMOTEHOST}:${REMOTEPORT}" else echo "Connection to ...


3

You can also try python -m SimpleHTTPServer 8180 It will serve the files in directory in which it executed over HTTP, you can access it via Browser.



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