从文件打印

从文件打印

我有一个文件,其在扫描后包含以下详细信息nmap

Host: 45.310.302.11 (li982-11.members.ionic.com)    
Ports: 21/closed/tcp//ftp///, 22/open/tcp//ssh//OpenSSH 5.3 (protocol 2.0)/, 23/closed/tcp//telnet///, 25/closed/tcp//smtp///, 53/closed/tcp//domain///, 80/open/tcp//http//Apache httpd/, 110/closed/tcp//pop3///, 111/closed/tcp//rpcbind///, 135/filtered/tcp//msrpc///, 139/filtered/tcp//netbios-ssn///, 143/closed/tcp//imap///, 443/open/tcp//ssl|http//Apache httpd/, 445/filtered/tcp//microsoft-ds///, 993/closed/tcp//imaps///, 995/closed/tcp//pop3s///, 1723/closed/tcp//pptp///, 3306/open/tcp//mysql//MySQL 5.7.24/, 3389/closed/tcp//ms-wbt-server///, 5900/closed/tcp//vnc///, 8080/closed/tcp//http-proxy///

我想只打印出包含端口 80 和 443 打开的行。这是我使用的以下代码。它可以打印出端口 80,但不能打印出 443

grep -Eo "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}.* 80/open/tcp//http//.*" ~/Downloads/nmap-output.txt|cut -d, -f1,6 --output-delimiter=" "|cut -d ' ' -f1,2,5

我怎样才能打印出端口 443?

答案1

可能有更好的方法,但这就是我要做的。首先转换输入文件:

$ cat nmap-scan.txt

Host: 45.310.302.11 (li982-11.members.ionic.com)    
Ports: 21/closed/tcp//ftp///, 22/open/tcp//ssh//OpenSSH 5.3 (protocol 2.0)/, 23/closed/tcp//telnet///, 25/closed/tcp//smtp///, 53/closed/tcp//domain///, 80/open/tcp//http//Apache httpd/, 110/closed/tcp//pop3///, 111/closed/tcp//rpcbind///, 135/filtered/tcp//msrpc///, 139/filtered/tcp//netbios-ssn///, 143/closed/tcp//imap///, 443/open/tcp//ssl|http//Apache httpd/, 445/filtered/tcp//microsoft-ds///, 993/closed/tcp//imaps///, 995/closed/tcp//pop3s///, 1723/closed/tcp//pptp///, 3306/open/tcp//mysql//MySQL 5.7.24/, 3389/closed/tcp//ms-wbt-server///, 5900/closed/tcp//vnc///, 8080/closed/tcp//http-proxy///

创建一个新文件,其中的字段用新行分隔:

$ sed -e 's/, /\n/g' nmap-scan.txt > nmap-new.txt

$ cat nmap-new.txt

Host: 45.310.302.11 (li982-11.members.ionic.com)    
Ports: 21/closed/tcp//ftp///
22/open/tcp//ssh//OpenSSH 5.3 (protocol 2.0)/
23/closed/tcp//telnet///
25/closed/tcp//smtp///
53/closed/tcp//domain///
80/open/tcp//http//Apache httpd/
110/closed/tcp//pop3///
111/closed/tcp//rpcbind///
135/filtered/tcp//msrpc///
139/filtered/tcp//netbios-ssn///
143/closed/tcp//imap///
443/open/tcp//ssl|http//Apache httpd/
445/filtered/tcp//microsoft-ds///
993/closed/tcp//imaps///
995/closed/tcp//pop3s///
1723/closed/tcp//pptp///
3306/open/tcp//mysql//MySQL 5.7.24/
3389/closed/tcp//ms-wbt-server///
5900/closed/tcp//vnc///
8080/closed/tcp//http-proxy///

然后使用更简单的语法 grep 新文件:

$ grep -E '^80/|^443/' nmap-new.txt

80/open/tcp//http//Apache httpd/
443/open/tcp//ssl|http//Apache httpd/

相关内容