一、用編輯器寫腳本,存為.sh檔
- 基本架構
- 不一定要有副檔名,但有比較好
- > /dev/null:把標準輸出(stdout)丟掉,因此畫面上不會看到 nmap 的掃描過程。
- -sT:TCP Connect Scan,利用完整 TCP 三向交握測試 Port 是否開啟。
- -oG MySQLscan:把結果以 grepable format(方便用 grep 處理的格式)輸出到MySQLscan
- 用 grep open 將有open的結果寫入MySQLscan2
#! /bin/bash // 告知使用的直譯器
nmap -sT 192.168.121.0-255 -p 3306 >/dev/null -oG MySQLscan
cat MySQLscan | grep open > MySQLscan2
cat MySQLscan2- 稍微進階:使用變數
#! /bin/bash
echo "Enter the starting IP address: "read FirstIP
echo "Enter the octet of the last IP address: "read LastOctetIP
echo "Enter the port number you want to scan for: "read port
nmap -sT $FirstIP-$LastOctetIP -p $port >/dev/null -oG MySQLscan
cat MySQLscan | grep open > MySQLscan2
cat MySQLscan2二、修改權限,改為可執行
用 ls -l 指令會看到變成綠色
sudo chmod 775 <filename>三、執行腳本
./<filename>./ 的意思是告訴系統,只執行當下這個資料夾裡的這個腳本檔案,可以避免其他資料夾有同名的腳本檔案。