博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux下的shell命令的编写,以及java怎样调用linux的shell命令(java怎样获取linux上的网卡的ip信息)...
阅读量:7220 次
发布时间:2019-06-29

本文共 2999 字,大约阅读时间需要 9 分钟。

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

近期在开发中,须要用到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 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
 进行共同学习!

你可能感兴趣的文章
USB 驱动架构浅析
查看>>
CSS定位元素居中显示
查看>>
Linux中用户和组中认证库和解析库的文件格式以及默认参数定义文件
查看>>
Windows中如何删除大量文件夹
查看>>
radio多次点击 选中与不选中
查看>>
21天让你成为Horizon View高手—Day19:Horizon View 5.2新功能—Html Ac
查看>>
netty初步认知
查看>>
redis
查看>>
用过的发送邮件的方法。
查看>>
VMWare
查看>>
【论文阅读】Image Super-Resolution Using Deep Convolutional Networks
查看>>
web.xml 中的listener、 filter、servlet 加载顺序及其详解
查看>>
try catch finally
查看>>
Windows编程之作业篇
查看>>
一文了解“Service Mesh(服务网格)”的历史与现在
查看>>
使用 rt_tables 巧妙配置 Linux centos7多网卡多路由实现策略路由
查看>>
Javascript中的RegExp类型
查看>>
Java 基础
查看>>
Spring的代理选择
查看>>
PHP搭建简易留言板
查看>>