#!/usr/bin/perl -w

##################################################################
# Description: Script to reboot (all?) Polycom SoundPoint IP phones.
# This is kind of a hack.  But hey!  It works -- at least, with
# default config Polycom SoundPoint IP 430 phones.  NOTE: the
# premise this uses is that you don't give a damn about your NAT
# keepalive time; unfortunately, there's not "reboot" button on
# the web interface, so you need to change *some* value, so I
# assign the NAT keepalive a random value.  The add'l presumption
# I make is that you're using this as part of a provisioning
# process, which would overwrite values you don't care about, anyway.
# Additionally, this assumes your phone has default values for username:
# "Polycom" and "456".  If you don't, you'll need to either figure out
# the md5sum (or capture it via Wireshark), and plug it in where
# commented, below.
# Released as PD -- have a blast.  -Ken
#################################################################
if ($#ARGV != 0){
	print "Usage: reboot_polycom.pl <IP or DNS-resolvable hostname of Polycom\x3e\n";
	exit(0);
}
use IO::Socket::INET;
$socket = IO::Socket::INET->new(PeerAddr => $ARGV[0],
				PeerPort => "80",
				Proto    => "tcp",
				Type     => SOCK_STREAM)
	or die "Coudln't connect to host or port: $!\n";

# The random value to be assigned to the NAT keepalive
$random = int(rand(10000));

# Hack reboot string.  I spaced it out as lines for the obvious \r\n -- 
# which, yes, I believe is required.  Ahhhh, HTTP.
$reboot_string = 
"POST /form-submit HTTP/1.1\r\n".
"Host: 172.17.7.12\r\n".
"User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.12) Gecko/20101027 Ubuntu/10.10 (maverick) Firefox/3.6.12\r\n".
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".
"Accept-Language: en-us,en;q=0.5\r\n".
"Accept-Encoding: gzip,deflate\r\n".
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n".
"Keep-Alive: 115\r\n".
"Connection: keep-alive\r\n".
"Referer: http://172.17.7.12/netConf.htm\r\n".
"Authorization: Basic UG9seWNvbTo0NTY=\r\n". # MD5 hash of "Polycom" and "456"
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: 797\r\n\r\n".
"qos.ethernet.rtp.user_priority=5&qos.ip.rtp.dscp=&qos.ip.rtp.min_delay=1&qos.ip.rtp.max_throughput=1&qos.ip.rtp.max_reliability=0&qos.ip.rtp.min_cost=0&qos.ip.rtp.precedence=5&qos.ethernet.callControl.user_priority=5&qos.ip.callControl.dscp=&qos.ip.callControl.min_delay=1&qos.ip.callControl.max_throughput=0&qos.ip.callControl.max_reliability=0&qos.ip.callControl.min_cost=0&qos.ip.callControl.precedence=5&qos.ethernet.other.user_priority=2&tcpIpApp.port.rtp.filterByIp=1&tcpIpApp.port.rtp.filterByPort=0&tcpIpApp.port.rtp.forceSend=&tcpIpApp.port.rtp.mediaPortRangeStart=&nat.ip=&nat.signalPort=&nat.mediaPortStart=&nat.keepalive.interval=$random&Submit3=Submit&tcpIpApp.keepalive.tcp.idleTransmitInterval=&tcpIpApp.keepalive.tcp.noResponseTrasmitInterval=&tcpIpApp.keepalive.tcp.sip.tls.enable=0\r\n";

print $socket $reboot_string;

