Linux下java获取本地网络信息
程序员文章站
2022-07-09 19:15:55
Linux下java获取本地网络信息实现机制:通过java调用perl程序来获取Linux本地网络信息。结果可以直接通过./net-statistics.pl验证上码:NetWorkInfo.javastatic class NetWorkInfo { public static void main(String[] args) { System.out.println(getNetworkStatistics()); }...
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";
-
本文地址:https://blog.csdn.net/u010655288/article/details/108036820
上一篇: Java面试题集