netcat实用程序的替代方案

时间:2015-09-29 08:03:28

标签: linux docker

netcat实用程序有什么替代方案吗? 我想运行docker API并且客户端系统上没有安装netcat实用程序。 docker命令示例 - echo -e“GET / info HTTP / 1.0 \ r \ n”| nc -U /var/run/docker.sock

5 个答案:

答案 0 :(得分:6)

socatncnetcat的更强大版本。

答案 1 :(得分:5)

根据this

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

可以替换nc / netcat。它应该适用于任何基于bash的终端。

示例:

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

返回link.

答案 2 :(得分:1)

你有Perl吗?你可以做这样的事情:

perl -MLWP::Simple -e "getprint('http://localhost')"

答案 3 :(得分:1)

Python现在无处不在,而套接字模块就是您所需要的。

以下是一些例子: 您可以使用它来测试端口443上的连接到3个主机的列表:

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)

这一个班轮可以读取标准输入或文件,并在端口9999上发送到主机名termbin.com上传文件到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

答案 4 :(得分:0)

遵循 Python 中的实现,以建立套接字连接并在 tcp 和 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')