zoukankan      html  css  js  c++  java
  • 执行shell命令-JAVA

    //执行shell命令和脚本

    public
    static String getIPByShell(String inetType) { String ifip = ""; StringBuilder sb = new StringBuilder(); String command = "/usr/sbin/ifconfig " + inetType; try { Process p = Runtime.getRuntime().exec(command); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); p.destroy(); } catch (IOException e) { e.printStackTrace(); } String ifinfo = sb.toString(); if (ifinfo.indexOf("netmask") == -1) { logger.warn("command=[{}] 没有返回 [netmask]相关的信息", command); } else { // 从netmask处截取,然后以空格拆分,最后一个值即为内网IP String[] ifinfoArr = ifinfo.substring(0, ifinfo.indexOf("netmask")).split(" "); ifip = ifinfoArr[ifinfoArr.length - 1]; } return ifip; }
    /**
         * 判断操作系统是否 win 或者 mac 等个人电脑
         */
        public static boolean isPC(){
            String os = System.getProperty("os.name");
            if(os.toLowerCase().startsWith("win") || os.toLowerCase().startsWith("mac")){
                return true;
            }
            return false;
        }
    /**
         * 得到公网IP
         * 
         */
        public static String getInternetIp2() {
            String ip = "";
            String chinaz = "http://ip.chinaz.com";
    
            StringBuilder inputLine = new StringBuilder();
            String read = "";
            URL url = null;
            HttpURLConnection urlConnection = null;
            BufferedReader in = null;
            try {
                url = new URL(chinaz);
                urlConnection = (HttpURLConnection) url.openConnection();
                in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
                while ((read = in.readLine()) != null) {
                    inputLine.append(read + "
    ");
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
            Pattern p = Pattern.compile("\<dd class\="fz24">(.*?)\<\/dd>");
            Matcher m = p.matcher(inputLine.toString());
            if (m.find()) {
                String ipstr = m.group(1);
                ip = ipstr;
            }
            return ip;
        }
     
  • 相关阅读:
    数据结构学习8——二叉树的销毁
    单链表的反向
    LNK4098: 默认库“MSVCRT”与其他库的使用冲突
    动态链接库(VC_Win32)
    注册表操作(VC_Win32)
    消息钩子与定时器(VC_Win32)
    套接字编程(VC_Win32)
    线程概述,优先级,睡眠,创建及终止(VC_Win32)
    进程通信(VC_Win32)
    进程概述及创建,终止(VC_Win32)
  • 原文地址:https://www.cnblogs.com/wanhua-wu/p/13601263.html
Copyright © 2011-2022 走看看