閱讀197 返回首頁    go 阿裏雲 go 技術社區[雲棲]


linux下的shell命令的編寫,以及java如何調用linux的shell命令(java如何獲取linux上的網卡的ip信息)

程序員都很懶,你懂的!

最近在開發中,需要用到服務器的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();//獲得本機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
 進行共同學習!

最後更新:2017-04-03 12:56:43

  上一篇:go 線段樹-點修改-hdoj-1754
  下一篇:go Android騰訊微博之1.0 Oauth認證