① 在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());