function startElement($parser, $name, $attrs) {
global $curTag;
$curTag .= "^$name";
}
function endElement($parser, $name) {
global $curTag;
$caret_pos = strrpos($curTag,'^');
$curTag = substr($curTag,0,$caret_pos);
}
function characterData($parser,$data) {
global $curTag;
global $firmwareVersion ;
#echo "Char $curTag \n" ;
if ($curTag == "^SETTINGS^PHONE-SETTINGS^FIRMWARE_VERSION") {
$firmwareVersion = $data ;
}
}
function icmpChecksum($data)
{
if (strlen($data)%2)
$data .= "\x00";
$bit = unpack('n*', $data);
$sum = array_sum($bit);
while ($sum >> 16)
$sum = ($sum >> 16) + ($sum & 0xffff);
return pack('n*', ~$sum);
}
function pingTest($domain)
{
// Making the package
$type= "\x08";
$code= "\x00";
$checksum= "\x00\x00";
$identifier = "\x00\x00";
$seqNumber = "\x00\x00";
$data= "Scarface";
$package = $type.$code.$checksum.$identifier.$seqNumber.$data;
$checksum = icmpChecksum($package); // Calculate the checksum
$package = $type.$code.$checksum.$identifier.$seqNumber.$data;
// And off to the sockets
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_set_option ( $socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>4, "usec"=>0) );
socket_connect($socket, $domain, null);
$startTime = microtime(true);
socket_send($socket, $package, strLen($package), 0);
$ttime = 0 ;
if (false===@(socket_read($socket, 255))) {
socket_close($socket);
return -1 ;
}
else {
#echo round(microtime(true) - $startTime, 4) .' seconds';
$ttime= round(microtime(true) - $startTime, 4) ;
}
socket_close($socket);
return $ttime ;
}
function parsefirmware($settings) {
return parsefirmwareHTML($settings) ;
}
function getsettings($phoneip) {
return getsettingsHTML($phoneip) ;
}
function parsefirmwareHTML($settings) {
global $firmwareVersion ;
$firmwareVersion="" ;
$tagSrch = "application_version!:" ;
#echo "Settings from $settings\n" ;
$res = preg_match('/application_version!:\s*snom320-SIP (.*)/',$settings,$matches) ;
if ($res == 1) {
$firmwareVersion = $matches[1] ;
} else {
$res = preg_match('/firmware_version!:\s*snom320-SIP (.*)/',$settings,$matches) ;
if ($res == 1) {
$firmwareVersion = $matches[1] ;
}
}
return $firmwareVersion ;
}
function parsefirmwareXML($xmlsettings) {
global $firmwareVersion ;
global $curTag;
$curTag = "" ;
$firmwareVersion="" ;
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!xml_parse($xml_parser,$xmlsettings)) {
return "" ;
}
xml_parser_free($xml_parser);
return $firmwareVersion ;
}
function getsettingsXML($phoneip) {
$timeout=10 ;
$url = "http://$phoneip/settings.xml" ;
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_VERBOSE, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($c, CURLOPT_TIMEOUT, $timeout);
$content=curl_exec($c) ;
curl_close($c) ;
return $content ;
}
function getsettingsHTML($phoneip) {
$timeout=10 ;
$url = "http://$phoneip/settings.htm" ;
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_VERBOSE, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($c, CURLOPT_TIMEOUT, $timeout);
$content=curl_exec($c) ;
curl_close($c) ;
return $content ;
}
function upgrade($host,$phoneip,$fware) {
$timeout = 5 ;
$firmware = $fware ;
$webserverurl="http://$host/$firmware";
$phoneurl="http://$phoneip/update.htm?swload=Load&firmware=$webserverurl" ;
$url=$phoneurl ;
# echo "URL IS $url\n" ;
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_VERBOSE, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($c, CURLOPT_TIMEOUT, $timeout);
$content=curl_exec($c) ;
curl_close($c) ;
# echo $content ;
if ($content == "") {
# echo "***FAILED*** $phoneip\n" ;
return -1 ;
} else {
# echo "Upgrade $phoneip\n" ;
return 1 ;
}
}
echo "Welcome to SNOM320 Upgrade Utility \n" ;
$baseip="10.0.0." ;
$webserver="10.0.0.93:8080" ;
$realupgrade = false ;
$noscan = false ;
foreach ($argv as $key => $value) {
if ($value=="upgrade") {
$realupgrade = true ;
} elseif ($value=="noscan") {
$noscan=true ;
}
}
if (!$realupgrade) {
echo "NO UPGRADING use upgrade on argument line (php -f $argv[0] upgrade [noscan]) to turn upgrade on!\n" ;
}
$phones=array() ;
if (!$noscan) {
$fh=fopen("scan.txt","w") ;
#$scanlist="27,107,120,115,125,126,118,63,92,96,25,114,123,121,103,116,59,112,56,109,62,97,104,113,76,55,94,102" ;
$scanlist="19" ;
$scanarray=split(",",$scanlist) ;
echo "Scanning ".count($scanarray)." Elements\n" ;
foreach ($scanarray as $index => $val) {
$ip=$baseip.$val ;
# echo "Check IP $ip\n" ;
# }
# for ( $counter = 2; $counter <= 255; $counter += 1) {
# $ip = $baseip.$counter ;
echo "Peeking $ip.." ;
$response=pingTest($ip) ;
if ($response >= 0) {
$settings = getsettings($ip) ;
if ($settings != "") {
$cfirmware =trim(parsefirmware($settings)) ;
if ($cfirmware != "") {
$phones[$ip] = $cfirmware ;
fwrite($fh,"$ip,$cfirmware\n") ;
echo "Found Phone -$ip Current Firmware is <$cfirmware> Count is ".count($phones)."\n" ;
} else {
echo "NO RECOGNISED PHONE-\n" ;
}
} else {
echo "NO PHONE OR EQUIPMENT-\n" ;
}
} else {
echo "NO EQUIPMENT -(no reply from ping)\n" ;
#fwrite($fh,"$ip,testfirmNOEUIP\n") ;
}
}
fclose($fh) ;
} else {
# No Scanning use file data instead
echo "NO SCANNING - USING FILEDATA - " ;
$lines=file("scan.txt") ;
foreach ($lines as $index => $val) {
$line=$val ;
$d=split(",",$line) ;
$ip=$d[0] ;
$cfirmware=trim($d[1]) ;
$phones[$ip]=$cfirmware ;
echo "Parse scan file - Phone - $ip Current Firmware is <$cfirmware>\n" ;
}
}
# echo "Settings ".$settings ;
# $res=upgrade($webserver,$ip,"snom320-7.3.23.bin") ;
# echo "Result $res\n" ;
$upgrading=array() ;
$upgradingold=array() ;
$completed=array() ;
foreach( $phones as $ip => $firmware){
echo "$ip has Firmware $firmware\n";
$newfirmware="" ;
if ($firmware == "7.3.14") {
$newfirmware="snom320-7.3.23.bin" ;
} elseif ($firmware=="7.1.30") {
# $newfirmware="snom320-7.3.14bf.bin" ;
} elseif ($firmware=="6.5.13") {
$newfirmware="snom320-6.5.20.bin" ;
} elseif ($firmware=="6.5.16") {
$newfirmware="snom320-6.5.20.bin" ;
} elseif ($firmware=="6.5.15") {
$newfirmware="snom320-6.5.20.bin" ;
} elseif ($firmware == "6.5.20") {
$newfirmware="snom320-7.3.14bf.bin" ;
} elseif ($firmware == "7.3.23") {
} elseif ($firmware == "7.3.23") {
} else {
# $newfirmware = "snom320-7.3.23.bin" ;
}
if ($newfirmware != "") {
$upgrading[$ip]=$newfirmware ;
$upgradingold[$ip]=$firmware ;
} else {
$completed[$ip]=$firmware ;
}
}
echo "Found ".count($phones)." Upgrade ".count($upgrading)." Completed ".count($completed)."\n" ;
echo "Upgrading Process..\n" ;
foreach( $upgrading as $ip => $newfirmware){
$oldfirmware = $upgradingold[$ip] ;
echo "Upgrading $ip from old $oldfirmware to $newfirmware..\n" ;
if ($realupgrade) {
$res=upgrade($webserver,$ip,$newfirmware) ;
if ($res == 1) {
echo "Something is being done!\n" ;
}
}
}
echo "Found ".count($phones)." Upgrading ".count($upgrading)." Completed ".count($completed)."\n" ;
if (!$realupgrade) {
echo "SCANNING ONLY COMPLETE\n" ;
} else {
echo "PLEASE WAIT 2 MINS BEFORE RUNNING A SCAN!\n" ;
}
?>
Monday, 30 November 2009
SNOM 320 PHP Upgrader
Subscribe to:
Post Comments (Atom)
2 comments:
What planet are you from John?
b e n
Sorry! - my Blog is now becoming a Diary of some of my old code which I may find useful in 20 years or so.
Post a Comment