我为端口扫描
编写了一个python程序这是我的源代码...
import socket
t_host = str(input("Enter the host to be scanned: "))
t_ip = socket.gethostbyname(t_host)
print(t_ip)
list=[1,3,4,6,7,9,13,17,19,20,21,22,23,24,25,26,30,32,33,37,42,43,49,53,70,79,80,81,82,83,84,85,88,89,90,99,100,106,109,110,111,113,119,125,135,139,143,144,146,161,163,179,199,211,212,222,254,255,256,259,264,280,301,306,311,340,366,389,40]
sock = socket.socket()
sock.settimeout(1)
for item in list:
t_port=int(item)
result = sock.connect_ex((t_ip,t_port))
if result == 0:
print("Port",t_port,": Opened")
else:
print("port",t_port,": Closed")
sock.close()
print("Port Scanning complete")
当我在列表中使用单个项目时,它会扫描并正确打开/关闭端口。(即)列表中的第一个项目被正确评估,而剩余的未被评估为正确地打开或关闭
例如list=[80]
会将输出正确打开。
但是,如果列表包含多个项目,则会为所有端口显示“已关闭”
例如list=[80,23,443,25]
的输出为port 80 : Opened
,但其余的都关闭了
答案 0 :(得分:3)
您不能将同一套接字用于多个连接。您需要在循环中每次创建一个新的套接字。
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.io.PrintStream;
public class Index {
public static void main(String[] args) {
long startTime = System.nanoTime();
PrintStream stdout = System.out;
try {
File folder = new File(args[0]);
File[] files = folder.listFiles();
for (File file : files) {
String name = file.getName();
int pos = name.lastIndexOf(".");
if (pos > 0) {
name = name.substring(0, pos);
}
Scanner sc;
sc = new Scanner(file);
Map<String, String> wordCount = new TreeMap<String, String>();
int count = 0;
while(sc.hasNext()) {
String word = sc.next();
word = word.trim().toLowerCase();
int len = word.length();
count = (int) count + len;
int pageNumber = (int) Math.ceil(count / Float.valueOf(args[2]));
if(!wordCount.containsKey(word))
wordCount.put(word, Integer.toString(pageNumber));
else
wordCount.put(word, wordCount.get(word) + ", " + Integer.toString(pageNumber));
}
// show results
sc.close();
PrintStream outputFile = new PrintStream(args[1]+"/"+name+"_output.txt");
System.setOut(outputFile);
for(String word : wordCount.keySet())
System.out.println(word + " " + wordCount.get(word));
}
}
catch(IOException e) {
System.out.println("Unable to read from file.");
}
long endTime = System.nanoTime();
long totalTime = endTime - startTime;
System.setOut(stdout);
System.out.println(totalTime / 1000000);
}
}
摘自socket.close()
的文档:
以后对套接字对象的所有操作都会失败。
答案 1 :(得分:0)
您的问题与端口列表无关, 但是您多次使用的套接字对象。
套接字关闭后,it can not be reused:
将插槽标记为关闭。当关闭makefile()中的所有文件对象时,基础系统资源(例如文件描述符)也将关闭。一旦发生这种情况,套接字对象上的所有将来操作都会失败。远程端将不再接收任何数据(在清除排队的数据之后)。
您可能要为每个扫描端口创建一个新的套接字:
import socket
t_host = str(input("Enter the host to be scanned: "))
t_ip = socket.gethostbyname(t_host)
print(t_ip)
list=[1,3,4,6,7,9,13,17,19,20,21,22,23,24,25,26,30,32,33,37,42,43,49,53,70,79,80,81,82,83,84,85,88,89,90,99,100,106,109,110,111,113,119,125,135,139,143,144,146,161,163,179,199,211,212,222,254,255,256,259,264,280,301,306,311,340,366,389,40]
for item in list:
t_port=int(item)
# create a new socket for each port scanned
sock = socket.socket()
sock.settimeout(1)
# probe port
result = sock.connect_ex((t_ip,t_port))
if result == 0:
print("Port",t_port,": Opened")
else:
print("port",t_port,": Closed")
sock.close()