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;

In reply to Trying to create a linux interfaces file out of rows from a database. by jduffany

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.