我正在编写一个 bash 脚本来 nmap 扫描开放端口,专门使用 -A 扫描这些端口
nmap -T3 -p- $1 > "openPorts.txt" #$1 is an IP provided when calling the script
ports=$(grep "open" openPorts.txt | cut -d " " -f 1 | tr -d "/tcp" | tr '\n' ', ')
nmap -T3 -p "$ports" -A > "openPorts.txt"
但是,运行脚本后,我从文件中的第一个 nmap 调用中获取打印信息,然后遇到错误,控制台显示:
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-02-01 16:44 EST
WARNING: No targets were specified, so 0 hosts scanned.
Nmap done: 0 IP addresses (0 hosts up) scanned in 0.02 seconds
我对脚本编写很陌生,甚至对 bash 也很陌生,所以很可能是格式问题。我在网上寻找了一些答案,但似乎不知道如何表达这个问题,所以谷歌知道我在寻找什么。
-旁注- 我意识到我可能可以将“$ports”声明放入 nmap 扫描中,但我无法弄清楚语法,而且我并不想做出世界上最有效的事情,所以这并不是世界末日我。
我想通过
nmap
列出打开的端口:
nmap -T3 -oX - localhost | xmlstarlet sel -t -v '//port/@portid'
我使用一种干净的方式将格式导出为 XML,然后在 xmlstarlet
中使用
xpath查询。