zoukankan      html  css  js  c++  java
  • linux下的shell命令的编写,以及java怎样调用linux的shell命令(java怎样获取linux上的网卡的ip信息)

    程序猿都非常懒,你懂的!

    近期在开发中,须要用到server的ip和mac信息。可是server是架设在linux系统上的,对于多网口,在获取ip时就产生了非常大的问题。以下是在windows系统上,java获取本地ip的方法。贴代码:

    package com.herman.test;
    
    import java.net.InetAddress;
    /**
     * @see 获取计算机ip
     * @author Herman.Xiong
     * @date 2014年5月16日 09:35:38
     */
    public class Test {
    	public static void main(String[] args) {
    		test0();
    	}
    	
    	/**
    	 * @see 获取windows系统上的ip(单网卡)
    	 * @author Herman.Xiong
    	 * @date 2014年5月16日 09:36:29
    	 */
    	public static void test0(){
    		try {
    			InetAddress addr = InetAddress.getLocalHost();
    			String ip=addr.getHostAddress().toString();//获得本机IP
    			String address=addr.getHostName().toString();//获得本机名称
    			System.out.println("获得本机IP:"+ip);
    			System.out.println("获得本机名称:"+address);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    获取具体信息,贴代码:

    /**
     * @see 获取windows系统上网卡信息
     * @author Herman.Xiong
     * @date 2014年5月16日 10:17:30
     */
    @SuppressWarnings("unchecked")
    public static void test1(){
    	Enumeration netInterfaces = null;
        try {
            netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
                System.out.println("DisplayName:" + ni.getDisplayName());
                System.out.println("Name:" + ni.getName());
                Enumeration ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    System.out.println("IP:"+ ((InetAddress) ips.nextElement()).getHostAddress());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    执行效果图:

    好吧,看看上面的打印,你就知道了,有多个ip,并且在linux上的情况更复杂。这样的比較麻烦的情况,被我排除了,我使用了一种新的方法,就是linux上的shell脚本。语法代码例如以下:

    #linux中的shell脚本的学习(so easy)
    #1.凝视
    #在进行shell编程时,以#开头的句子表示凝视,直到这一行的结束。
    #我们真诚地建议您在程序中使用凝视。假设您使用了凝视,
    #那么即使相当长的时间内没有使用该脚本,您也能在非常短的时间内明确该脚本的作用及工作原理。
    
    #2变量
    #在其它编程语言中您必须使用变量。在shell编程中,全部的变量都由字符串组成,而且您不须要对变量进行声明。要赋值给一个变量,您能够这样写:
    #变量名=值
    #取出变量值能够加一个美元符号($)在变量前面:
    
    #hello world
    #!/bin/sh
    #对变量赋值:
    hw="hello world"
    # 如今打印变量hw的内容:
    echo "变量hw的值为:"
    echo $hw

    一下是获取ip的shell脚本代码:

    #!/bin/bash
    #get net export
    network=`cat /nac/config/nac_sys.conf | grep "manager"|awk '{print $2}'`
    #get net export local ip
    ifconfig $network|egrep "inet addr:"|cut -d ":" -f2|awk '{print $1}'
    
    脚本vi写好了,随便放一个位置。然后用java调用,一下是java在linux上调用shell脚本的命令:

    /**
     * @see 运行脚本获取linux上的ip
     * @author Herman.Xiong
     * @date 2014年5月16日 10:33:23
     * @return
     */
    public static String execShell(){
    	String ip="";
    	// 获取当前程序的运行进程对象
    	Runtime runtime = Runtime.getRuntime();
    	// 声明处理类对象
    	Process process = null;
    	// 返回行信息
    	// 输入流
    	InputStream is = null;
    	// 字节流
    	InputStreamReader isr = null;
    	// 缓冲流
    	BufferedReader br = null;
    	// 结果
    	try {
    		// 运行PING命令
    		process = runtime.exec("/var/script/herman.sh");
    		// 实例化输入流
    		is = process.getInputStream();
    		// 把输入流转换成字节流
    		isr = new InputStreamReader(is);
    		// 从字节中读取文本
    		br = new BufferedReader(isr);
    		String line="";
    		while ((line = br.readLine()) != null) {
    			ip+=line;
    		}
    		is.close();
    		isr.close();
    		br.close();
    	} catch (IOException e) {
    		e.printStackTrace();
    		runtime.exit(1);
    	}
    	return ip;
    }
    OK,一切大功告成。

    欢迎大家关注我的博客,如有疑问,请加qq群

    135430763
     进行共同学习!





  • 相关阅读:
    二进制位运算
    Leetcode 373. Find K Pairs with Smallest Sums
    priority_queue的用法
    Leetcode 110. Balanced Binary Tree
    Leetcode 104. Maximum Depth of Binary Tree
    Leetcode 111. Minimum Depth of Binary Tree
    Leetcode 64. Minimum Path Sum
    Leetcode 63. Unique Paths II
    经典的递归练习
    案例:java中的基本排序
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3777820.html
Copyright © 2011-2022 走看看