Mac OS 推荐用 brew install netcat
装 GNU netcat,更为原生Linux体验
之后 vim ~/.zshrc
alias nc=‘netcat’
. ~/.zshrc
➜ ~ tldr nc
nc
Netcat is a versatile utility for working with TCP or UDP data.
-
监听端口 Listen on a specified port and print any data received:
nc -l port
例如nc -l 11111
终端开着
然后开另一个终端telnet localhost 11111
往里面输入,第一个终端就看得到了
同样第一个终端也可以输入,第二个终端能看到,双向的 -
连端口 Connect to a certain port (you can then write to this port):
nc ip_address port
类似 telent,不过telent 所有 Linux都会装,但是netcat不一定 。此外MacOS之后的版本telent也不会装。 -
设置超时时间 Set a timeout:
nc -w timeout_in_seconds ipaddress port
这个事一旦连接成功了,那么多时间不回话就会关掉 -
存文件 Serve a file:
nc -l port < file
开了之后,连接成功了才会传送 -
Receive a file:
nc ip_address port > file
和上面一个连起来用 -
Server stay up after client detach:
nc -k -l port
这个再结合 man 看一下
-k Forces nc to stay listening for another connection after its cur- rent connection is completed. It is an error to use this option without the -l option.
就是之前的一个 listen 了,另一个连上了,连上的那个断开了,第一个也结束了。但是加上了 -k,第二个关了,第一个还在
-
Client stay up after EOF:
nc -q timeout ip_address
没看到这个命令 -
Scan the open ports of a specified host:
nc -v -z ip_address port
man 一下
-v --verbose Prints status messages, usually needed for using netcat as user front-end. All messages are printed to stderr in order not to affect the data stream.
-v 都是详细信息
--zero Sets the zero I/O flag for the selected mode. In connect mode it means that as soon as the port is open it is immediately shutdown and closed. This may be useful for probing or scanning (even if there are faster portscanners out there, but this may be useful for scripting purposes). In listen mode, it makes netcat refusing all the incoming connections thus running in timeout (if set), or wait- ing forever. In both cases, no data is transfered. This option is incompatible with the tunnel mode.
设置一个 0 IO的标志,不同模式下有不同的用法
-
连接模式 连上了立刻关了,可以用来扫端口
-
监听模式 拒绝所有连接
-
tunnel 模式不兼容
-
Act as proxy and forward data from a local TCP port to the given remote host:
nc -l local_port | nc hostname remote_port
当代理
监听一个端口,再发给别人