端口檢查指令
lsof
lsof -i:端口號(hào)用于查詢(xún)某一端口的占用情況,比如查詢(xún)我們熟悉的3306端口的使用情況,可以使用lsof -i:3306
root@iZbp11s2rh7xf6u48hlcroZ:~# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 20109 mysql 16u IPv6 50086 0t0 TCP *:mysql (LISTEN)
可以看到3306端口已經(jīng)被mysql數(shù)據(jù)庫(kù)的守護(hù)進(jìn)程mysqld給占用了。
netstat
netstat -tunpl|grep 端口號(hào),用于查看指定的端口號(hào)的進(jìn)程情況,如查看3306端口的進(jìn)程情況,可以使用netstat -tunpl|grep 3306
root@iZbp11s2rh7xf6u48hlcroZ:~# netstat -tunpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:32000 0.0.0.0:* LISTEN 4350/java
tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN 16977/java
tcp 0 0 0.0.0.0:8009 0.0.0.0:* LISTEN 16977/java
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 23371/redis-server
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 16977/java
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 911/sshd
tcp 0 0 127.0.0.1:8088 0.0.0.0:* LISTEN 18522/influxd
tcp6 0 0 :::3306 :::* LISTEN 20109/mysqld
tcp6 0 0 :::6379 :::* LISTEN 23371/redis-server
tcp6 0 0 :::8086 :::* LISTEN 18522/influxd
udp 0 0 0.0.0.0:68 0.0.0.0:* 614/dhclient
udp 0 0 172.16.113.172:123 0.0.0.0:* 755/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 755/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 755/ntpd
udp6 0 0 :::123 :::* 755/ntpd
root@iZbp11s2rh7xf6u48hlcroZ:~# netstat -tunpl|grep 3306
tcp6 0 0 :::3306 :::* LISTEN 20109/mysqld
對(duì)上述指令中的關(guān)鍵參數(shù)進(jìn)行一下介紹:
-t (tcp):僅顯示tcp相關(guān)的選項(xiàng);
-u(udp):僅顯示udp相關(guān)的選項(xiàng);
-n:拒絕顯示別名,能顯示為數(shù)字的全部顯示為數(shù)字;
-p:顯示建立連接的程序名;
-l:僅列出在listen的服務(wù)狀態(tài);
端口掃描程序
下面給出一個(gè)python編寫(xiě)的簡(jiǎn)單的掃描的端口占用檢測(cè)的程序ip_scan.py,該程序可以檢測(cè)指定的IP地址下0-65534端口的占用情況,完整的腳本程序如下,無(wú)需引用任何的第三方的包:
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
import socket, time, thread
socket.setdefaulttimeout(3) # 設(shè)置套接字的默認(rèn)超時(shí)時(shí)間
def socket_scan(ip, port):
"""
輸入IP和端口號(hào),掃描判斷端口是否被占用
:param ip: 待掃描的IP地址
:param port: 待掃描的端口號(hào)
:return:
"""
try:
if port > 65535:
print u'端口掃描結(jié)束'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 使用了socket.connect_ex方法,該方法不會(huì)拋出異常,只會(huì)給出錯(cuò)誤碼,
result = s.connect_ex((ip, port))
# 如果錯(cuò)誤碼為0表示成功連上了某個(gè)端口;如果返回其他值表示出錯(cuò)了,端口沒(méi)有打開(kāi)。
if result == 0:
lock.acquire()
print ip, u':', port, u'端口已經(jīng)被占用'
lock.release()
except:
print u'端口掃描異常'
def ip_scan(ip):
"""
輸入IP地址,掃描0-65534端口情況
:param ip:
:return:
"""
try:
print u'開(kāi)始掃描 %s' % ip
start_time = time.time()
for i in range(0, 65534):
thread.start_new_thread(socket_scan, (ip, int(i)))
print u'端口掃描完成,總共耗時(shí):%.2f秒' % (time.time() - start_time)
# raw_input("Please press enter to exit")
except:
print u'掃描ip出錯(cuò)'
if __name__ == '__main__':
ip_address = raw_input("Please input the IP address you want to scan: ")
lock = thread.allocate_lock()
ip_scan(ip_address)
在本人的一臺(tái)阿里云的云主機(jī)上的測(cè)試結(jié)果如下所示:
root@iZbp11s2rh7xf6u48hlcroZ:/home/louxj424/document# python ip_scan.py
Please input the IP address you want to scan: 127.0.0.1
開(kāi)始掃描 127.0.0.1
127.0.0.1 : 22 端口已經(jīng)被占用
127.0.0.1 : 3306 端口已經(jīng)被占用
127.0.0.1 : 6379 端口已經(jīng)被占用
127.0.0.1 : 8005 端口已經(jīng)被占用
127.0.0.1 : 8009 端口已經(jīng)被占用
127.0.0.1 : 8080 端口已經(jīng)被占用
127.0.0.1 : 8086 端口已經(jīng)被占用
127.0.0.1 : 8088 端口已經(jīng)被占用
127.0.0.1 : 32000 端口已經(jīng)被占用
端口掃描完成,總共耗時(shí):5.05秒