nc命令:
Linux中nc命令是一个功能强大的网络工具,全称是netcat。
语法:nc 选项 参数
选项:
-g: <网关>:设置路由器跃程通信网关,最多设置8个
-G: <指向器数目>:设置来源路由指向器,其数值为4的倍数
-h:在线帮助
-i: <延迟秒数>:设置时间间隔,以便传送信息及扫描通信端口
-l:使用监听模式,监控传入的资料
-n:直接使用ip地址,而不通过域名服务器;
-o: <输出文件>:指定文件名称,把往来传输的数据以16进制字码倾倒成该文件保存
-p: <通信端口>:设置本地主机使用的通信端口
-r:指定源端口和目的端口都进行随机的选择
-s: <来源位址>:设置本地主机送出数据包的IP地址
-u:使用UDP传输协议
-v:显示指令执行过程
-w: <超时秒数>:设置等待连线的时间
-z:使用0输入/输出模式,只在扫描通信端口时使用
[root@salt-slave-node1 ~]# nc --help
-4 Use IPv4 only
-6 Use IPv6 only
-U, --unixsock Use Unix domain sockets only
-C, --crlf Use CRLF for EOL sequence
-c, --sh-exec <command> Executes the given command via /bin/sh
-e, --exec <command> Executes the given command
--lua-exec <filename> Executes the given Lua script
-g hop1[,hop2,...] Loose source routing hop points (8 max)
-G <n> Loose source routing hop pointer (4, 8, 12, ...)
-m, --max-conns <n> Maximum <n> simultaneous connections
-h, --help Display this help screen
-d, --delay <time> Wait between read/writes
-o, --output <filename> Dump session data to a file
-x, --hex-dump <filename> Dump session data as hex to a file
-i, --idle-timeout <time> Idle read/write timeout
-p, --source-port port Specify source port to use
-s, --source addr Specify source address to use (doesn't affect -l)
-l, --listen Bind and listen for incoming connections
-k, --keep-open Accept multiple connections in listen mode
-n, --nodns Do not resolve hostnames via DNS
-t, --telnet Answer Telnet negotiations
-u, --udp Use UDP instead of default TCP
--sctp Use SCTP instead of default TCP
-v, --verbose Set verbosity level (can be used several times)
-w, --wait <time> Connect timeout
-z Zero-I/O mode, report connection status only
参数:
主机:指定主机的IP地址或主机名称
端口号:可以是单个整数或者是一个范围
端口扫描
[root@salt-slave-node1 ~]# nc -z -v -w 2 39.108.217.12 22
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 39.108.217.12:22.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.
补充:http://man.linuxde.net/nc_netcat
实例一. 测试某主机的某端口是否可以正常连接
[root@iZwz9bhan5nqzh979qokrkZ ~]# nc -z -w 2 47.106.141.17 10050
[root@iZwz9bhan5nqzh979qokrkZ ~]# echo $?
0
[root@iZwz9bhan5nqzh979qokrkZ ~]# nc -z -w 2 47.106.141.17 4506
[root@iZwz9bhan5nqzh979qokrkZ ~]# echo $?
1
注:
返回为0 连接成功
返回为1 连接不成功
参数:
-z 表示检测或者扫描端口
-w 表示超时时间
-u 表示UDP协议
实例二. 文件传输
接收端
root@salt-slave-node1 ~]# nc -l 44 > a.txt
发送端
[root@iZwz9bhan5nqzh979qokrkZ ~]# nc 47.106.141.17 44 < a.txt (其中,这里的IP地址为接收端的IP地址)