目录

  • 一、软件功能与截图
    • 多Excel文档批量导入IP信息
      • 1.文件支持选择多个Excel文档
      • 2.Excel文件的表头要求
    • IP信息Ping结果列表展示
  • 二、源码分享与设计
    • Maven-Pom依赖
    • Excel数据导入-基于POI包
    • IP地址Ping方法-
    • GUI界面-Java Swing-程序入口

一、软件功能与截图

做一款面向网络运维人员的IP通断状态检测小软件,核心是通过运行Ping命令拿到IP网络状态,目前有一个简陋UI界面,支持批量导入Excel格式的IP数据,然后自动检测网络通断。软件本体是jar包,可以通过exe4j工具打包为Windows PC机下运行的EXE程序,也可以运行到Linux操作系统。
该文档不定时更新。
未来打算支持功能点有:

  • 将支持单个IP做网络通断测试,即每一个IP旁边放置一个按钮
  • 将支持将IP检测结果表格导出为PDF或图片,方便分享数据
  • 将支持自动不定时对IP地址做检测,动态更新检测结果
  • 计划增加多线程,缩短响应时间

多Excel文档批量导入IP信息

1.文件支持选择多个Excel文档

2.Excel文件的表头要求

表头名字随便取,顺序必须与截图保持一致

IP信息Ping结果列表展示

二、源码分享与设计

Maven-Pom依赖

<!--  POI Excel Jar--><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.2</version></dependency>

Excel数据导入-基于POI包

  • 负责解析Excel数据为Java对象PingInfoInputDTO的方法类:MyExcelServiceImpl
public class MyExcelServiceImpl implements  EasyExcelService {@Overridepublic List<PingInfoInputDTO> readExcel(File file) {try {//            HSSFWorkbook workBook = new HSSFWorkbook(new FileInputStream(new File(file.getAbsolutePath())));HSSFWorkbook workBook = new HSSFWorkbook(new FileInputStream(file));//页int sheetNum = workBook.getNumberOfSheets();DataFormatter cellDataFormatter = new DataFormatter();List<PingInfoInputDTO> pingInfoList = new ArrayList<>(300);for( int i=0; i<sheetNum; i++ ){HSSFSheet sheetData =  workBook.getSheetAt(i);//行:每行数据对应一个对象int lastRowNum = sheetData.getLastRowNum();for( int j=1; j<lastRowNum ; j++){//跳过首行表头,j=1HSSFRow rowData = sheetData.getRow(j);//单元格short lastCellNum = rowData.getLastCellNum();PingInfoInputDTO pingDTO = new PingInfoInputDTO();int k = 0;//IpNo数据编号pingDTO.setIpNo(cellDataFormatter.formatCellValue(rowData.getCell(k++)));//IP地址别名pingDTO.setIpAliasName(cellDataFormatter.formatCellValue(rowData.getCell(k++)));//IP地址pingDTO.setIpAddress(cellDataFormatter.formatCellValue(rowData.getCell(k++)));//Ping超时pingDTO.setPingTimeout(cellDataFormatter.formatCellValue(rowData.getCell(k++)));//最大Ping次数pingDTO.setPingMaxCount(cellDataFormatter.formatCellValue(rowData.getCell(k++)));pingInfoList.add(pingDTO);}}workBook.close();return pingInfoList;} catch (FileNotFoundException e) {e.printStackTrace();return new ArrayList<>();} catch (IOException e) {e.printStackTrace();return new ArrayList<>();}}
}
  • 接口层 EasyExcelService.java
public interface EasyExcelService {List<PingInfoInputDTO> readExcel(File file);
}
  • POJO:PingInfoInputDTO
public class PingInfoInputDTO {//数据标识和序号private String ipNo;//Ping 目标地址private String ipAddress;//Ping地址别名private String ipAliasName;//Ping超时设置,默认1000msprivate String pingTimeout;//Ping次数上限private String pingMaxCount;public String getIpAddress() {return ipAddress;}public void setIpAddress(String ipAddress) {this.ipAddress = ipAddress;}public String getPingTimeout() {return pingTimeout;}public void setPingTimeout(String pingTimeout) {this.pingTimeout = pingTimeout;}public String getPingMaxCount() {return pingMaxCount;}public void setPingMaxCount(String pingMaxCount) {this.pingMaxCount = pingMaxCount;}public String getIpAliasName() {return ipAliasName;}public void setIpAliasName(String ipAliasName) {this.ipAliasName = ipAliasName;}public String getIpNo() {return ipNo;}public void setIpNo(String ipNo) {this.ipNo = ipNo;}@Overridepublic String toString() {return "PingInfoInputDTO{" +"ipNo='" + ipNo + '\'' +", ipAddress='" + ipAddress + '\'' +", ipAliasName='" + ipAliasName + '\'' +", pingTimeout='" + pingTimeout + '\'' +", pingMaxCount='" + pingMaxCount + '\'' +'}';}
}

IP地址Ping方法-

  • 实现类
public class PingServiceImpl implements PingService {public PingInfoOutputDTO ping(PingInfoInputDTO infoInputDTO) {PingInfoOutputDTO outputDTO = new PingInfoOutputDTO();outputDTO.setInputDTO(infoInputDTO);outputDTO.setTargetIp(infoInputDTO.getIpAddress());BufferedReader in = null;String pingCommand = null;Runtime r = Runtime.getRuntime();String osName = System.getProperty("os.name");if(osName.contains("Windows")){//Windows Style: ping www.baidu.com -n 3pingCommand = "ping " + infoInputDTO.getIpAddress() + " -n " + infoInputDTO.getPingMaxCount();}else{//Linux Style: ping -c 10 www.baidu.compingCommand = "ping " + " -c " + infoInputDTO.getPingMaxCount() + "  " +  infoInputDTO.getIpAddress();}try {//ProcessSystem.out.println("To process command:" + pingCommand);Process p = r.exec(pingCommand);if (p == null) {outputDTO.setPingStatus(false);outputDTO.setPingRes("System Error!");outputDTO.setPingResponseTime(new Date());}//Checkin = new BufferedReader(new InputStreamReader(p.getInputStream()));int connectedCount = 0;String line = null;while ((line = in.readLine()) != null) {if(osName.contains("Windows")){if(line.contains("TTL=")){connectedCount++;}}else{if(line.contains("ttl=")){connectedCount++;}}}System.out.println("IP: " + infoInputDTO.getIpAddress() +  ",Ping Success Count:" +connectedCount);if(connectedCount >= 1){outputDTO.setPingResponseTime(new Date());outputDTO.setPingStatus(true);outputDTO.setPingRes("连通");}else{outputDTO.setPingResponseTime(new Date());outputDTO.setPingStatus(false);outputDTO.setPingRes("故障");}return outputDTO;} catch (Exception ex) {ex.printStackTrace();return null;} finally {try {in.close();} catch (IOException e) {e.printStackTrace();return null;}}}
}
  • 接口定义
public interface PingService {PingInfoOutputDTO ping(PingInfoInputDTO infoInputDTO);}

GUI界面-Java Swing-程序入口

本类同时也是程序的入口

public class FrameUITest {public static void main(String[] args) {//校验通过的文件类型List<File> VALID_FILE_LIST = new ArrayList<File>(6);//校验失败的文件类型List<File> INVALID_FILE_LIST = new ArrayList<File>(6);int frameWidth = 700;int frameHeight = 500;SimpleDateFormat simpleDateFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String appTitle = "小平头网络IP地址通讯状态检测工具PingTow2.0";//统一设置字体InitGlobalFont(new Font("alias", Font.PLAIN, 18));JFrame frame = new JFrame(appTitle);frame.setSize(frameWidth, frameHeight);frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);frame.setLocationRelativeTo(null);// 创建内容面板,指定使用 流式布局JPanel panel = new JPanel(new FlowLayout());
//        JPanel panel = new JPanel(new GridLayout(3,1));//显示用户所选文件名称JLabel fileNameLabel = new JLabel();fileNameLabel.setText("您选择的文件是:_______");fileNameLabel.setFont(new Font(null, Font.PLAIN, 15));fileNameLabel.setSize(300,80);panel.add(fileNameLabel);//用于文件上传的按钮JButton fileUploadBtn = new JButton("       上传Excel文件      ");fileUploadBtn.setSize(400,80);fileUploadBtn.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//点击按钮后打开文本框JFileChooser chooser = new JFileChooser();//开启文件多选chooser.setMultiSelectionEnabled(true);/** 过滤文件类型 * */FileNameExtensionFilter filter = new FileNameExtensionFilter("","xls");chooser.setFileFilter(filter);int returnVal = chooser.showOpenDialog(fileUploadBtn);if (returnVal == JFileChooser.APPROVE_OPTION) {//选择的文件File[] fileArr = chooser.getSelectedFiles();if (fileArr == null || fileArr.length == 0) {JOptionPane.showMessageDialog(new JDialog(), "错误!请正确选择文件.");return;}//循环校验所选文件的类型StringBuilder fileNameBuilder = new StringBuilder();for (File item : fileArr) {//判断是否有文件为xls或xlsxString fileName = item.getName();String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);if ((prefix.equals("xls") || prefix.equals("xlsx"))) {VALID_FILE_LIST.add(item);fileNameBuilder.append(fileName);} else {INVALID_FILE_LIST.add(item);}}//更新UI界面用户所选文件fileNameLabel.setText("您所选文件是:" + fileNameBuilder.toString());System.out.println("有效文件:" + VALID_FILE_LIST + ",无效文件:" + INVALID_FILE_LIST);//检测IP//Excel File -> PingInfoInputDTOList<PingInfoInputDTO> pingInfoList = new ArrayList<>(100);EasyExcelService excelService = new MyExcelServiceImpl();if(CollectionUtils.isNotEmpty(VALID_FILE_LIST) && !CollectionUtils.sizeIsEmpty(VALID_FILE_LIST)){for (File file : VALID_FILE_LIST) {List<PingInfoInputDTO> list = excelService.readExcel(file);pingInfoList.addAll(list);}}//检测IPList<PingInfoOutputDTO> outPutList = new ArrayList<>(100);PingService pingService = new PingServiceImpl();if(CollectionUtils.isNotEmpty(pingInfoList) && !CollectionUtils.sizeIsEmpty(pingInfoList)){for (PingInfoInputDTO item : pingInfoList) {PingInfoOutputDTO out = pingService.ping(item);outPutList.add(out);}}//放一个列表展示解析结果//转化为二维数组String[][] tableData = new String[outPutList.size()][5];int i=0;for (PingInfoOutputDTO item : outPutList) {PingInfoInputDTO inputDTO = item.getInputDTO();tableData[i][0] = inputDTO.getIpNo();tableData[i][1] = inputDTO.getIpAddress();tableData[i][2] = inputDTO.getIpAliasName();tableData[i][3] = item.getPingRes();tableData[i][4] = simpleDateFormat.format(item.getPingResponseTime());i++;}Object[] columnNames = {"序号", "   IP地址   ","    IP简称    ", " 网络状态  ", "   操作时间   "};// 创建一个表格,指定 所有行数据 和 表头JTable table = new JTable(tableData, columnNames);table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);table.setForeground(new Color(100,200,80));table.setShowGrid(true);table.setRowHeight(40);table.setPreferredScrollableViewportSize(new Dimension((int)0.8*frameWidth,(int)0.8*frameHeight));// 把 表头 添加到容器顶部(使用普通的中间容器添加表格时,表头 和 内容 需要分开添加)panel.add(table.getTableHeader(), BorderLayout.NORTH);// 把 表格内容 添加到容器中心panel.add(table, BorderLayout.CENTER);}}});panel.add(fileUploadBtn);frame.setContentPane(panel);frame.setVisible(true);}/*** 统一设置字体,父界面设置之后,所有由父界面进入的子界面都不需要再次设置字体*/private static void InitGlobalFont(Font font) {FontUIResource fontRes = new FontUIResource(font);for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {Object key = keys.nextElement();Object value = UIManager.get(key);if (value instanceof FontUIResource) {UIManager.put(key, fontRes);}}}
}

继续你的创作。

【网络运维】小平头PingTow网络IP导入检测工具软件开发源代码分享相关推荐

  1. vpc经典网络区别_网络运维与管理 经典网络还是VPC如何选择

    网络运维与管理 经典网络还是VPC如何选择 本文标签: 服务器安全 游戏防护 云防护 cc攻击防护 ddos攻击防护 服务器被攻击 数据库 运维 网络运维与管理 经典网络还是VPC如何选择, 1.VP ...

  2. 2021AIOps挑战赛|基于移动通信网络运维中的多指标时空序列预测

    2021国际AIOps挑战赛决赛暨AIOps创新高峰论坛于2021年5月13日在北京成功举行.本文根据中国移动研究院首席科学家.人工智能与智慧运营中心总经理冯俊兰博士现场发言纪要整理而成. 演讲内容 ...

  3. 什么是网络运维工程师? 就业前景好吗?

    互联网发展日渐成熟,所有企业都依赖于网络管理,有企业的地方就需要网络运维工程师. 在一般人的概念里,网络工程师不过就是通过拨号上网,发个Email,聊聊天,计算机组装与维护,组建局域网就以为是网络工程 ...

  4. 中国移动研究院冯俊兰:基于移动通信网络运维中的多指标时空序列预测

    这和我当年做的工作就很像了. 转载自:https://mp.weixin.qq.com/s/veqH753nxWW0Hr0PDB1uSg 中国移动研究院冯俊兰:基于移动通信网络运维中的多指标时空序列预 ...

  5. 网络回溯分析技术八大应用之运维评估 网络运维的真正价值

    提示:<科来网络回溯分析系统​>V4.0已经正式发布啦!详见科来软件官方网站                      网络回溯分析技术八大应用之运维评估 网管人员最尴尬的地方在于,当你的 ...

  6. dataguard日志传输模式解析_网络运维基础 日志审计

    点击上面蓝字关注我们 综合日志审计平台 综合日志审计平台通过集中采集信息系统中的系统安全事件.用户访问记录.系统运行日志.系统运行状态等各类信息,经过规范化.过滤.归并和告警分析等处理后,以统一格式的 ...

  7. 智和信通:立足数字化、智能化、可视化,打造一站式制造业网络运维平台

    新时代下,云.大.物.移.智等新一代信息技术得到快速发展与大范围应用,一方面推动传统制造业改造升级向智能工厂转型,促进海量信息互通共享,但另一方面,也产生了一系列网络运维及安全难题.在"云制 ...

  8. Python在网络运维中的应用与分析

    目前Python是新兴技术编程语言的主流之一,随着互联网技术的不断发展,计算机网络的应用范围也随之增加,网络运维自动化和智能化需求越来越高,Python编程语言逐渐盛行,该程序设计语言简单灵活,拥有庞 ...

  9. 网络运维超融合服务器,超融合解读 | 极简体验!四大武器让数据中心省心无忧...

    深信服超融合aCloud基于"软件定义数据中心"的思想,以虚拟化技术为核心,将计算.存储.网络.安全等虚拟资源融合到一台标准x86服务器中,形成一个大资源池.在资源池上部署数据中心 ...

最新文章

  1. win7怎么关闭虚拟机服务器,大师教您Win7系统怎么彻底关闭退出vmware虚拟机的处理要领...
  2. 双linux grub rescue,Grub Rescue修复方法
  3. ABAP选择屏幕的例子
  4. Ajax里的onreadystatechange的作用
  5. 《穿靴子的猫》蓝光720P 1080P高清中英双字 下载
  6. ssl1127-方程的解数【HASH,dfs】
  7. 剑指Offer14-剪绳子12
  8. PHP中功能强大却少使用的函数 -- 为你提供更多的思路
  9. Python 类的特殊成员方法
  10. python vector变量_用Python实现因子分析
  11. Python 标准库 —— json
  12. Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader
  13. 3dmax9中文版注册机
  14. Java核心技术11 | Java IO
  15. 微信小程序——简单的售后服务单
  16. 推荐收藏|9个国外知名免费高清图片素材网站
  17. 有码变高清!一秒还原马赛克
  18. 操作系统抖动现象、网络抖动与延迟、函数抖动之防抖与节流
  19. minikube start命令的国内使用方法
  20. IT计算机行业都有哪些证可以考

热门文章

  1. (23) 基于深度学习框架的出租车OD需求预测应用对比
  2. 错误处理(包括日志记录)
  3. 被“Python之父”称为最强外挂 这个Python库没人敢说不好
  4. IMPERVA- 系统导出
  5. 雅佳5000音色中英文对照表 AKAI EWI5000
  6. 志愿者系统/新时代文明实践平台
  7. 你知道吗?希腊神话人物表
  8. Tomcat框架学习
  9. DSP_基于TMS320F28377D双核芯片和CCS7.40的编程入门
  10. 选择UTON PAD平板,这才是真正的平板电脑,双十二就它了