PHP Loop Question

Hello,

I will start with a little bit of background on what I’m trying to do. I’m running an snmpwalk command to retrieve the version of software running on 8 servers. This is the code I’m currently using to check the versions…

[php] exec(‘snmpwalk -v2c server01.cluster1.mydomain.com -c attask .1.4.7.1.3 -Ovq’,$cluster1,$return1);
exec(‘snmpwalk -v2c server02.cluster1.mydomain.com -c attask .1.4.7.1.3 -Ovq’,$cluster1,$return2);
exec(‘snmpwalk -v2c server02.cluster1.mydomain.com -c attask .1.4.7.1.3 -Ovq’,$cluster1,$return3);
[/php]

etc…

Instead of having this eight times is there a way I can loop through this command for each server and store it in a variable? (like using foreach or something??)

After this command gathers all the server versions it runs a foreach and loops to make sure they are all the same. (because the versions get stored in the $cluster1 variable.)

The reason I ask is because servers get added over time and it would be easier to make a small change instead of adding several lines every time we boot up a new server.

John,
Set the server addresses in an array and use a foreach loop to run this command on each value in the array. The just store the results in a new variable using one of these examples

http://php.net/manual/en/language.variables.variable.php

it should be simple enough.

Sponsor our Newsletter | Privacy Policy | Terms of Service