導航:首頁 > 編程語言 > linux獲取本機ipjava

linux獲取本機ipjava

發布時間:2022-07-29 16:25:14

① 在linux下用 java 輸出 InetAddress.getLocalHost();的結果是什麼

InetAddress.getLocalHost()在Linux下其實是非常不給力的……你可以試一下,雖然這個方法在Windows下可以正常工作,但是在Linux下只會返回127.0.0.1……
因為該方法在Linux下的具體實現僅僅是讀取/etc/hosts中的配置內容,所以在Linux下不要嘗試用這個方法來獲得本機的IP地址。如果真要獲取的話,可以參考這個網頁:
http://www.javaeye.com/topic/169889
或者是這個:
http://wjason.javaeye.com/blog/206320

② java如何查詢本機ip地址和mac地址

Java中可以使用程序來獲取本地ip地址和mac地址,使用InetAddress這個工具類,示例如下:

importjava.net.*;
publicclassNetInfo{
publicstaticvoidmain(String[]args){
newNetInfo().say();
}
publicvoidsay(){
try{
InetAddressi=InetAddress.getLocalHost();
System.out.println(i);//計算機名稱和IP
System.out.println(i.getHostName());//名稱
System.out.println(i.getHostAddress());//只獲得IP
}
catch(Exceptione){e.printStackTrace();}
}
}

也可以通過命令行窗口來查看本地ip和mac地址,輸入命令:ipconfig。

③ java獲取linux ip

上面的方法是枚舉全部的IP地址,,,,,,,,,,樓主你測試的結果是怎麼個不行法。。。

④ linux怎麼查看IP地址

ifconfig用於獲取網卡配置與網路狀態等信息:格式為"ifconfig [網路設備] [參數]"。
作為一名合格的運維人員,為了更好的了解咱們的Linux伺服器,您必須具備快速查看系統狀態的能力,所以接下來咱們會逐個來學習下對於網卡網路、系統內核、系統負載、內存使用情況、當前登錄用戶、歷史登錄記錄、命令執行記錄以及救援診斷命令的使用方法,都是超級實用的。
查看本機當前的網卡配置與網路狀態等信息,咱們主要就是看每段開頭的網卡名稱、inet參數後面的IP地址、ether參數後面的物理mac地址以及RX、TX的接收與發送數據包的大小linuxprobe.com/chapter-02.html#24:
[root@linuxprobe ~]# ifconfig
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fec4:a409 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c4:a4:09 txqueuelen 1000 (Ethernet)
RX packets 36 bytes 3176 (3.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 38 bytes 4757 (4.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 386 bytes 32780 (32.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 386 bytes 32780 (32.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

⑤ Linux 怎麼獲取本機IP

打開終端,輸入ifconfig,要想改變IP地址,使用命令:sudo ifconfig eth0 192.168.xxx.xxx
打開超級終端的快捷鍵是ctrl+alt+t,請採納。。。

⑥ linux 下獲取本地ip地址後 怎樣

在進行Linux網路編程時,經常用到本機IP地址。本文羅列一下常見方法,以備不時之需。
獲取本機IP地址,是一個相當靈活的操作,原因是網路地址的設置非常靈活而且都是允許用戶進行個性化設置的。比如一台計算機上可以有多塊物理網卡或者虛擬網卡,一個網卡上可以綁定多個IP地址,用戶可以為網卡設置別名,可以重命名網卡。用戶計算機所在網路拓撲結構未知,主機名設置是一個可選項,並且同樣可以為一個計算機綁定多個主機名等,這些信息都會有影響。脫離了網路連接,單獨的網路地址沒有任何意義。編程中遇到必須獲取計算機IP的場景,應該考慮將這一選項放到配置文件中,由用戶自己來設置。
參考網路和書本,編程獲取本機IP地址大約有以下幾種方法。
方法一:ioctl()獲取本地IP地址
Linux 下 可以使用ioctl()函數以及結構體 struct ifreq和結構體struct ifconf來獲取網路介面的各種信息。
具體過程是先通過ictol獲取本地的所有介面信息,存放到ifconf結構中,再從其中取出每個ifreq表示的ip信息(一般每個網卡對應一個IP地址,如:」eth0…、eth1…」)。
先了解結構體 struct ifreq和結構體struct ifconf:
//ifconf通常是用來保存所有介面信息的
//if.h
struct ifconf
{
int ifc_len; /* size of buffer */
union
{
char *ifcu_buf; /*input from user->kernel*/
struct ifreq *ifcu_req; /* return from kernel->user*/
} ifc_ifcu;
};

#define ifc_buf ifc_ifcu.ifcu_buf /*buffer address */
#define ifc_req ifc_ifcu.ifcu_req /*array of structures*/

//ifreq用來保存某個介面的信息
//if.h
struct ifreq {
char ifr_name[IFNAMSIZ];
union {
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
short ifru_flags;
int ifru_metric;
caddr_t ifru_data;
} ifr_ifru;
};
#define ifr_addr ifr_ifru.ifru_addr
#define ifr_dstaddr ifr_ifru.ifru_dstaddr
#define ifr_broadaddr ifr_ifru.ifru_broadaddr

⑦ linux 查看ip地址的命令

linux如何查看ip地址,可以使用ifconfig命令來快速查看ip地址。

1、首先在電腦中打開linux客戶端,點擊連接linux按鈕。

⑧ linux下使用perl獲取本機ip的幾種方法介紹

第一種辦法,最簡單的,
調用 shell:
$ip = `ifconfig eth0|grep -oE '([0-9]{1,3}\.?){4}'|head -n 1`;

註:這里輸入是固定的,所以簡單的 [0-9]{1,3} 了,如果是在 web 程序等地方驗證 ip,需要更嚴謹!
或者
$ip = `ifconfig eth0|awk -F: '/inet addr/{split($2,a," ");print a[1];exit}'`;

好吧,這樣顯得太不 perl 了,而且頻繁的調用外部 shell 不太好

第二種:
open FH,"ifconfig eth0|";
while(){
last unless /inet addr:((\d{1,3}\.?){4})/;
print $1;
}
看起來稍微 perl 了一些,雖然實質跟上面的調用 shell 和 grep 法是一樣的。

第三種,更 perl 一點,純粹讀文件:
open FH,'<','/etc/sysconfig/network-scripts/ifcfg-eth0';
while(){
next unless /IPADDR\s*=\s*(\S+)/;
print $1;
}

進一步的,如果不一定 rh 系,還要去讀 /etc/issue ,
確定網路配置文件到底是 /etc/sysconfig/network-script/ifcfg-eth0
還是 /etc/network/interfaces 還是其他,然後根據不同發行版寫不同的處理方法……額,
這是打算自己寫模塊么?

好吧,大家來充分體會 CPAN 的魅力,去 search 一下,
找到一把 Sys::HostIP、Sys::HostAddr、Net::Inetface 等模塊。

第四種:
use Sys::HostAddr;
my $interface = Sys::HostAddr->new(ipv => '4', interface => 'eth0');
print $interface->main_ip;

不過進去看看pm文件,汗,這幾個模塊都是調用ifconfig命令,不過是根據發行版的不同進行封裝而已。
還有辦法么?還有,看

第五種:
perl -MPOSIX -MSocket -e 'my $host = (uname)[1];print inet_ntoa(scalar gethostbyname($host))';
不過有童鞋說了,這個可能因為hostname的原因,導致獲取的都是127.0.0.1……

那麼最後還有一招。通過 strace ifconfig 命令可以看到,
linux 實質是通過 ioctl 命令完成的網路介面 ip 獲取。那麼,我們也用 ioctl 就是了!

第六種如下:
#!/usr/bin/perl
use strict;
use warnings;
use Socket;
require 'sys/ioctl.ph';
sub get_ip_address($) {
my $pack = pack("a*", shift);
my $socket;
socket($socket, AF_INET, SOCK_DGRAM, 0);
ioctl($socket, SIOCGIFADDR(), $pack);
return inet_ntoa(substr($pack,20,4));
};
print get_ip_address("eth0");

這樣的好處,就是只調用了核心模塊,在分發腳本時,不用連帶安裝其他模塊。

⑨ 請教:linux下怎麼得到本機實際ip

打開終端,輸入ifconfig,要想改變IP地址,使用命令:sudo ifconfig eth0 192.168.xxx.xxx 打開超級終端的快捷鍵是ctrl+alt+t,請採納。。。

⑩ 幫我用java寫個能獲取主機IP,主機名的窗體

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* @className: SystemTool
* @description: 與系統相關的一些常用工具方法. 目前實現的有:獲取MAC地址、IP地址、主機名
* @author: 笑遍世界
* @createTime: 2010-11-13 下午08:03:44
*/
public class SystemTool {

/**
* 獲取當前操作系統名稱.
* return 操作系統名稱 例如:windows xp,linux 等.
*/
public static String getOSName() {
return System.getProperty("os.name").toLowerCase();
}

/**
* 獲取unix網卡的mac地址.
* 非windows的系統默認調用本方法獲取.如果有特殊系統請繼續擴充新的取mac地址方法.
* @return mac地址
*/
public static String getUnixMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ifconfig eth0");// linux下的命令,一般取eth0作為本地主網卡 顯示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("hwaddr");// 尋找標示字元串[hwaddr]
if (index >= 0) {// 找到了
mac = line.substring(index +"hwaddr".length()+ 1).trim();// 取出mac地址並去除2邊空格
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}

return mac;
}

/**
* 獲取widnows網卡的mac地址.
* @return mac地址
*/
public static String getWindowsMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ipconfig /all");// windows下的命令,顯示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
index = line.toLowerCase().indexOf("physical address");// 尋找標示字元串[physical address]
if (index >= 0) {// 找到了
index = line.indexOf(":");// 尋找":"的位置
if (index>=0) {
mac = line.substring(index + 1).trim();// 取出mac地址並去除2邊空格
}
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}

return mac;
}

/**
* @return 本機主機名
*/
public static String getHostName() {
InetAddress ia = null;
try {
ia = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (ia == null ) {
return "some error..";
}
else
return ia.getHostName();
}

/**
* @return 本機IP 地址
*/
public static String getIPAddress() {
InetAddress ia = null;
try {
ia = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (ia == null ) {
return "some error..";
}
else
return ia.getHostAddress();
}

/**
* 測試用的main方法.
*
* @param argc
* 運行參數.
*/
public static void main(String[] argc) {
String os = getOSName();
System.out.println("OS Type:"+os);
if(os.startsWith("windows")){
//本地是windows
String mac = getWindowsMACAddress();
System.out.println("MAC Address:"+mac);
}else{
//本地是非windows系統 一般就是unix
String mac = getUnixMACAddress();
System.out.println(mac);
}
System.out.println("HostName:"+getHostName());
System.out.println("IPAddress:"+getIPAddress());

閱讀全文

與linux獲取本機ipjava相關的資料

熱點內容
戰地聯盟3解壓密碼 瀏覽:803
s型命令 瀏覽:23
php年薪5年 瀏覽:67
如何上網上設個人加密賬戶 瀏覽:44
linux打開ssh服務 瀏覽:78
微信位置可以加密嗎 瀏覽:470
演算法蠻力法 瀏覽:438
隨機排練命令 瀏覽:147
python多進程並發 瀏覽:41
安卓軟體安裝如何躲避安全檢測 瀏覽:647
奇幻潮翡翠台源碼百度雲盤 瀏覽:187
什麼軟體可以免費pdf轉word 瀏覽:15
php正則表達式大全 瀏覽:394
androidntp時間 瀏覽:299
輪機長命令簿英文 瀏覽:148
oppo鈴聲設置被加密怎麼處理 瀏覽:548
粵苗app圖形驗證碼怎麼填 瀏覽:899
管家婆架設雲伺服器 瀏覽:254
php的登錄界面代碼 瀏覽:997
php開發客戶端 瀏覽:998