...

View Full Version : Perl code looping using XML::Twig



coding_new
04-20-2009, 10:22 PM
I am a fairly newbiee at coding in perl so please bear with me. I am sure this is probably a simple issue, but cant seem to figure it out myself.
Basically I have this code that pulls in XML using XML::Twig, and then is supposed to process based on when it hits a certain criteria. Well the issue is that it works, but it loops 4 times with that code. I have figured out that it has to do with the XML having 4 entries, but am not sure how to clean it up and remove it.
I have some variables that will be dynamic once I get past this section, so the code is not complete yet, so please bear with me.
Can anyone help out?


Below is my code and the XML that I am working with.


#!/usr/local/bin/perl
use XML::Twig;
use Net::Telnet();

$db = "hrdm2";
$comm = "stop-$db.sh";
$cachedel = "No";

print "$comm $db\n\n";

my $s= new XML::Twig( TwigHandlers=> { Product => \&product});
$s->parsefile( 'testservers2.xml');
#$s->print;
exit;

sub product
{
my ($s, $product)= @_;
my %product;
$product{id}= $product->field( 'ServerID');
$product{name}= $product->field( 'DBName');
$product{commandstop}= $product->field( 'CommandStop');
$product{commandstart}= $product->field( 'CommandStart');
$product{cache}= $product->field( 'CacheDir');

print "$product{id}\n";
print "$product{name}\n";

#$product->delete;

if ($db eq $product{name} && $comm eq $product{commandstop} && $cachedel eq 'No')
{
print "DB Name: $product{name}\n" ;
print "Command: $product{commandstop}\n";
print "Server: $product{id}\n";
print "Cache Dir:$product{cache}\n";
print "Cache Del: No\n";

$pass = "pass";
$t = new Net::Telnet (Timeout => 500);
$t->open("$product{id}");
$t->login('root', $pass);
@lines = $t->cmd("cd /apps/gcpsscripts");
@lines = $t->cmd("ls");
@lines = $t->cmd("sh $product{commandstop}");
print @lines;

$product->delete;
}

elsif ($db eq $product{name} && $comm eq $product{commandstop} && $cachedel eq 'Yes')
{
print "DB Name: $product{name}\n" ;
print "Command: $product{commandstop}\n";
print "Server: $product{id}\n";
print "Cache Dir:$product{cache}\n";
print "Cache Del: Yes\n";

$pass = "pass";
$t = new Net::Telnet (Timeout => 500);
$t->open("$product{id}");
$t->login('root', $pass);
@lines = $t->cmd("cd /apps/gcpsscripts");
@lines = $t->cmd("ls");
@lines = $t->cmd("sh $product{commandstop}");
print @lines;

$product->delete;
}

elsif ($db eq $product{name} && $comm eq $product{commandstart})
{
print "DB Name: $product{name}\n" ;
print "Command: $product{commandstart}\n";
print "Server: $product{id}\n";

$pass = "pass";
$t = new Net::Telnet (Timeout => 500);
$t->open("$product{id}");
$t->login('root', $pass);
@lines = $t->cmd("cd /apps/gcpsscripts");
@lines = $t->cmd("ls");
@lines = $t->cmd("sh $product{commandstart}");
print @lines;

$product->delete;
}

}


XML


<?xml version='1.0'?>

<Products>

<Product>
<DBName>hrdm2</DBName>
<ServerID>psdevax03</ServerID>
<CommandStop>stop-hrdm2.sh</CommandStop>
<CommandStart>start-hrdm2.sh</CommandStart>
<CacheDir>/apps/846hrdm2/appserv/hrdm2/CACHE</CacheDir>
</Product>

<Product>
<DBName>hrdev</DBName>
<ServerID>psdevax06</ServerID>
<CommandStop>stop-hrdev.sh</CommandStop>
<CommandStart>start-hrdev.sh</CommandStart>
<CacheDir>/apps/846hrdev/appserv/hrdev/CACHE</CacheDir>
</Product>

</Products>

Resolution was found. Basically it involved an exit code to get out from



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum