Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not sure how to do this.

Status
Not open for further replies.
Jun 20, 2003
40
GB
I'm trying to gather host information on a number of Windows Boxes. The information source is "ipconfig /all". I want to populate a MySQL DataBase with the following information :-

Host Name . . . . . . . . . . . . : servername
Ethernet adapter Local Area Connection: (Only the section Local Area Connection)
IP Address. . . . . . . . . . . . : 192.168.1.200

All of the servers has six for seven NIC's installed, I can display the information I need but can't seem to work out how to collect it in order and then place it into a table. So the output I'm looking for is :-

servername, LAN, 192.168.1.200
servername, SWITCH1, 192.168.1.210
servername, SWITCH2, 192.168.1.220

Thanks for your help.
 
Hello Maximo,

You have a couple of things to decide first I think.

1 - How are you going to get the data from the monitored windows box?

2 - How are you planning to run the Perl script on the monitored windows box?

Or..... Have I misunderstood the question completely and you just want to re-format the output of ipconfig?

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
It may be better to use windows scripting to gather the information, say into a text file, then use perl to process the file and put the info into a database. Otherwise, I'd think you'd need perl on each machine you need to gather the information from. And if the machines don't have perl installed it'd probably be easier to just enter the info manually into the database than install perl on each machine.


 
Thanks for the replies,

Perl is installed on each of the servers, so what I'm looking to do is process the information with out the need to transfer text files around. I'm basicly just trying to this output into a database :-

servername, LAN, 192.168.1.200
servername, SWITCH1, 192.168.1.210
servername, SWITCH2, 192.168.1.220


 
Do you have a specific question about this and have you gotten started on it yet?

You'll want to get the DBI perl module if you don't have it already. It provides connectivity to your MySQL database.

 
Philote, Yes I'm all ready to go. I just can't seem to get the output right. I'm sure it's just my code and my lack of Perl knowledge. Here's a version of my code that doesn't work, at least it's a starting point:

use strict;
use DBI;

my ($dbname, $host, $user, $password) =
qw (db_infrastructure 192.168.254.99 username password);

my $dbh = DBI->connect("DBI:mysql:$dbname:$host:$user:$password")
or die "Can't Connect user $user to database $host:$dbname";

my $ipconfig = "ipconfig /all";
my @ipconfig = `$ipconfig`;
my $description = "Backup Server";
my $ipaddress = "";
my $hostname = "localhost";
my $fields = "";

foreach my $line (@ipconfig) {
chop($line);

my $data = grep (/Host Name/, $line);
if ($data > 0) {
(my $rubbish, my $hostname) = split(/:\s+/, $line);
$hostname = uc $hostname;
#print "$hostname\n";
$fields = $hostname;
}

my $data = grep (/Admin/, $line);
if ($data > 0) {
$description = "Admin Server";
#print "$description\n";
$fields = $fields." ".$description;
}

my $data = grep (/IP Address/, $line);
if ($data > 0) {
(my $rubbish, my $ipaddress) = split(/:\s+/, $line);
#print "$ipaddress\n";
$fields = $fields." ".$ipaddress;
}
}
print "$fields\n";
 
I tried this code and it output the hostname and IP address of my computer. What is it exactly you're having trouble with?

 
The servers that this will run on have upto 8 NICS all with different names and address. So I'm trying to get the code to produce this type of output :

servername, LAN, 192.168.1.200
servername, SWITCH1, 192.168.1.210
servername, SWITCH2, 192.168.1.220

Once I have the output right I can then dump it into a database.

I know the code will happly do this for a single nic but will not work with on the servers with more than one nic.
 
Ok, this probably isn't the best code, but see if it works for you.
Code:
...
#Get the relevant lines and ignore everything else
my @lines = grep(/(Host Name)|(adapter)|(IP Address)/, @ipconfig);

#get the host name
my $hostname = shift @lines;
$hostname =~ s/.*://;
chomp $hostname;

#get each adapter and ip address
my %hash = @lines;
foreach my $key (keys %hash){
  my @temp = split(/adapter\s/, $key);
  my $adapter = $temp[1];
  @temp = split(/:\s/,$hash{$key});
  my $ip = $temp[1];
  chomp $adapter;
  chop $adapter;
  chomp $ip;
  print "$hostname, $adapter, $ip\n";
}

 
Philote,

Good stuff, it works!!!!!!!!!!!!!!!!!!!!!!!!

Great stuff and thatnks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top