alternative for netcat utility

前端 未结 5 2397
庸人自扰
庸人自扰 2021-02-20 07:09

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

相关标签:
5条回答
  • 2021-02-20 07:28

    socat is a more powerful version of nc and netcat.

    0 讨论(0)
  • 2021-02-20 07:30

    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.

    0 讨论(0)
  • 2021-02-20 07:31

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

    perl -MLWP::Simple -e "getprint('http://localhost')"
    
    0 讨论(0)
  • 2021-02-20 07:46

    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')
    
    0 讨论(0)
  • 2021-02-20 07:50

    Python is ubiquitous these days and the socket module is all you need.

    Here are a few examples: You can use it to test connectivity on port 443 to a list of 3 hosts:

    import socket
    
    def test_socket(ip,port):
            s = socket.socket()
    
            try:
                s.settimeout(3)
                s.connect((ip,port))
            except socket.error as msg:
                s.close()
                print 'could not open %s:%s %s' % (ip,port,msg)
                return(1)
            else:
                s.close()
                print '%s:%s is OK' % (ip,port)
                return(0)
    
    
    hosts=['host1.example.com','host2.example.com','host3.example.com']
    
    for host in hosts:
       print "testing %s 443" % host
       test_socket(host,443)
    

    This one liner can read stdin or files and send to hostname termbin.com on port 9999 upload a file to termbin:

     python -c "import socket,fileinput;  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); s.connect(('termbin.com', 9999)) ; [ s.send(b) for b in fileinput.input() ]; print s.recv(1024); s.close();" filetoupload.txt
    
    0 讨论(0)
提交回复
热议问题