linux nc - arbitrary TCP and UDP connections and listens


【基本紹介】
ncは任意のポートを開くことができるTCP/UDP接続と傍受である.
The nc (or netcat) utility is used for just about anything under the sun involving TCP or UDP.  It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6.  Unlike telnet(1), nc scripts nicely, and separates error messages onto standard error instead of sending them to standard output, as telnet(1) does with some.
【基本パラメータ】
パラメータ:
-g<ゲートウェイ>ルータ遷移通信ゲートウェイを設定し、最もなくしたものは8つ設定できます.
-G<ポインタ数>ソースルーティングポインタを4の倍数で設定します.
-hオンラインヘルプ.
-i<遅延秒数>情報の送信および通信ポートのスキャンのための時間間隔を設定します.
-l傍受モードを使用して、入力された資料を管理する.
−nは、ドメイン名サーバを介さずにIPアドレスを直接使用する.
-o<出力ファイル>ファイル名を指定し、転送されたデータを16進数のワードコードでこのファイルに倒して保存します.
-p<通信ポート>ローカルホストで使用する通信ポートを設定します.
-r乱数は、ローカルホストとリモートホストの通信ポートを指定します.
-s<ソースアドレス>ローカルホストがパケットを送出するIPアドレスを設定します.
−uはUDPトランスポートプロトコルを使用する.
-v命令実行手順を表示します.
-w<タイムアウト秒数>接続待ち時間を設定します.
-zは0入出力モードを使用し、通信ポートのスキャン時のみ使用します.
【簡易例】
コマンド/ファイルの転送

[root@sparkVM tmux]# nc -l 1000
hello ni

[root@pandaVM ~]# telnet 192.168.197.128 1000
Trying 192.168.197.128...
Connected to 192.168.197.128.
Escape character is '^]'.
hello ni^H^H

[root@sparkVM test]# cat testfile
cat: testfile: No such file or directory
[root@sparkVM test]# nc -l 1000 > testfile
[root@sparkVM test]# cat testfile 
insert into pressent values(1,'na');
insert into pressent values(2,'na');

[root@pandaVM ~]# cat sql 
insert into pressent values(1,'na');
insert into pressent values(2,'na');
[root@pandaVM ~]# cat sql | nc 192.168.197.128 1000

memcached,redisなどと交換が必要なプログラムとの通信を実現する

[root@pandaVM ~]# printf "set chen 0 0 5\r
61123\r
" | nc 127.0.0.1 11211 STORED [root@pandaVM ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. get chen END get chen END get chen END get chen VALUE chen 0 5 61123 END

スキャンポート

[root@sparkVM test]# nc -z -w2 127.0.0.1 1-30    
Connection to 127.0.0.1 22 port [tcp/ssh] succeeded!

【参考引用】
http://www.thegeekstuff.com/2012/04/nc-command-examples/
http://blog.zyan.cc/post/384/
http://bbs.chinaunix.net/thread-3654112-1-1.html