端口扫描程序设计._第1页
端口扫描程序设计._第2页
端口扫描程序设计._第3页
端口扫描程序设计._第4页
端口扫描程序设计._第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、网络综合实验任务书一、目的与要求1. 性质:设计性实验。2. 任务:设计并实现一个端口扫描程序,检测某个IP或某段IP的计算机的端口工作 情况。3. 目的:加深对课堂讲授知识的理解, 熟练掌握基本的网络编程技术和方法,建立网 络编程整体概念,使得学生初步具有研究、设计、编制和调试网络程序的能力。4. 要求:熟悉有关定义、概念和实现算法, 设计出程序流程框图和数据结构,编写出 完整的源程序,基本功能完善,方便易用,操作无误。5. 学生要求人数:1人。二、主要内容1. 编写一个端口扫描程序, 能够显示某个IP或某段IP的计算机的某一个或某些端口 是否正在工作。基本工作过程如下:(1) 设定好一定的

2、端口扫描范围;(2) 设定每个端口扫描的次数,因为有可能有的端口一次扫描可能不通;(3) 创建socket,通过socket的connect方法来连接远程IP地址以及对应的端口;(4) 如果返回false,表示端口没有开放,否则端口开放。三、进度计划序号设计内容完成时间备注1任务布置及资料收集第1周周一、周二2软件方案设计与制定第1周周三至周四3编程实现第1周周周五至第2周周二4程序测试与修改,撰写报告第2周周三至周五上午5报告提交,软件验收周五下午四、设计成果要求1. 完成规定的实验任务,保质保量;2 完成综合实验报告, 要求格式规范, 内容具体而翔实,应体现自身所做的工作,注 重对实验思路

3、的归纳和对问题解决过程的总结。五、考核方式1 平时成绩验收答辩实验报告;2 五级分制。学生姓名:指导教师:2009 年 6月8 日、综合实验的目的与要求1目的:加深对课堂讲授知识的理解,熟练掌握基本的网络编程技术和方法,建立网 络编程整体概念,加深对JAVA编程语言的使用,促进编程能力的提高,同时为网络编程打下一个比较好的基础。同时对实验的过程要有完全的了解。2. 要求:熟悉有关定义、概念和实现算法,设计出程序流程框图和数据结构,编写出 完整的源程序,基本功能完善,方便易用,操作无误。、综合实验正文1. 编程语言的选择针对编写的是端口扫描的程序,所以我选择了JAVA编程,因为要简单而方便的试验

4、端口扫描的功能。2. 端口扫描实现的功能是:显示某个IP或某段IP的计算机的某一个或某些端口是否正 在工作2. 1如何选择IP段和端口创建socket,通过socket的connect方法来连接远程IP地址以及对应的端口。而我使用 的是socket函数来实现端口的扫描和IP地址的选择扫描。2. 2如何实现快速扫描端口通过实现多线程,当一个 IP或者一个端口同时分配多个线程进行扫描,速度会比单线 程扫描快许多。2. 3显示扫描结果可以在使用JAVA编写的时候,可以通过使用JScrollPane控件实现显示结果。2. 4实现要求功能之余美化外观3. 创建应用程序界面3. 1建立主窗体,“JAVA端

5、口扫描器”图1-13. 2 IP地址的输入部分 请选挥:图1-23. 3端口部分,其中包括指定的端口和指定扫描的端口范围W5 口 泗0:1000图1-33. 4多线程的实现和延时的控制部分100图1-43. 5增加的保存及快捷键功能图1-54 .扫描的整个过程4. 1 填写 IP 地址 00 204. 2填写端口扫描的范围为1 3004. 3线程数选择为1004 . 4扫描结果如下!- 1FW 止:192| 16B1100 1201 1O主机名:locathost端口范匡1:0| 十00请选择:戟程埶100并始扫楷退出扫借结果:192.168.110

6、O:139:(net0IOS00 110:(POP)192 16S 1.101.80:(HTTP192J6B 1 10Tl39:(netBEOS)图1-6三端口扫描的主要部分程序如下import .*;import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;/*实现扫描的主体程序*/public class ThreadScanpublic static JFrame main=new JFrame(JA VA 端口扫描器 );/显示扫描结果public static

7、JTextArea Result=new JTextArea(,4,40);/滚动条面板public static JScroll PaneresultPane newJScrollPane(Result,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;/输入主机名文本框public static JTextField hostname=new JTextField(localhost,8);/输入 ip 地址前 3 位的输入框public static JTextFiel

8、d fromip1=new JTextField(0,3);/输入 ip 地址 46 位的输入框public static JTextField fromip2=new JTextField(0,3);/输入 ip 地址 79 位的输入框public static JTextField fromip3=new JTextField(0,3);/输入起始 ip 地址最后 4 位的输入框 public static JTextField fromip4=new JTextField(0,3);/输入目标 ip 地址最后 4 位的输入框public static JTextField toip=ne

9、w JTextField(0,3);/输入最小端口的输入框public static JTextField minPort=new JTextField(0,4); /输入最大端口的输入框public static JTextField maxPort=new JTextField(1000,4); /输入最大线程数量的输入框public static JTextField maxThread=new JTextField(100,3);/错误提示框public static JDialog DLGError=new JDialog(main, 错误 !);public static JLab

10、el DLGINFO=new JLabel();public static JLabel type=new JLabel( 请选择: );/扫描类型public static JRadioButton radioIp = new JRadioButton(IP 地址: ); public static JRadioButton radioHost = new JRadioButton( 主机名: ,true);/单选框组public static ButtonGroup group = new ButtonGroup();public static JLabel P1=new JLabel(

11、端口范围 :);public static JLabel P2=new JLabel();public static JLabel P3=new JLabel();public static JLabel Pdot1 = new JLabel(.);public static JLabel Pdot2 = new JLabel(.);public static JLabel Pdot3 = new JLabel(.);public static JLabel TNUM=new JLabel( 线程数 :);public static JLabel RST=new JLabel( 扫描结果 :

12、);public static JLabel con=new JLabel( );/定义按钮public static JButton OK = new JButton( 确定 );public static JButton Submit = new JButton( 开始扫描 );public static JButton Cancel = new JButton( 退出 );public static JButton saveButton = new JButton( 保存扫描结果 );/菜单栏public static JMenuBar myBar = new JMenuBar();pu

13、blic static JMenu myMenu = new JMenu( 文件 (F);public static JMenuItem saveItem = new JMenuItem( 保存扫描结果 (S);public static JMenuItem exitItem = new JMenuItem( 退出 (Q);public static void main(String args) main.setSize(500,400);main.setLocation(300,300); main.setResizable(false);main.setLayout(new GridBag

14、Layout(); main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);DLGError.setSize(300,100); DLGError.setLocation(400,400);/添加“菜单栏” myMenu.add(saveItem);myMenu.add(exitItem); myBar.add(myMenu);main.setJMenuBar(myBar);/设置热键 myMenu.setMnemonic(F);saveItem.setMnemonic (S); /为“另存为”组件设置快捷键为ctrl+ssaveItem.set

15、Accelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_ MASK);exitItem.setMnemonic(Q);exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_ MASK);/采用表格包型布局Container mPanel = main.getContentPane(); GridBagConstraints c = new GridBagConstraints(); c.insets = new Inset

16、s(10,0,0,10);c.gridx = 0;c.gridy = 0;c.gridwidth = 10;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(type,c);group.add(radioIp);group.add(radioHost);c.gridx = 0;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPane

17、l.add(radioIp,c);c.gridx = 1;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(fromip1,c);c.gridx = 2;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(Pdot1,c);c.gridx = 3;c.gridy

18、 = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(fromip2,c);c.gridx = 4;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(Pdot2,c);c.gridx = 5;c.gridy = 1;c.gridwidth = 1;c.fill = GridBa

19、gConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(fromip3,c);c.gridx = 6;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(Pdot3,c);c.gridx = 7;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBag

20、Constraints.CENTER; mPanel.add(fromip4,c);c.gridx = 8;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(P2,c);c.gridx = 9;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(toip,c);

21、c.gridx = 0;c.gridy = 2;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(radioHost,c);c.gridx = 1;c.gridy = 2;c.gridwidth = 3;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(hostname,c);c.gridx = 0;c.gridy = 3;c.gridw

22、idth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(P1,c);c.gridx = 1;c.gridy = 3;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(minPort,c);c.gridx = 2;c.gridy = 3;c.gridwidth = 1;c.fill = GridBagConstraints.BO

23、TH;c.anchor = GridBagConstraints.CENTER; mPanel.add(P3,c);c.gridx = 3;c.gridy = 3;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(maxPort,c);c.gridx = 0;c.gridy = 4;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER

24、; mPanel.add(TNUM,c);c.gridx = 1;c.gridy = 4;c.gridwidth = 3;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(maxThread,c);c.gridx = 0;c.gridy = 5;c.gridwidth = 3;c.fill = GridBagConstraints.VERTICAL;c.anchor = GridBagConstraints.CENTER; mPanel.add(Submit,c);c.gridx

25、= 6;c.gridy = 5;c.gridwidth = 4;c.fill = GridBagConstraints.VERTICAL;c.anchor = GridBagConstraints.CENTER; mPanel.add(Cancel,c);c.gridx = 0;c.gridy = 6;c.gridwidth = 10;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER; mPanel.add(RST,c);/设置文本区域可以换行Result.setLineWrap(true);/设置文本区

26、域不可编辑Result.setEditable(false);c.gridx = 0;c.gridy = 7; c.gridwidth = 10;c.gridheight = 4;c.fill = GridBagConstraints.VERTICAL;c.anchor = GridBagConstraints.CENTER; mPanel.add(resultPane,c);Container dPanel = DLGError.getContentPane(); dPanel.setLayout(new FlowLayout(FlowLayout.CENTER); dPanel.add(D

27、LGINFO);dPanel.add(OK);Submit.addActionListener(new SubmitAction();Cancel.addActionListener(new CancleAction();OK.addActionListener(new OKAction();/实现退出功能exitItem.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) System.exit(0););/实现帮助功能s

28、aveButton.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) JFileChooser fc=new JFileChooser(); int returnVal=fc.showSaveDialog(null);/点击“保存” if(returnVal = 0) File saveFile=fc.getSelectedFile(); try FileWriter writeOut = new FileWriter(s

29、aveFile); writeOut.write(ThreadScan.Result.getText(); writeOut.close();catch (IOException ex) System.out.println( 保存失败 );/点击“取消”elsereturn;);main.setVisible(true);/*实现“取消”功能*退出程序*/class CancleAction implements ActionListenerpublic void actionPerformed (ActionEvent e)System.exit(0);/*实现“确定”功能*完成扫描*/c

30、lass SubmitAction implements ActionListenerpublic void actionPerformed (ActionEvent a)int minPort;int maxPort;int maxThread;int ip1 = 0;int ip2 = 0;int ip3 = 0;int ipstart = 0;int ipend = 0;String ipaddress = ;String hostname = ;ThreadScan.Result.setText();/将确定 按钮设置成为不可用 if(ThreadScan.Submit.isEnabl

31、ed()ThreadScan.Submit.setEnabled(false);/*判断搜索的类型* 按照 ip 地址扫描: type = 0*按照主机名称扫描: type = 1*/if(ThreadScan.radioIp.isSelected()TCPThread.type = 0;/判断 ip 的前 3位是否为 int 型try ip1=Integer.parseInt(ThreadScan.fromip1.getText();catch(NumberFormatException e) ThreadScan.DLGINFO.setText( 错误的 ip!); ThreadScan.

32、DLGError.setVisible(true); return;/ 判断 ip 的 46 位是否为 int 型tryip2=Integer.parseInt(ThreadScan.fromip2.getText();catch(NumberFormatException e)ThreadScan.DLGINFO.setText( 错误的 ip!);ThreadScan.DLGError.setVisible(true);return;/ 判断 ip 的 79 位是否为 int 型try ip3=Integer.parseInt(ThreadScan.fromip3.getText(); c

33、atch(NumberFormatException e)ThreadScan.DLGINFO.setText( 错误的 ip!);ThreadScan.DLGError.setVisible(true);return;/判断起始 ip 的最后 4 位是否为 int 型try ipstart=Integer.parseInt(ThreadScan.fromip4.getText(); catch(NumberFormatException e)ThreadScan.DLGINFO.setText( 错误的 ip!);ThreadScan.DLGError.setVisible(true);re

34、turn;/判断目标 ip 的最后 4 位是否为 int 型try ipend=Integer.parseInt(ThreadScan.toip.getText(); catch(NumberFormatException e)ThreadScan.DLGINFO.setText( 错误的目标 ip!);ThreadScan.DLGError.setVisible(true);return;/ 判断起始 ip 是否正确/ 判断条件:大于 0 且小于等于 255if(ip1255|ip2255|ip3255|ipstart255) ThreadScan.DLGINFO.setText( ip 地

35、址为 0-255 的整数 ! ); ThreadScan.DLGError.setVisible(true);return;elseTCPThread.ip1 = ip1;TCPThread.ip2 = ip2;TCPThread.ip3 = ip3;TCPThread.ipstart = ipstart;/ 判断目标 ip 是否正确/ 判断条件:大于 0 且小于等于 255if(ipend255)ThreadScan.DLGINFO.setText( 目标 ip 地址为 0-255 的整数 ! );ThreadScan.DLGError.setVisible(true);return;els

36、eTCPThread.ipend = ipend;ipaddress = + ip1 + ip2 + ip3 + ipstart;/* 判断 ip 地址的有效性*/tryTCPThread.hostAddress=InetAddress.getByName(ipaddress);catch(UnknownHostException e)ThreadScan.DLGINFO.setText( 错误的 IP 或地址不可达 ! );ThreadScan.DLGError.setVisible(true);return;/根据主机名进行端口扫描if(ThreadScan.radioHost.isSel

37、ected()TCPThread.type = 1;/*判断主机名称的有效性*/tryTCPThread.hostAddress=InetAddress.getByName(ThreadScan.hostname.getText();catch(UnknownHostException e)ThreadScan.DLGINFO.setText( 错误的域名或地址不可达 ! );ThreadScan.DLGError.setVisible(true);return;/*判断端口号的有效性*/tryminPort=Integer.parseInt(ThreadScan.minPort.getTex

38、t();maxPort=Integer.parseInt(ThreadScan.maxPort.getText();maxThread=Integer.parseInt(ThreadScan.maxThread.getText();catch(NumberFormatException e)ThreadScan.DLGINFO.setText( 错误的端口号或线程数 ! 端口号和线程数必须为整数 !);ThreadScan.DLGError.setVisible(true);return;/*判断最小端口号的有效范围 *判断条件:大于 0 且小于 65535,最大端口应大于最小端口 */if(

39、minPort65535 | minPortmaxPort) ThreadScan.DLGINFO.setText( 最小端口必须是 0-65535 并且小于最大端口的 整数 !);ThreadScan.DLGError.setVisible(true);return;else TCPThread.MIN_port=minPort;/*判断最大端口号的有效范围 *判断条件:大于 0 且小于 65535,最大端口应大于最小端口 */if(maxPort65535 | maxPortminPort) ThreadScan.DLGINFO.setText( 最大端口必须是 0-65535 并且大于最

40、小端口的 整数 !);ThreadScan.DLGError.setVisible(true);return;else TCPThread.MAX_port=maxPort;/*判断线程数量的有效范围 *判断条件:大于 1 且小于 200 */if(maxThread200) ThreadScan.DLGINFO.setText( 线程数为 1-200 的整数 ! ); ThreadScan.DLGError.setVisible(true);return;ThreadScan.Result.append(”线程数+ThreadScan.maxThread.getText()+n);/启动线程

41、for(int i=0;imaxThread;i+) new TCPThread(T + i,i).start();/* *实现错误提示框中的“确定”按钮功能 */class OKAction implements ActionListenerpublic void actionPerformed (ActionEvent e) ThreadScan.DLGError.dispose();多线程类文件:import .*;import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public

42、class TCPThread extends Thread public static InetAddress hostAddress; /最小的端口号public static int MIN_port;/最大的端口号public static int MAX_port;/线程总数private int threadnum;/查询方式:0为ip; 1为主机名 public static int type;/ip 地址前 3 位 public static int ip1;/ip 地址 46 位 public static int ip2;/ip 地址 79 位public static i

43、nt ip3;/起始 ip 地址的最后 4 位 public static int ipstart;/结束 ip 地址的最后 4 位 public static int ipend;/完整的 ip 地址 public static String ipAll;/扫描的主机名称或 ipString hostname = ; /端口的类别String porttype = 0;/* 构造函数*/public TCPThread(String name,int threadnum) super(name);this.threadnum = threadnum;/* 运行函数*/public void

44、run() /ip 地址int h = 0;/端口号int i = 0;Socket theTCPsocket;/根据 ip 地址进行扫描 if(type = 0)/ip 地址循环扫描for(h = ipstart; h =ipend; h+)/组成完整的 ip 地址ipAll = + ip1 + . + ip2 + . + ip3 + . + h; hostname = ipAll;try/在给定主机名的情况下确定主机的IP 地址hostAddress=InetAddress.getByName(ipAll);catch(UnknownHostException e)+=/不同的端口循环扫描

45、for (i = MIN_port+threadnum; i MAX_portInteger.parseInt(ThreadScan.maxThread.getText(); iInteger.parseInt(ThreadScan.maxThread.getText()trytheTCPsocket=new Socket(hostAddress,i); theTCPsocket.close();ThreadScan.Result.append(hostname+:+i);/判断端口的类别 switch(i) case 21: porttype = (FTP); break;case 23:porttype = (TELNET); break;case 25: porttype = (SMTP); break;case 80: porttype = (HTTP); break;case 110: porttype = (POP); break;case 139:porttype = (netBIOS); break;case 1433: porttype = (

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论