jduffany has asked for the wisdom of the Perl Monks concerning the following question:
I've almost got it, but I can't get the 'auto' line right. I think this is a pretty no brainer question, but alas,I am a noob.
auto lo iface lo inet loopback auto eth0 <--This line needs eth1, too! iface eth0 inet dhcp iface eth1 inet static address 192.168.0.4 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1
This is my table select output
my $sth = $dbh->prepare( "SELECT host, name, type, address, netmask, +gateway FROM $table_name" ); #(PRINTED) app17, eth1, static, 192.168.0.2, 255.255.255.0, 192.168.0.1 app15, eth0, dhcp, , , app15, eth1, static, 192.168.0.4, 255.255.255.0, 192.168.0.1 app12, eth1, static, 192.168.0.5, 255.255.255.0, 192.168.0.1
My first instinct was to create a hash keyed off of hostname, but there's duplicates, then I thought IP address, but those that are DHCP don't have them.
Anyway here's some of the code:
my @ifaces; my @hosts; while ( my @row = $sth->fetchrow_array ) { my $host = $row[0]; my $iface = $row[1]; my $type = $row[2]; my $address = $row[3]; my $netmask = $row[4]; my $gateway = $row[5]; push( @hosts, "$host" ); ########### Here's what I can't get right ############# while ( @hosts ) { push( @ifaces, "$iface"); @ifaces = pop( @ifaces ); last; } ########### Above is what I can't get right ############# my $tmp_file = "/tmp/interfaces.$host"; my $loopback_string = "auto lo\niface lo inet loopback\n\nauto"; open(my $interfaces_file,"+>>$tmp_file") or die "Unable to open $t +mp_file: $!\n"; if ( -z $tmp_file ) { print {$interfaces_file} "$loopback_string @ifaces\n\n"; } if ($type eq "dhcp") { print {$interfaces_file} "iface $iface inet $type\n\n"; } else { print {$interfaces_file} "iface $iface inet $type\naddress $ad +dress\nnetmask $netmask\nnetwork 192.168.0.0\nbroadcast 192.168.0.255 +\ngateway $gateway\n"; close($interfaces_file) or die "Can't close it\n"; } } $dbh->disconnect;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trying to create a linux interfaces file out of rows from a database.
by GrandFather (Saint) on Jul 09, 2011 at 01:14 UTC | |
by jduffany (Initiate) on Jul 14, 2011 at 00:49 UTC | |
by GrandFather (Saint) on Jul 14, 2011 at 09:34 UTC | |
by jduffany (Initiate) on Jul 14, 2011 at 18:08 UTC |