Linux的shell編程前奏之常見網絡管理命令實戰六ios
前言: nmap命令是一款開發源代碼的網絡探測和安全審覈工具,是Network Mapper的縮寫。其設計目標是快速地掃描大型網絡。nmap能夠發現網絡上有哪些主機,主機提供了什麼服務(應用程序名稱和版本號),並探測操做系統的類型及版本信息。shell
1>若是系統沒有nmap命令,則可使用下面的命令來安裝:編程
[root@lll bin]# yum -y install nmap 已加載插件:fastestmirror, langpacks Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast http://mirrors.cqu.edu.cn/CentOS/7.6.1810/os/x86_64/repodata/repomd.xml: [Errno 14] curl#56 - "Recv failure: Connection reset by peer" 正在嘗試其它鏡像。 base | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/2): extras/7/x86_64/primary_db | 179 kB 00:00:01 (2/2): updates/7/x86_64/primary_db | 2.4 MB 00:01:29 Determining fastest mirrors * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: centos.ustc.edu.cn 正在解決依賴關係 --> 正在檢查事務 ---> 軟件包 nmap.x86_64.2.6.40-13.el7 將被 升級 ---> 軟件包 nmap.x86_64.2.6.40-16.el7 將被 更新 --> 正在處理依賴關係 nmap-ncat = 2:6.40-16.el7,它被軟件包 2:nmap-6.40-16.el7.x86_64 須要 --> 正在檢查事務 ---> 軟件包 nmap-ncat.x86_64.2.6.40-13.el7 將被 升級 ---> 軟件包 nmap-ncat.x86_64.2.6.40-16.el7 將被 更新 --> 解決依賴關係完成 依賴關係解決 ================================================================================================================ Package 架構 版本 源 大小 ================================================================================================================ 正在更新: nmap x86_64 2:6.40-16.el7 base 3.9 M 爲依賴而更新: nmap-ncat x86_64 2:6.40-16.el7 base 206 k 事務概要 ================================================================================================================ 升級 1 軟件包 (+1 依賴軟件包) 總下載量:4.2 M Downloading packages: No Presto metadata available for base (1/2): nmap-ncat-6.40-16.el7.x86_64.rpm | 206 kB 00:00:01 (2/2): nmap-6.40-16.el7.x86_64.rpm | 3.9 MB 00:00:02 ---------------------------------------------------------------------------------------------------------------- 總計 1.2 MB/s | 4.2 MB 00:00:03 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在更新 : 2:nmap-ncat-6.40-16.el7.x86_64 1/4 正在更新 : 2:nmap-6.40-16.el7.x86_64 2/4 清理 : 2:nmap-6.40-13.el7.x86_64 3/4 清理 : 2:nmap-ncat-6.40-13.el7.x86_64 4/4 驗證中 : 2:nmap-ncat-6.40-16.el7.x86_64 1/4 驗證中 : 2:nmap-6.40-16.el7.x86_64 2/4 驗證中 : 2:nmap-ncat-6.40-13.el7.x86_64 3/4 驗證中 : 2:nmap-6.40-13.el7.x86_64 4/4 更新完畢: nmap.x86_64 2:6.40-16.el7 做爲依賴被升級: nmap-ncat.x86_64 2:6.40-16.el7 完畢!
2>nmap查看目標主機開放端口windows
[root@lll bin]# nmap 192.168.0.100 Starting Nmap 6.40 ( http://nmap.org ) at 2019-02-16 16:57 CST Nmap scan report for 192.168.0.100 (192.168.0.100) Host is up (0.00052s latency). Not shown: 990 closed ports PORT STATE SERVICE 135/tcp open msrpc 139/tcp open netbios-ssn 445/tcp open microsoft-ds 902/tcp open iss-realsecure 912/tcp open apex-mesh 3389/tcp open ms-wbt-server 49152/tcp open unknown 49154/tcp open unknown 49155/tcp open unknown 49156/tcp open unknown MAC Address: 48:D2:24:2C:19:F8 (Liteon Technology) Nmap done: 1 IP address (1 host up) scanned in 0.72 seconds
3>nmap掃描主機的指定端口centos
[root@lll bin]# nmap -p 200-500 192.168.0.100 Starting Nmap 6.40 ( http://nmap.org ) at 2019-02-16 16:58 CST Nmap scan report for 192.168.0.100 (192.168.0.100) Host is up (0.00048s latency). Not shown: 300 closed ports PORT STATE SERVICE 445/tcp open microsoft-ds MAC Address: 48:D2:24:2C:19:F8 (Liteon Technology) Nmap done: 1 IP address (1 host up) scanned in 0.42 seconds
4>掃描局域網內全部ip安全
[root@lll /]# nmap 192.168.0.0/24 Starting Nmap 6.40 ( http://nmap.org ) at 2019-02-16 17:08 CST Nmap scan report for 192.168.0.1 (192.168.0.1) Host is up (0.0045s latency). Not shown: 998 filtered ports PORT STATE SERVICE 80/tcp open http 1900/tcp open upnp MAC Address: 20:6B:E7:72:8D:D5 (Unknown) Nmap scan report for 192.168.0.100 (192.168.0.100) Host is up (0.0012s latency). Not shown: 990 closed ports PORT STATE SERVICE 135/tcp open msrpc 139/tcp open netbios-ssn 445/tcp open microsoft-ds 902/tcp open iss-realsecure 912/tcp open apex-mesh 3389/tcp open ms-wbt-server 49152/tcp open unknown 49154/tcp open unknown 49155/tcp open unknown 49156/tcp open unknown MAC Address: 48:D2:24:2C:19:F8 (Liteon Technology) Nmap scan report for 192.168.0.101 (192.168.0.101) Host is up (0.032s latency). Not shown: 999 closed ports PORT STATE SERVICE 55555/tcp open unknown MAC Address: 70:D9:23:B8:DA:A7 (Unknown) Nmap scan report for 192.168.0.103 (192.168.0.103) Host is up (0.019s latency). All 1000 scanned ports on 192.168.0.103 (192.168.0.103) are closed MAC Address: 34:79:16:56:DC:DA (Unknown) RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 RTTVAR has grown to over 2.3 seconds, decreasing to 2.0 Nmap scan report for 192.168.0.104 (192.168.0.104) Host is up (0.000062s latency). Not shown: 996 closed ports PORT STATE SERVICE 21/tcp open ftp 22/tcp open ssh 80/tcp open http 111/tcp open rpcbind
5>探測目標主機的服務和操做系統的版本bash
[root@lll /]# nmap -O -sV 192.168.0.100 Starting Nmap 6.40 ( http://nmap.org ) at 2019-02-16 17:11 CST Nmap scan report for 192.168.0.100 (192.168.0.100) Host is up (0.0012s latency). Not shown: 990 closed ports PORT STATE SERVICE VERSION 135/tcp open msrpc Microsoft Windows RPC 139/tcp open netbios-ssn 445/tcp open netbios-ssn 902/tcp open ssl/vmware-auth VMware Authentication Daemon 1.10 (Uses VNC, SOAP) 912/tcp open vmware-auth VMware Authentication Daemon 1.0 (Uses VNC, SOAP) 3389/tcp open ms-wbt-server Microsoft Terminal Service 49152/tcp open msrpc Microsoft Windows RPC 49154/tcp open msrpc Microsoft Windows RPC 49155/tcp open msrpc Microsoft Windows RPC 49156/tcp open msrpc Microsoft Windows RPC MAC Address: 48:D2:24:2C:19:F8 (Liteon Technology) Device type: general purpose Running: Microsoft Windows Vista OS CPE: cpe:/o:microsoft:windows_vista OS details: Microsoft Windows Vista Network Distance: 1 hop Service Info: OS: Windows; CPE: cpe:/o:microsoft:windows OS and Service detection performed. Please report any incorrect results at http://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 56.84 seconds
6>寫一個shell腳本,判斷192.168.0.0/24網絡裏,當前在線的ip有哪些?(掃描網絡內存活主機)網絡
方法一: #!/bin/sh CMD="ping -W 2 -c 2" Ip="192.168.0." for n in $(seq 254)----------正確的說法是:根據IP地址和子網掩碼進行計算,IP地址所在網段的首位和末尾分別是網段地址和廣播地址,不能使用。 do { $CMD $Ip$n &>/dev/null if [ $? -eq 0 ];then echo "$Ip$n is ok" fi }& done 方法二: #!/bin/sh CMD="nmap -sP" Ip="192.168.0.0/24" $CMD $Ip|awk '/Nmap scan report for/ {print $NF}' ##/Nmap scan report for/ 表示匹配到該字段所在的行;$NF 表示匹配最後一列