To list all ip address in a machine using perl core modules only - perl-module

how do i list all ip address in a machine using perl core modules only.
The code i have used is as follows. But it is listing only one ip address not all the ip addresses of a machine.
Can i be able to get list of ip addresses from a machine using perl.
I just want only ipv4 addresses.
To get all ip addresses should i use ifconfig command or is there a way perl can give me this with its API.
#!/usr/bin/perl -w
use Socket;
sub getIpAddressbyHost {
my ($hostname,$refAddress) = #_;
return -1 unless ($hostname ne "");
my $address = "";
unless ($hostname =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
eval {
if($hostname ne "") {
my $nformat = inet_aton($hostname);
unless(defined($nformat)) {
print("Error at getIpAddressbyHost for hostname $hostname\n");
return -1;
}
$address = inet_ntoa(inet_aton($hostname));
$$refAddress = $address;
}
};
if ($#) {
print("Error at getIpAddressbyHost for host $hostname : $#");
#$self->addErrorDetails("Error at getIpAddressbyHost for host $hostname : $#");
return -1;
}
}
else {
$$refAddress = $hostname;
}
return 1;
}
my $ipaddr = "Can't find";
print "enter the hostname: \n";
my $input = <STDIN>;
chomp($input);
my $retval = getIpAddressbyHost($input,\$ipaddr);
if($retval == 1){
print("the value of ipaddr: $ipaddr \n");
}

Related

finding proxmox ip address with terraform and Ansible

I'm having this code:
terraform {
required_providers {
proxmox = {
source = "telmate/proxmox"
version = "2.8.0"
}
}
}
provider "proxmox" {
pm_api_url = "https://url/api2/json"
pm_user = "user"
pm_password = "pass"
pm_tls_insecure = true
}
resource "proxmox_vm_qemu" "test" {
count = 1
name = "test-${count.index + 1}"
target_node = "prm01"
clone = "image-here"
guest_agent_ready_timeout = 60
os_type = "cloud-init"
cores = 2
sockets = 1
cpu = "host"
memory = 4048
scsihw = "virtio-scsi-pci"
bootdisk = "scsi0"
disk {
slot = 0
size = "32G"
type = "scsi"
storage = "local-lvm"
iothread = 1
}
network {
model = "virtio"
bridge = "vmbr0"
}
lifecycle {
ignore_changes = [
network,
]
}
}
output "proxmox_ip_address_default" {
description = "Current IP Default"
value = proxmox_vm_qemu.test.*.default_ipv4_address
}
This is created via Ansible playbook. What I'm trying to find is the IP assigned to the machine as I'm running then another playbook to provision that machine. The problem is that I didn't found any solution on how to find the assigned IP address of that machine
Output it is empty!
Any help?

Grammar inference: How can the algorithm Alignment Based Learning help us infering a grammar

I'm a beginner in the grammar inference domain. During my research about it, I found an implementation called Alignment Based Learning (ABL). I have understood what is being done in this implementation (Alignment -> Clustering -> selection) but i don't know what to do next in order to reach my objective which is the grammar induced.
My question is: after having done this 3 steps of the algorithm, what should we do next to get the induced grammar, and how?
This is a link to the ABL implementation (works on linux):
http://ilk.uvt.nl/menno/research/software/abl
There are a manual pdf in the package, in addition I wrote to Dr. Zaanen (who presents ABL) and this is his answer:
I have written a small Perl script some years ago that does something
like this. I'm not sure the conversion
of left recursive rules to right recursive rules works well, though.
Zaanen
this is his code (I've not checked it)
#!/usr/bin/perl -w
use strict;
use vars qw($opt_h $opt_i $opt_o $opt_l $opt_d);
use Getopt::Std;
$opt_i ="-"; # default value
$opt_o ="-"; # default value
getopts('hi:o:ld');
$opt_h ||=0; # default value
$opt_d ||=0; # default value
$opt_l ||=0; # default value
my $usage =
"Usage: $0 [OPTION]\.\.\.
This program converts an ABL file into a Nuance GSL file\.
-i FILE Name of input file (default: $opt_i)
-o FILE Name of output file (default: $opt_o)
-l Convert left recurive rules to right recursive (default: off)
-d Debug (default: off)
-h Show this help and exit
";
die $usage if $opt_h;
die $usage if $opt_i eq "";
open(INPUT, "<$opt_i")
|| die "Couldn't open input file: $!\n";
die $usage if $opt_o eq "";
open(OUTPUT, ">$opt_o")
|| die "Couldn't open outputfile: $!\n";
my %grammar;
my $start_nonterm;
my %first;
sub read_line() {
my $line=<INPUT>;
while (defined($line) and ($line=~/^#/)) {
$line=<INPUT>;
}
return $line;
}
sub extract_structure {
my $structure=$_[0];
my %structure;
my $constituent;
while ($structure=~/\(\s*(\d+)\s*,\s*(\d+)\s*,\s*\[([^\]]+)\]\s*\)\s*/g) {
my $b=$1;
my $e=$2;
my $n=$3;
if ($n!~/^\d+$/) {
die "Incorrect nonterminal format: $n";
}
if ($b!=$e) {
$structure{$b}{$e}=$n;
}
}
return %structure;
}
sub find_rhs {
my #sentence=#{$_[0]};
my %structure=%{$_[1]};
my $begin=$_[2];
my $end=$_[3];
my #result;
my $counter=$begin;
while ($counter!=$end) {
if (defined($structure{$counter})) {
# handle non-term
my #keys=sort { $a < $b } keys %{$structure{$counter}};
if ($keys[0]>$end) {
die "Found overlapping constituent: ($counter,$keys[0],[${$structure{$counter}}{$keys[0]}])";
}
my $nonterm=translate_nonterm(${$structure{$counter}}{$keys[0]});
push (#result, $nonterm);
find_grammar(\#sentence, \%structure, $counter, $keys[0]);
$counter=$keys[0];
}
else {
# handle term
push (#result, $sentence[$counter]);
++$counter;
}
}
return join(" ", #result);
}
sub find_grammar {
my #sentence=#{$_[0]};
my %structure=%{$_[1]};
my $begin=$_[2];
my $end=$_[3];
my $top_level=$_[4];
my $lhs="";
if (defined($structure{$begin}) and
defined(${$structure{$begin}}{$end})) {
$lhs=${$structure{$begin}}{$end};
delete ${$structure{$begin}}{$end};
if (scalar(keys %{$structure{$begin}})==0) {
delete $structure{$begin};
}
}
if ($lhs eq "") {
die "missing start symbol";
}
my $rhs=find_rhs(\#sentence, \%structure, $begin, $end);
$lhs=translate_nonterm($lhs);
if ($top_level) {
$start_nonterm=$lhs;
}
${$grammar{$lhs}}{$rhs}++;
}
sub translate_nonterm {
my $in=$_[0];
if ($in==0) {
return "A";
}
my $result;
while ($in!=0) {
my $digit=$in%10;
$in=($in-($in%10))/10;
$result.=chr(ord("A")+$digit);
}
return $result;
}
sub remove_direct_left_recursion {
my $Ai=$_[0];
my #rhss=keys %{$grammar{$Ai}};
my %recursive;
my %nonrecursive;
foreach my $rhs (#rhss) {
if ($rhs=~/^$Ai\s*(.*)$/) {
my $alpha=$1;
$recursive{$alpha}++;
}
else {
$nonrecursive{$rhs}++;
}
}
if (scalar(keys %recursive)>0) {
delete $grammar{$Ai}; # remove
delete $first{$Ai};
foreach my $rhs (keys %nonrecursive) {
if ($rhs=~/^([A-Z]+)\s*(.*)$/) {
$first{$Ai}{$1}=1;
}
$grammar{$Ai}{$rhs}++;
$grammar{$Ai}{$rhs." $Ai'"}++;
}
foreach my $rest (keys %recursive) {
if ($rest=~/^([A-Z]+)\s*(.*)$/) {
$first{$Ai}{$1}=1;
}
$grammar{"$Ai'"}{$rest}++;
$grammar{"$Ai'"}{$rest." $Ai'"}++;
}
}
}
sub remove_left_recursion {
my $nonterm=$_[0];
print "handling $nonterm\n";
if (defined($first{$nonterm})) { # already done
print "already done\n";
return;
}
# find left nonterms in first
my #rhs=keys %{$grammar{$nonterm}};
foreach my $rhs (#rhs) {
if ($rhs=~/^([A-Z]+)\s*.*$/) {
my $left_nonterm=$1;
$first{$nonterm}{$left_nonterm}=1;
}
}
print "first level:".join(" ", (keys %{$first{$nonterm}}))."\n";
# find first of nonterms in first
foreach my $left_nonterm (keys %{$first{$nonterm}}) {
remove_left_recursion($left_nonterm);
foreach my $n (keys %{$first{$left_nonterm}}) {
$first{$nonterm}{$n}=1;
}
}
if (defined($first{$nonterm}{$nonterm})) {
my #chain=keys %{$first{$nonterm}};
foreach my $a (keys %first) {
print "$a:".join(" ", (keys %{$first{$a}}))."\n";
}
print "Identified chain $nonterm=".join(" ", #chain)."\n";
remove_left_recursion_chain(\#chain);
#chain=keys %{$first{$nonterm}};
foreach my $a (keys %first) {
print "$a:".join(" ", (keys %{$first{$a}}))."\n";
}
print "rest of chain: $nonterm=".join(" ", #chain)."\n";
}
}
sub remove_left_recursion_chain {
my #lhs=#{$_[0]};
my $nr_lhs=scalar(#lhs)-1;
for my $i (0 .. $nr_lhs) {
my $Ai=$lhs[$i];
my #rhs_Ai=keys %{$grammar{$Ai}};
if ($opt_d) {
print STDERR "Removing recursion of $Ai: ";
print STDERR "$i of $nr_lhs (".scalar(#rhs_Ai).")\n";
}
for my $j (0 .. $i-1) {
my $Aj=$lhs[$j];
foreach my $rhs (#rhs_Ai) {
if ($rhs=~/^$Aj\s*(.*)$/) {
my $alpha=$1;
delete ${$grammar{$Ai}}{$rhs};
foreach my $beta (keys %{$grammar{$Aj}}) {
${$grammar{$Ai}}{"$beta $alpha"}++;
if ($rhs=~/^([A-Z]+)\s*(.*)$/) {
$first{$Ai}{$1}=1;
}
}
delete $first{$Ai}{$Aj};
}
}
}
# transform direct left recursion
remove_direct_left_recursion($Ai);
}
}
sub remove_unreachable_rules {
my %used_tokens;
$used_tokens{$start_nonterm}++; # start is always reachable
foreach my $lhs (keys %grammar) {
foreach my $rhs (keys %{$grammar{$lhs}}) {
foreach my $token (split(/\s+/, $rhs)) {
$used_tokens{$token}++;
}
}
}
my #lhss=keys %grammar;
foreach my $lhs (#lhss) {
if (!defined($used_tokens{$lhs})) {
delete $grammar{$lhs};
}
}
}
sub print_grammar {
foreach my $lhs (sort keys %grammar) {
my %rhss=%{$grammar{$lhs}};
my #rhss;
foreach my $rhs (keys %rhss) {
if ($rhs=~/\s/) {
push (#rhss, "($rhs)");
}
else {
push (#rhss, "$rhs");
}
}
if ($lhs eq $start_nonterm) {
$lhs=".$lhs";
}
if (scalar(#rhss)>1) {
print OUTPUT "$lhs [".join(" ", #rhss)."]\n";
}
else {
print OUTPUT "$lhs ".join(" ", #rhss)."\n";
}
}
}
my $nr_lines;
my $line=read_line();
while (defined($line)) {
$nr_lines++;
chomp ($line);
if ($line!~/^(.*)###(.*)$/) {
die "Incorrect input: $line";
}
my #sentence=split(/\s+/, $1);
my $structure=$2;
my %structure=extract_structure($structure);
# do top level
find_grammar(\#sentence, \%structure, 0, scalar(#sentence), 1);
$line=read_line();
}
if ($opt_d) {
print STDERR "$nr_lines lines\n";
print STDERR scalar(keys %grammar)." nonterminals\n";
my $nr_rules;
foreach my $nonterm (keys %grammar) {
$nr_rules+=scalar(keys %{$grammar{$nonterm}});
}
print STDERR "$nr_rules rules\n";
}
if ($opt_l) {
remove_left_recursion($start_nonterm);
# remove_left_recursion();
# remove_unreachable_rules();
if ($opt_d) {
print STDERR scalar(keys %grammar)." non left recursive nonterminals\n";
my $nr_rules;
foreach my $nonterm (keys %grammar) {
$nr_rules+=scalar(keys %{$grammar{$nonterm}});
}
print STDERR "$nr_rules non left recursive rules\n";
}
}
print_grammar();
close(INPUT);
close(OUTPUT);

Perl script to start/stop windows service

Here is the script for checking status of windows services and if they are in stop state, it will start the service. I can able to get the status of service, but can't able to start services.
please help and let me know what I need to do.
#!/usr/local/bin/perl
use Win32::Service;
use strict;
sub checkService();
sub getDate();
sub getTime();
sub logEvent();
my #timeInfo = localtime(time);
my $serviceName = "TapiSrv";
my $currentDate = getDate();
my $currentTime = getTime();
my %status;
my %status_code = (1 => 'not running',
2 => 'start pending',
3 => 'stop pending',
4 => 'running',
5 => 'resume pending',
6 => 'pause pending',
7 => 'paused');
checkService();
########
# SUBS
########
sub checkService() {
my $startCounter = 0;
Win32::Service::GetStatus('', $serviceName, \%status);
if($status{"CurrentState"} eq '4') {
# Log the event
&logEvent("$currentTime: $serviceName is running\n");
} elsif($status{"CurrentState"} eq '1') {
Win32::Service::StartService('', $serviceName);
}
while($startCounter < 3) {
sleep(5);
Win32::Service::GetStatus('', $serviceName, \%status);
if($status{"CurrentState"} eq '2') {
$startCounter++;
} else {
$startCounter = 3;
}
}
if($startCounter == 3) {
&logEvent("$currentTime: Unable to start $serviceName in $startCounter attempts\n");
} else {
&logEvent("$currentTime: Started $serviceName in $startCounter attempts\n");
}
}
sub getDate() {
my $year = $timeInfo[5] + 1900;
my $month = $timeInfo[4] + 1;
my $day = $timeInfo[3];
return sprintf("%04d-%02d-%02d", $year, $month, $day);
}
sub getTime() {
my $hour = $timeInfo[2];
my $min = $timeInfo[1];
my $sec = $timeInfo[0];
return sprintf("%02d:%02d:%02d", $hour, $min, $sec);
}
sub logEvent() {
# Log the event
open(OUT, ">> C:/servicestatus/$currentDate.txt");
print OUT "$_[0]";
close(OUT);
}
Based upon some comments below (including some nice points by #Ron Bergin), I'm revising this post to show code that works for me (Windows 8.1, ActivePerl 5.16).
#!/usr/local/bin/perl
use strict;
use warnings;
use POSIX;
use Win32::Service;
my $currentDate = getDate();
my %status;
my %status_code = (
Stopped => 1,
StartPending => 2,
StopPending => 3,
Running => 4,
ResumePending => 5,
PausePending => 6,
Paused => 7
);
checkService("Apple Mobile Device");
########
# SUBS
########
sub checkService {
my $serviceName = shift || die "No arg passed";
my $startCounter = 1;
Win32::Service::GetStatus('', $serviceName, \%status);
if ($status{"CurrentState"} eq $status_code{Running}) {
logEvent("$serviceName is running\n");
}
elsif ($status{"CurrentState"} eq $status_code{'Stopped'}) {
my $maxAttempts = 3;
while ($startCounter <= $maxAttempts) {
logEvent("Attempting to start $serviceName");
Win32::Service::StartService('', $serviceName);
sleep(5);
Win32::Service::GetStatus('', $serviceName, \%status);
if ($status{"CurrentState"} eq $status_code{Running}) {
logEvent("Started $serviceName in $startCounter attempts\n");
last;
}
$startCounter++;
}
if ($startCounter eq $maxAttempts) {
logEvent("Unable to start $serviceName in $startCounter attempts\n");
}
}
}
sub getDate {
my #timeInfo = localtime(time);
my $year = $timeInfo[5] + 1900;
my $month = $timeInfo[4] + 1;
my $day = $timeInfo[3];
return sprintf("%04d-%02d-%02d", $year, $month, $day);
}
sub logEvent {
my $msg = strftime("%H:%M:%S", localtime) . ": $_[0]\n";
print "$msg";
open(OUT, ">> C:/servicestatus/$currentDate.txt");
print OUT "$msg";
close(OUT);
}
Running this NOT as an admin, gives output like this:
14:11:30: Attempting to start Apple Mobile Device
14:11:35: Attempting to start Apple Mobile Device
14:11:40: Attempting to start Apple Mobile Device
Running as an admin looks like this:
14:14:29: Attempting to start Apple Mobile Device
14:14:34: Started Apple Mobile Device in 1 attempts
One major problem I have with this Win32::Service module is that it returns undef on failure BUT doesn't set $! so finding out why it failed is more work. I have not done any testing in retrieving that error, but it's probably a call to one of the functions in the Win32 module.
#!/usr/local/bin/perl
use 5.010;
use strict;
use warnings;
use POSIX qw(strftime);
use Win32::Service qw(StartService GetStatus GetServices);
my $service = shift || 'Apple Mobile Device';
check_service($service);
exit;
###############################################################################
sub check_service {
my $service = shift;
my %status_code = (
Stopped => 1,
StartPending => 2,
StopPending => 3,
Running => 4,
ResumePending => 5,
PausePending => 6,
Paused => 7
);
my (%status, %services);
GetServices('', \%services) or do {
log_event('Failed to retieve list of services');
exit;
};
%services = reverse %services;
if (! exists $services{$service}) {
log_event("'$service' is not a configured Windows service");
exit;
}
if (GetStatus('', $service, \%status)) {
if ($status{"CurrentState"} eq $status_code{Running} ) {
log_event("$service is running");
}
elsif ( $status{"CurrentState"} eq $status_code{'Stopped'} ) {
ATTEMPT: for (1..3) {
log_event("Attempting to start '$service'");
if (StartService('', $service)) {
sleep 5;
GetStatus('', $service, \%status);
if ($status{"CurrentState"} eq $status_code{Running}) {
log_event("Started '$service'");
last ATTEMPT;
}
}
else {
die "StartService() function failed";
}
}
}
}
else {
log_event("failed to retrieve the status of service '$service'");
exit;
}
return;
}
sub log_event {
# Using one of the better loging modules such as Log::Log4perl
# would be a much better and more robust logging mechanism
my $msg = shift;
my $timestamp = strftime("%H:%M:%S", localtime);
my $filename = strftime("C:/servicestatus/%Y-%m-%d.txt", localtime);
open(my $fh, '>>', $filename) or die "failed to open '$filename' <$!>";
say $fh "$timestamp: $msg";
close $fh;
return;
}

how to send message from client-web to server-MFC-app?

My task is create a client-web communicate to server-mfc-app. That mean I am not allow to change server-mfc-app code, but must make web can talk to that server-mfc-app. That server-mfc-app uses socket and can communicate to client-mfc-app.
I created a client-web can connect to server-mfc-app successful (server-mfc-app accepts connection), but there is a problem when client-web sends message to server-mfc-app: server-MFC-app knows client-web sends message, but it can get message by function void CChatServerDlg::ReceiveData(SOCKET hSocket) (I run debug mode server-mfc-app to watch all functions).
Server-MFC-app code:
ON_MESSAGE(MSG_ASYNC, OnAsyncSelect)
LRESULT CChatServerDlg::OnAsyncSelect(WPARAM wParam, LPARAM lParam)
{
if (WSAGETSELECTERROR(lParam) != 0)
{
}
else
{
switch(WSAGETSELECTEVENT(lParam))
{
case FD_READ:
ReceiveData(wParam);
break;
case FD_ACCEPT:
{
CUserSocket *pClient = AcceptConnection();
if(pClient)
{
CString sReport;
sReport.Format(_T("Có kết nối từ IP: %s , Port: %d"),
pClient->GetRemoteIpAddr(),pClient->GetRemotePort());
m_lbStatus.AddString(sReport);
SendData(pClient->GetSocket(),_T("Server sẵn sàng"));
m_groupNewUsers.AddUser(pClient);
}
}
break;
case FD_CLOSE:
CUserGroup *gr = m_groupList.FindGroup(wParam);
if (gr!=NULL)
gr->RemoveUser(wParam);
closesocket(wParam);
OnLbnSelchangeGrouplist();
m_lbStatus.AddString(_T("Đã đóng kết nối"));
break;
}
}
return 0L;
}
void CChatServerDlg::ReceiveData(SOCKET hSocket)
{
char szBuf[MAX_LEN];
int nByteRe = recv(hSocket, szBuf, MAX_LEN, 0);//always successful
// with client-mfc-app, and always unsuccessful with client-web below.
if (nByteRe<=0)
return;
wchar_t wch[MAX_LEN];
CString s = _T("");
MultiByteToWideChar(CP_UTF8,0,szBuf,nByteRe,wch,nByteRe/2);
wch[nByteRe/2] = '\0';
s += wch;
SolveRequest(hSocket,s);
}
Client-web code:
<?php
error_reporting(E_ALL);
$port = 2012;
$address = "127.0.0.1";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: "
.socket_strerror(socket_last_error())."<br/>";
}
$result = socket_connect($socket, $address, $port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) "
.socket_strerror(socket_last_error($socket)) ."<br/>";
}else{
echo "Connecting successful."."<br/>";
}
$msg = "ULIST";
$len = strlen($msg);
$flag=0;
socket_sendto($socket, $msg, $len, $flag, $address, $port);
//socket_close($socket);
?>
Please try to use socket_write($socket, $msg, $len ); instead of socket_sendto for connected socket

Get the server name and ip address in C# 2010

Get the server name and ip address in C# 2010
I want to get the IP address of the server. The following code comes from:
public static void DoGetHostEntry(string hostname)
{
IPHostEntry host;
host = Dns.GetHostEntry(hostname);
MessageBox.Show("GetHostEntry({0}) returns:"+ hostname);
foreach (IPAddress ip in host.AddressList)
{
MessageBox.Show(" {0}"+ ip.ToString());
}
}
This code must know the name of the server computer.
AddressFamily in System.Net.IPAddress
System.Net.IPAddress i;
string HostName = i.AddressFamily.ToString();
Error ------------->Use of unassigned local variable 'i'
How can I get the name of the server computer?
To get the host name you can do the following:
string name = System.Net.Dns.GetHostName();
If you want the hostname and (first IPv4) IP of your computer use the following:
string name = System.Net.Dns.GetHostName();
host = System.Net.Dns.GetHostEntry(name);
System.Net.IPAddress ip = host.AddressList.Where(n => n.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First();
The name and the ip will hold the info for the local computer.
The server could then send out the ip via a udp multicast and the client on the network would just join a known multicast address that is not specific to the server.
multicast example.
First of all, you need to figure out for yourself that error(unassigned local variable) and learn why it is coming(it is very basic), before looking for some magical code that will do the job for you.
And secondly, there is no magical code. I am no socket programmer but it seems to me that in your application running on the client machines, you need to hardcode the name of your server. If you don't want to do that, program in such a way that only your server machine will listen on a particular port and all client machines will listen on a different port. Thus, each machine in the LAN can enumerate over the available machines and establish/determine the client server connection/relation for the first time. and that approach is still very ugly unless you are writing a virus or something.
public string[] ServerName()
{
string[] strIP = DisplayIPAddresses();
int CountIP = 0;
for (int i = 0; i < strIP.Length; i++)
{
if (strIP[i] != null)
CountIP++;
}
string[] name = new string[CountIP];
for (int i = 0; i < strIP.Length; i++)
{
if (strIP[i] != null)
{
try
{
name[i] = System.Net.Dns.GetHostEntry(strIP[i]).HostName;
}
catch
{
continue;
}
}
}
return name;
}
public string[] DisplayIPAddresses()
{
StringBuilder sb = new StringBuilder();
// Get a list of all network interfaces (usually one per network card, dialup, and VPN connection)
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
int i = -1;
string[] s = new string[networkInterfaces.Length];
foreach (NetworkInterface network in networkInterfaces)
{
i++;
if (network.OperationalStatus == OperationalStatus.Up)
{
if (network.NetworkInterfaceType == NetworkInterfaceType.Tunnel) continue;
if (network.NetworkInterfaceType == NetworkInterfaceType.Tunnel) continue;
//GatewayIPAddressInformationCollection GATE = network.GetIPProperties().GatewayAddresses;
// Read the IP configuration for each network
IPInterfaceProperties properties = network.GetIPProperties();
//discard those who do not have a real gateaway
if (properties.GatewayAddresses.Count > 0)
{
bool good = false;
foreach (GatewayIPAddressInformation gInfo in properties.GatewayAddresses)
{
//not a true gateaway (VmWare Lan)
if (!gInfo.Address.ToString().Equals("0.0.0.0"))
{
s[i] = gInfo.Address.ToString();
good = true;
break;
}
}
if (!good)
{
continue;
}
}
else
{
continue;
}
}
}
return s;
}

Resources