Hot answers tagged

24

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


20

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 | ...


4

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 ...


4

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.


3

On the receiving side: nc -l -p 5959 | ( cd /media/data; cpio -idmv ) On the sending side: find /some/dir -type f -print | cpio -oa | nc machine 5959 However, you really should consider using scp instead: cd /some/dir scp -r . user@machine:/media/data


3

I assume you are using netcat-openbsd because you specify -4. It has -b to enable broadcast address, but it's known that UDP broadcast is not supported by this version of netcat even with -b. Debian Bug#702204 suggests a patch to fix that. You can install an alternative package netcat-traditional which seems to correctly support -b with UDP. Note that ...


3

The command you are running (nc a.k.a. netcat) will listen for input when run with the -l flag. Normally, netcat in listen mode will close when it receives the end-of-file character, but the -k flag prevents that. In other words, netcat won't close until you kill it because of the way you invoked the command. See the man page for more info.


3

You could use the read command (bash builtin) to force characters to be read one by one : netcat localhost 9090 | ( cnt=0 line= while read -N 1 c; do line="$line$c" if [ "$c" = "{" ]; then cnt=$((cnt+1)) elif [ "$c" = "}" ]; then cnt=$((cnt-1)) if [ $cnt -eq 0 ]; then ...


3

It's in the manpage, under the section for ProxyCommand: Specifies the command to use to connect to the server. The command string extends to the end of the line, and is executed with the user's shell. In the command string, any occurrence of ‘%h’ will be substituted by the host name to connect, ‘%p’ by the port, and ‘%r’ by the remote user name. ...



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