TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
怎么样扫描计算机系统本地和远程的端口,监测其是打开还是关闭的在很多应用程序中都要用到,下面是用java实现的简单的端口扫描程序,在局域网中使用还不错。扫描结果会输出到文件到控制台。
Source Code: -------------------------------------------------------------------------------
/*
* Created on 2005-3-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/ /**
* @author whandey connect to me: whandey@163.com
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class SocketPort {
- public static void main(String[] args) {
- String ip = "10.10.1.1";
- String hostname = new String();
- try{ //get the target ip address and hostname
- InetAddress address = InetAddress.getByName(ip);
- System.out.println(address);
- hostname = address.getHostName();
- System.out.println(hostname);
- }
- catch(UnknownHostException e){
- System.out.println("Could not find "+ ip);
- }
- try{ // creat the output file
- PrintWriter fout = new PrintWriter( new FileWriter("PortInf.txt"));
- fout.println("Information Of The Port On the " + hostname +"computer ");
- System.out.println("Information Of The Port On the " + hostname +"computer ");
- fout.println();
- // do ports scan
- for(int nport = 24;nport <= 60;++nport){
- try{
- Socket s = new Socket(hostname,nport);
- fout.println("The port " + nport + " is open!");
- System.out.println("The port " + nport + " is open!");
- fout.println("Connected to "+ s.getInetAddress() + " on port " + s.getPort() + " from port "+
- s.getLocalPort() + " of " + s.getLocalAddress());
- System.out.println("Connected to "+ s.getInetAddress() + " on port " + s.getPort() + " from port "+
- s.getLocalPort() + " of " + s.getLocalAddress());
- //print the connected socket information
- }
- catch(IOException e){
- fout.println("The port " + nport + " is closed!");
- System.out.println("The port " + nport + " is closed!");
- }
- }
- fout.close();
- }
- catch(IOException e){}
- }
- }
复制代码 下面是程序运行结果:
C:java>java SocketPort
/10.10.1.1
ZZ3ZONE
Information Of The Port On the ZZ3ZONEcomputer
The port 24 is closed!
The port 25 is open!
Connected to ZZ3ZONE/10.10.1.1 on port 25 from port 4868 of /10.10.1.99
The port 26 is closed!
The port 27 is closed!
The port 28 is closed!
The port 29 is closed!
The port 30 is closed!
The port 31 is closed!
The port 32 is closed!
The port 33 is closed!
The port 34 is closed!
The port 35 is closed!
The port 36 is closed!
The port 37 is closed!
The port 38 is closed!
The port 39 is closed!
The port 40 is closed!
The port 41 is closed!
The port 42 is open!
Connected to ZZ3ZONE/10.10.1.1 on port 42 from port 1177 of /10.10.1.99
The port 43 is closed!
The port 44 is closed!
The port 45 is closed!
The port 46 is closed!
The port 47 is closed!
The port 48 is closed!
The port 49 is closed!
The port 50 is closed!
The port 51 is closed!
The port 52 is closed!
The port 53 is open!
Connected to ZZ3ZONE/10.10.1.1 on port 53 from port 1313 of /10.10.1.99
The port 54 is closed!
The port 55 is closed!
The port 56 is closed!
The port 57 is closed!
The port 58 is closed!
The port 59 is closed!
The port 60 is closed! C:java>
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
源码下载:http://file.javaxxz.com/2014/10/31/000014968.zip |
|