java linux修改网卡信息,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();

}

}

执行效果图:

07b27dd456d3a90200ce395747256703.png

好吧,看看上面的打印,你就知道了,有多个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

进行共同学习!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值