linux下的shell命令的编写,以及java如何调用linux的shell命令(j

程序员都很懒,你懂的!

最近在开发中,需要用到服务器的ip和mac信息。但是服务器是架设在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();//获得本机IPString 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 exportnetwork=`cat /nac/config/nac_sys.conf | grep "manager"|awk '{print $2}'`#get net export local ipifconfig $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进行共同学习!在乎的是沿途的风景以及看风景的心情,让心灵去旅行!

linux下的shell命令的编写,以及java如何调用linux的shell命令(j

相关文章:

你感兴趣的文章:

标签云: