Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alternative for netcat utility

Tags:

linux

docker

Is there any alternative for netcat utility? I want to run docker API and netcat utility is not installed on client system. docker command example - echo -e "GET /info HTTP/1.0\r\n" | nc -U /var/run/docker.sock

like image 958
Rakesh Avatar asked Sep 29 '15 08:09

Rakesh


4 Answers

socat is a more powerful version of nc and netcat.

like image 148
Maxim Egorushkin Avatar answered Oct 05 '22 15:10

Maxim Egorushkin


According to this,

(exec 3<>/dev/tcp/url/port; cat >&3; cat <&3; exec 3<&-)

can replace nc/netcat. It should work in any bash based terminal.

Example:

printf "Hello World!" | (exec 3<>/dev/tcp/termbin.com/9999; cat >&3; cat <&3; exec 3<&-)

returns a link.

like image 32
Oliver Avatar answered Oct 05 '22 16:10

Oliver


Have you got Perl? You could do something like this maybe:

perl -MLWP::Simple -e "getprint('http://localhost')"
like image 45
Mark Setchell Avatar answered Oct 05 '22 15:10

Mark Setchell


Follow a implementation in Python for making sockets connections and sending data in tcp and udp:

import socket

def netcat(hostname, port, content=None, protocol='tcp'):
    print('')
    if protocol == 'tcp':
        s = socket.socket() # (socket.AF_INET, socket.SOCK_STREAM)
    if protocol == 'udp':
        s = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
    if protocol != 'tcp' and protocol != 'udp':
        print("Error: Protocol must be 'tcp' or 'udp'")
    try:
        s.connect((hostname, port))
        print('Connection Success to ' + hostname + ':' + str(port) + '/' + protocol)
    except:
        print('Connection failed to ' + hostname + ':' + str(port) + '/' + protocol)
        return None
    if content != None:
        print('Starting to send content: ' + str(content))
        s.send(str.encode(content))
        # s.sendall(content)
        hasHacievedAnyData = False
        while True:
            s.settimeout(10)
            try:
                data = s.recv(1024)
            except Exception:
                if hasHacievedAnyData:
                    print('Info: Timeout while expecting to receve more data')
                else:
                    print('Error: Timeout while expecting to receve data')
                break
            if len(data) == 0:
                break
            print('Received:' + str(repr(data)))
            hasHacievedAnyData = True
        s.shutdown(socket.SHUT_WR)
        s.close()
        print('Connection closed.')

#Examples of usage
netcat('localhost', 443)
netcat('localhost', 3478)
netcat('localhost', 3478, protocol='udp')
netcat('localhost', 16384, 'Hello', 'udp')
like image 30
lucasarr Avatar answered Oct 05 '22 16:10

lucasarr