免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6617 | 回复: 11
打印 上一主题 下一主题

请问如何获取本机ip [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-31 15:30 |只看该作者 |倒序浏览
如题
非常感谢!

论坛徽章:
0
2 [报告]
发表于 2006-08-31 15:50 |只看该作者

问题很难?

原帖由 笑面狐 于 2006-8-31 15:30 发表
如题
非常感谢!


这个问题很难?

请大家协助。。。。

论坛徽章:
0
3 [报告]
发表于 2006-08-31 15:52 |只看该作者

用perl获取本机ip很难?

用perl获取本机ip很难?

论坛徽章:
0
4 [报告]
发表于 2006-08-31 16:18 |只看该作者
原帖由 笑面狐 于 2006-8-31 15:52 发表
用perl获取本机ip很难?


不难,为什么你不自己动手试试?

论坛徽章:
0
5 [报告]
发表于 2006-08-31 20:18 |只看该作者
原帖由 笑面狐 于 2006-8-31 15:52 发表
用perl获取本机ip很难?


Hi,
how can one answer your "uncool" question?
I can answer your question with "不难" and "难":

不难!
when you are already familiar with system network administration,
using perl to call system's programm and process its return data,
such as: /sbin/ifconfig eth0, or
cat /etc/network/interfaces, or
cat /etc/hosts, or cat /etc/hostname, ... etc.

when hostname is registed in DNS or your network, you can use
CPAN modules: suchas Net::Domain, Net::hostent, Net::Ping, Socket, ...etc.
here is some demo code: for late to demonstration if possiblw.

难!
when not familiar with both above and your localhost's ip assigned as DHCP!

regards, ulmer

论坛徽章:
0
6 [报告]
发表于 2006-08-31 20:43 |只看该作者
不同的系统有不同的方法……

论坛徽章:
0
7 [报告]
发表于 2006-09-01 11:55 |只看该作者

用java很简单,但现在这个环境我想用perl,维护系统用,perl刚接触

自己解决这个问题了  在perl里调用shell  ifconfig  再提取ip地址 ,但不是我期望的那种方式(像java那种),请高手指点一下

  1. import java.net.*;
  2. public class Test
  3. {
  4. public static void main(String[] args)
  5. {
  6. String addr = null;
  7. try
  8. {
  9. InetAddress destAddr = InetAddress.getLocalHost();
  10. addr = destAddr.getHostAddress();
  11. }
  12. catch (Exception e)
  13. {
  14. System.out.print("error");
  15. }
  16. System.out.print(addr);
  17. }
  18. }
复制代码

论坛徽章:
0
8 [报告]
发表于 2006-09-01 17:15 |只看该作者
原帖由 笑面狐 于 2006-9-1 11:55 发表
用java很简单,但现在这个环境我想用perl,维护系统用,perl刚接触

自己解决这个问题了  在perl里调用shell  ifconfig  再提取ip地址 ,但不是我期望的那种方式(像java那种),请高手指点一下

[code]import  ...


Your question "用perl获取本机ip" is still unclear!!!
Your java code is only get localhost IP or loopback => 127.0.0.1 and same as
shell ifconfig lo, NOT ifconfig eh0!!!
Wenn you really want to get "本机ip" that means qualified network ip (i.e.: 192.168.123.22),
your javacode is a WRONG way to get it!!!

regards, ulmer

-------
perluser is not a blind for java

论坛徽章:
0
9 [报告]
发表于 2006-09-01 17:50 |只看该作者
我在SunOS上 ifconfig -a|awk '/broadcast/ {print $2}' 可以
谁能告诉我在HP-UX上的shell怎么获得本机ip吗?

再有Perl自己可以知道本机ip吗?

论坛徽章:
0
10 [报告]
发表于 2006-09-01 18:29 |只看该作者
Net::Ifconfig::Wrapper
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP