nc应用手册

2014-05-11 14:55  1056人阅读  评论 (0)

监听

nc -l 8888
nc -k -l 8888 # 持续监听

连接

nc 127.0.0.1 8888

检查端口

nc -v -w 5 127.0.0.1 8888

端口扫描

nc -v -w 2 -z 127.0.0.1 20-80 # -w 超时时间 -z 发送0值
nc -u -v -w 2 -z 127.0.0.1 20-80 # udp

文件传输

nc -l 8888 > data.gz
nc 127.0.0.1 8888 < data.gz

多个文件或大文件传输

nc -l 8888 | tar -C /www -xz
tar cz /www | nc 127.0.0.1 8888

加密传输

nc -l 8888 | openssl enc -aes-256-cbc -d -pass pass:dotcoo | tar -C /www -xz
tar cz /www | openssl enc -aes-256-cbc -pass pass:dotcoo | nc 127.0.0.1 8888

发送/接收

while true ; do date "+%Y-%m-%d %H:%M:%D"; done | nc 127.0.0.1 8888
nc -k -l 8888 | while read line ; do echo $line; done

请求/响应

mkfifo msg & tail -f msg | nc -k -l 8888 | while read req; do echo Request ":" $req ; echo world > msg; echo Response ":" world; done
echo hello | nc 127.0.0.1 8888

广播/订阅

# 广播 用golang 或 nodejs实现比较好
nc 127.0.0.1 8888 | while read line ; do echo $line; done # 订阅 

cat list.txt | while read host port ; do echo close | nc $host $port; done # 广播
nc -k -l 8888 | while read line ; do echo $line; done # 订阅

复杂远程调用

cat << EOF | nc 127.0.0.1 80
GET / HTTP/1.1
Host: 127.0.0.1

EOF