​shell版本

​#!/bin/bash

for i in cat /home/haoren/iplist20171214.txt

do
(sleep 1;)|telnet $i 80 2>&1 |grep "Connected to $ip$i">/dev/null 2>&1

if [ $? == 0 ]

     then

         echo "$i 开放了 80端口"

     else

         echo "$i 未开放 80 端口"

  fi

(sleep 1;)|telnet $i 8080 2>&1 |grep "Connected to $ip$i">/dev/null 2>&1

if [ $? == 0 ]

     then

         echo "$i 开放了8080端口"

     else

         echo "$i 未开放8080 端口"

  fi

(sleep 1;)|telnet $i 443 2>&1 |grep "Connected to $ip$i">/dev/null 2>&1

if [ $? == 0 ]

     then

         echo "$i 开放了443端口"

     else

         echo "$i 未开放443端口"

  fi

#nmap -sS $i -p 80 >>/root/saomiaojieguo-nmap.txt
#nmap -sS $i -p 8080 >>/root/saomiaojieguo-nmap.txt
#nmap -sS $i -p 443 >>/root/saomiaojieguo-nmap.txt

nc -zv $i 80 >>/root/saomiaojieguo-nc.txt
nc -zv $i 8080 >>/root/saomiaojieguo-nc.txt
nc -zv $i 443 >>/root/saomiaojieguo-nc.txt

done


​python 版本

​#!/usr/bin/env python
#--coding:utf-8--

import os, sys, re,string
import time, tarfile,getopt
import socket

common_port = [80 ,8080,443]

def check_port(ip):
for port in common_port:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
try:
result = s.connect_ex((ip, port))
except:
s.close()
return False
if result == 0:
print "IP:" + ip + ' Port:' + str(port) + '\n'
s.close()

filename='/home/haoren/iplist20171214.txt'

a_file = open(filename, 'r')

for a_line in a_file.readlines():
print a_line.strip()
ip = a_line.strip()
check_port(ip)