#!/usr/bin/perl -w
use strict;
use IO::Select;
use IO::Socket;
my ($data, $fh);
my $s = IO::Select->new();
my $l = new IO::Socket::INET(Listen => 5, LocalAddr => 'localhost', LocalPort => 6089, Proto => "tcp");
$s->add(\*STDIN);
print "listening...\n";
my $incoming = $l->accept;
$s->add($incoming);
print "incoming connection...\n";
while (1) {
if (my @ready = $s->can_read(.01)) {
foreach $fh (@ready) {
if ($fh == \*STDIN) {
my $data=<STDIN>;
$incoming->send($data);
} else {
$fh->recv($data, 1024);
if($data eq "") {
print "connection closed\n";
$s->remove($fh);
$fh->close;
exit;
} else {
print "$data";
}
}
}
}
}
相关:用于测试docker/docker-compose端口Map
# docker-compose.yml : dummy port-test container
version: "3.9"
services:
test:
container_name:test
image: debian
#network_mode: host
ports:
- 6000:6000
#extra_hosts:
#- "host.docker.internal:host-gateway"
docker-compose run --service-ports -it test bash
docker run -p 6000:6000 -it debian bash : run debian directly with port-mapping
docker exec -it CONTAINER_ID bash : open a second command prompt
docker ps -a : shows mapped ports
To test port mapping: put a nc-listen receiver inside the container and run a
nc-connect from the host-os (`echo "test" >/dev/tcp/localhost/1234`).
# check port mapping
sudo iptables -L -v -n | less
# docker-proxy is run once for each host-mapped-port-direction
ps auwx | grep docker-proxy
network_mode:host puts container ports right on the host and does not map them
extra_hosts makes host.docker.internal work in linux
6条答案
按热度按时间r7xajy2e1#
根据这个
可以替代nc/netcat。它应该在任何基于bash的终端中工作。
示例:
printf "Hello World!" | (exec 3<>/dev/tcp/termbin.com/9999; cat >&3; cat <&3; exec 3<&-)
返回link.
9w11ddsr2#
socat
是nc
和netcat
的更强大版本。flseospp3#
你有Perl吗?你可以这样做:
brc7rcf04#
按照Python中的实现进行套接字连接并在tcp和udp中发送数据:
sirbozc55#
Python现在无处不在,socket模块就是你所需要的一切。
以下是几个例子:您可以使用它来测试端口443与3台主机的连接:
这一个liner可以读取stdin或文件,并在端口9999上发送到主机名termbin.com,上传文件到termbin:
8cdiaqws6#
一些bash和perl方法:
任何类型的监听操作都需要bind+accept调用,这在bash中无法使用
/dev/tcp
完成。任何类型的双向IO都需要某种非阻塞IO方法,如select
。可读的完整版本的“perl nc-listen(2-way)”(可根据需要进行改编)
相关:用于测试docker/docker-compose端口Map