欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

[20190211]简单测试端口是否打开.txt

程序员文章站 2022-05-02 12:55:26
[20190211]简单测试端口是否打开.txt--//昨天看一个链接,提到如果判断一个端口是否打开可以简单执行如下:--//参考链接:https://dba010.com/2019/02/04/check-if-a-port-on-a-remote-system-is-reachable-with ......

[20190211]简单测试端口是否打开.txt

--//昨天看一个链接,提到如果判断一个端口是否打开可以简单执行如下:
--//参考链接:https://dba010.com/2019/02/04/check-if-a-port-on-a-remote-system-is-reachable-without-telnet/

1.测试:
# cat < /dev/tcp/192.168.100.40/1521
# echo $?
0
# cat < /dev/tcp/192.168.100.40/1522
-bash: connect: connection refused
-bash: /dev/tcp/192.168.100.40/1522: connection refused

# alias zdate
alias zdate='date +'\''%y/%m/%d %t'\'''
--//不过我的测试需要等待一分钟,不知道那个受那个内核参数的影响,不知道那位知道:
# zdate ;cat < /dev/tcp/192.168.100.40/1521 ; zdate
2019/02/11 08:42:59
2019/02/11 08:43:59

--//换另外一台机器测试:
# zdate ;cat < /dev/tcp/192.168.100.78/1521;zdate
2019/02/11 08:46:07
2019/02/11 08:46:17

# zdate ;cat < /dev/tcp/192.168.100.40/1521;zdate
2019/02/11 08:46:36
2019/02/11 08:47:36

--//视乎和远程服务器某个内核参数有关.测试100.40需要60秒(这台机器很老redhat 4.3的版本),而测试100.78需要10秒.
--//在100.78上使用tcpdump监测:
# tcpdump -i eth0 host 192.168.xxx.xx -nnn
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type en10mb (ethernet), capture size 96 bytes
09:03:41.242607 ip 192.168.xxx.xx.48292 > 192.168.100.78.1521: s 226276375:226276375(0) win 29200 <mss 1460,sackok,timestamp 3393435130 0,nop,wscale 7>
09:03:41.242785 ip 192.168.100.78.1521 > 192.168.xxx.xx.48292: s 995705425:995705425(0) ack 226276376 win 14480 <mss 1460,sackok,timestamp 1354661055 3393435130,nop,wscale 7>
09:03:41.242852 ip 192.168.xxx.xx.48292 > 192.168.100.78.1521: . ack 1 win 229 <nop,nop,timestamp 3393435130 1354661055>
09:03:51.253449 ip 192.168.100.78.1521 > 192.168.xxx.xx.48292: f 1:1(0) ack 1 win 114 <nop,nop,timestamp 1354671066 3393435130>
09:03:51.253770 ip 192.168.xxx.xx.48292 > 192.168.100.78.1521: f 1:1(0) ack 2 win 229 <nop,nop,timestamp 3393445141 1354671066>
09:03:51.253797 ip 192.168.100.78.1521 > 192.168.xxx.xx.48292: . ack 2 win 114 <nop,nop,timestamp 1354671066 3393445141>

--//也可以看出有10秒的等待时间.在100.78上执行:
# echo /proc/sys/net/ipv4/* | xargs   -n 1  strings -1 -f | grep ": 10$"
/proc/sys/net/ipv4/cipso_cache_bucket_size: 10
/proc/sys/net/ipv4/igmp_max_msf: 10
/proc/sys/net/ipv4/inet_peer_gc_mintime: 10
/proc/sys/net/ipv4/tcp_keepalive_intvl: 10

*/
--//开始以为/proc/sys/net/ipv4/tcp_keepalive_intvl: 10有关,我查看100.40的设置/proc/sys/net/ipv4/tcp_keepalive_intvl=75,也不是这个
--//参数.我修改这个参数测试还是10秒.

--//实际上还有1个更快的测试方法,发送信息到/dev/tcp/ip_address/port,例子如下:

# echo a >  /dev/tcp/192.168.100.78/1521
# echo $?
0

# echo a >  /dev/tcp/192.168.100.78/1522
-bash: connect: 拒绝连接
-bash: /dev/tcp/192.168.100.78/1522: 拒绝连接

--//在100.78上使用tcpdump监测:
 # tcpdump -i eth0 host 192.168.xxx.xx -nnn
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type en10mb (ethernet), capture size 96 bytes
09:12:41.312584 ip 192.168.xxx.xx.48331 > 192.168.100.78.1521: s 3309983125:3309983125(0) win 29200 <mss 1460,sackok,timestamp 3393975215 0,nop,wscale 7>
09:12:41.312761 ip 192.168.100.78.1521 > 192.168.xxx.xx.48331: s 124881852:124881852(0) ack 3309983126 win 14480 <mss 1460,sackok,timestamp 1355201125 3393975215,nop,wscale 7>
09:12:41.312850 ip 192.168.xxx.xx.48331 > 192.168.100.78.1521: . ack 1 win 229 <nop,nop,timestamp 3393975216 1355201125>
09:12:41.312895 ip 192.168.xxx.xx.48331 > 192.168.100.78.1521: p 1:3(2) ack 1 win 229 <nop,nop,timestamp 3393975216 1355201125>
09:12:41.312911 ip 192.168.100.78.1521 > 192.168.xxx.xx.48331: . ack 3 win 114 <nop,nop,timestamp 1355201125 3393975216>
09:12:41.312922 ip 192.168.xxx.xx.48331 > 192.168.100.78.1521: f 3:3(0) ack 1 win 229 <nop,nop,timestamp 3393975216 1355201125>
09:12:41.313454 ip 192.168.100.78.1521 > 192.168.xxx.xx.48331: f 1:1(0) ack 4 win 114 <nop,nop,timestamp 1355201126 3393975216>
09:12:41.313661 ip 192.168.xxx.xx.48331 > 192.168.100.78.1521: . ack 2 win 229 <nop,nop,timestamp 3393975217 1355201126>
--//这样几乎瞬间响应.后面2个网络包是监测1522端口的.
09:13:08.704102 ip 192.168.xxx.xx.15174 > 192.168.100.78.1522: s 2841139274:2841139274(0) win 29200 <mss 1460,sackok,timestamp 3394002608 0,nop,wscale 7>
09:13:08.704140 ip 192.168.100.78.1522 > 192.168.xxx.xx.15174: r 0:0(0) ack 2841139275 win 0

--//前面测试在root用户下进行,我在oracle用户下测试看看:
# zdate ;cat < /dev/tcp/192.168.100.40/1521 ; zdate
2019/02/11 09:22:51
2019/02/11 09:23:51
--//也是可以的.

2.继续测试:
--//我反复测试发现仅仅探测100.78需要10秒,其它的机器都是60秒.这样可能与内核的参数无关,因为我们服务器许多版本使用与100.78一样的.
--//我最终测试与参数inbound_connect_timeout_listener设置有关(在$oracle_home/network/admin/listener.ora下).

$ grep inbound_connect_timeout_listener listener.ora
inbound_connect_timeout_listener=10

--//而这个参数缺省就是60秒.修改它等于1,要重启监听看看(我测试要重启监听才生效):
# zdate ;cat < /dev/tcp/192.168.100.78/1521;zdate
2019/02/11 09:36:14
2019/02/11 09:36:15

--//ok现在需要1秒.

--//一般以前使用nc或者telnet判断,链接如下:
# echo a | nc -w 1 -n -v  192.168.xxx.xx 1-1521 2>/dev/null | grep "succeeded"
--//参考链接:http://blog.itpub.net/267265/viewspace-2148257/

--//关于inbound_connect_timeout的设置,在sqlnet.ora与listener.ora意义不一样,一个表示等待用户认证超时的时间,一个表示等待用
--//户连接请求超时的时间. 可以参考我以前链接:
--// http://blog.itpub.net/267265/viewspace-2147529/

3.自己在重复验证看看.
$ grep inbound_connect_timeout sqlnet.ora listener.ora
sqlnet.ora:sqlnet.inbound_connect_timeout=30
listener.ora:inbound_connect_timeout_listener=1

$ ps -ef | grep oracleboo[k]
--//没有任何输出.也就是远程没有客户连接服务器.
$ zdate;sqlplus scott/xxxxx@book
2019/02/11 09:48:04
sql*plus: release 11.2.0.4.0 production on mon feb 11 09:48:04 2019
copyright (c) 1982, 2013, oracle.  all rights reserved.
error:
ora-01017: invalid username/password; logon denied
--//注意sqlplus不能使用-l参数,不然马上退出了.
--//切换终端会话,执行如下:
$ ps -ef | grep oracleboo[k]
oracle   43849     1  0 09:48 ?        00:00:00 oraclebook (local=no)
--//可以发现实际上网络连接已经建立.

--//等alert*.log出现如下信息,在取消上面sqlplus的执行:
--//alert.log
mon feb 11 09:48:34 2019
warning: inbound connection timed out (ora-3136)
--//两者时间相减就是30秒.这个参数也就是sqlnet.ora有关.也就是等待用户认证超时的时间.

$ zdate ;cat < /dev/tcp/192.168.100.78/1521;zdate
2019/02/11 10:00:21
2019/02/11 10:00:22
--//正好1秒,这个时候就是表示等待用户连接请求超时的时间.

$ zdate; telnet 192.168.100.78 1521;zdate
2019/02/11 10:01:38
trying 192.168.100.78...
connected to xxxxxdg4.com (192.168.100.78).
escape character is '^]'.
connection closed by foreign host.
2019/02/11 10:01:39

$ time telnet  192.168.100.78 1521
trying 192.168.100.78...
connected to xxxxxdg4.com (192.168.100.78).
escape character is '^]'.
connection closed by foreign host.

real    0m1.007s
user    0m0.001s
sys     0m0.003s

$ /usr/bin/time -p telnet  192.168.100.78 1521 > /dev/null
connection closed by foreign host.
command exited with non-zero status 1
real 1.00
user 0.00
sys 0.00