Linux下java获取本地网络信息

本文介绍了一种在Linux环境下使用Java调用Perl脚本获取本地网络信息的方法。Perl脚本从网络接口配置文件中读取信息,判断网络模式(DHCP或静态),并收集IP地址、子网掩码、网关、DNS服务器和主机名等关键网络参数。Java程序则负责调用Perl脚本并解析输出,将网络信息整理为Map数据结构返回。

Linux下java+perl获取本地网络信息

  • 实现机制:

    • 通过java调用perl程序来获取Linux本地网络信息。
    • 结果可以直接通过./net-statistics.pl验证
  • 上码:

    • NetWorkInfo.java

      static class NetWorkInfo {
              public static void main(String[] args) {
                  System.out.println(getNetworkStatistics());
              }
      
      
              public static Map getNetworkStatistics() {
                  Map result = new HashMap();
                  String[] rows = execPerl("net-statistics.pl").split("\n");
                  String[] rows1 = rows;
                  int length = rows.length;
      
                  for (int i = 0; i < length; ++i) {
                      String each = rows1[i];
                      String[] keyValuePair = getKeyValueFromRow(each);
                      result.put(keyValuePair[0], keyValuePair[1]);
                  }
      
                  return result;
              }
      
              private static String[] getKeyValueFromRow(String each) {
                  String[] columns = each.split("[\\s,:]");
                  if (columns.length == 2) {
                      return columns;
                  } else if (each.endsWith(":")) {
                      return new String[]{columns[0], ""};
                  } else {
                      return new String[]{"", ""};
                  }
              }
      
              private static String execPerl(String filename) {
                  String cmd = "";
                  String msg = "";
                  String brs = "";
                  cmd = "perl " + filename;
                  try {
                      Process pro = Runtime.getRuntime().exec(cmd);
                      InputStream ins = pro.getInputStream();
                      BufferedReader br = new BufferedReader(new InputStreamReader(ins));
                      while ((brs = br.readLine()) != null) {
                          msg += brs + "\n";
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  return msg;
              }
      
      
          }
      
    • net-statistics.pl

      #!/usr/bin/perl
      
      my $interface = "eth0";
      my $isDhcp = 0;
      my $isStatic = 0;
      
      open(INTF,"</etc/network/interfaces");
      while($line = <INTF>) {
          chomp $line;
          if ($line =~/eth0/) {
      		$interface = "eth0"
      	}
          if ($line =~ /$interface.*dhcp/) {
              $isDhcp = 1;
          } elsif ($line =~ /$interface.*static/) {
              $isStatic = 1;
          }
      }
      close INTF;
      
      my $interface_operstate = `cat /sys/class/net/$interface/operstate`;
      my @ifconfig = split(/\n/,`/sbin/ifconfig $interface 2>&1`);
      
      my $addr = "";
      my $mask = "";
      my $ifconfig_str = join(" ", @ifconfig);
      my $isNetDown = 1;
      
      
      if ($interface_operstate =~ /up/) {
          $isNetDown = 0;
      }
      
      if ($isDhcp) {
          print "Mode:dhcp\n";
      } elsif ($isStatic) {
          print "Mode:static\n";
      } else {
          print "Mode:disabled\n";
      }
      
      if ($isNetDown) {
          print "Net down\n";
      } else {
          print "Net up\n";
      }
      
      
      foreach $line (@ifconfig) {
          chomp $line;
          if($line =~ /inet addr/) {
      	$addr = $line;
      	$addr =~ s/.*inet addr:([^ ]*).*/$1/;
          }
          if($line =~ /Mask/) {
      	$mask = $line;
      	$mask =~ s/.*Mask:([^ ]*).*/$1/;
          }
      }
      
      my @route = split(/\n/,`/sbin/route -n`);
      my $gateway = "";
      foreach $line (@route) {
          chomp $line;
          if($line =~ /^0.0.0.0 /) {
              $gateway = $line;
              $gateway =~ s/^0.0.0.0[ ]*([^ ]*).*/$1/;
          }
      }
      
      open RES, "</etc/resolv.conf";
      my @nameservers;
      while($line = <RES>) {
          chomp $line;
          if($line =~ /nameserver/) {
              my $name=$line;
              $name =~ s/nameserver //;
              push @nameservers, $name;
          }
      }
      close RES;
      
      my @hostname = split(/\n/, `hostname`);
      
      print "Address:$addr\n";
      print "Mask:$mask\n";
      print "Gateway:$gateway\n";
      print "nameserver1:" . $nameservers[0] . "\n";
      print "nameserver2:" . $nameservers[1] . "\n";
      print "Hostname:$hostname[0]\n";
      
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值