The regular expression seem not do work as it suppose to.
Only the last ip address get replace with the proper value.
This is the coding:
$ip_file = "./ips.txt";
$contents = file($ip_file);
$interval = 2;
print "<html><head><title>PHP Pinger</title></head><body>";
foreach ($contents As $ip) {
$ping_result = shell_exec("ping $ip -n $interval");
if (preg_match("/TTL/i", $ping_result)) {
#print "Server, $ip is up.<br>";
}
else {
$gateip = ereg_replace(".([0-9]+)$",".1",$ip);
print "$gateip<br>";
$ping_result = shell_exec("ping $gateip -n $interval");
if (preg_match("/TTL/i", $ping_result)) {
print "Server, $ip is down";
}
else {
print "Gateway, $gateip is down<br>";
}
}
}
print "</body></html>";
The output:
Gateway, 172.23.50.10 is down
Gateway, 192.168.65.1 is down
The ip, 172.23.50.10 should be 172.23.50.1
Any comment?