#!/usr/bin/perl ######################################## # This script logs into a google search appliance and tells it to update its keymatch keywords by visiting a url. # # Questions about this script can be directed to: # Blake Crosby (bcrosby@nm.cbc.ca) # # The Canadian Broadcasting Corporation does not support nor endorse this script. # # # # This script was tested on FreeBSD and requires the following perl modules: # libwwwperl (LWP::UserAgent) # HTML::TokeParser # # This script was tested on a 1001 model Google Search Appliance # ######################################### ################################# # Config section # # # hostname of the google appliance you want to update keymatches. # For example: google.hostname.ca $googlehost = 'google.hostname.ca'; # # username and password for the perl script to log in as. I suggest you create a new account specifically # for this script. # For example: user = googlescript password = google # $username = 'updatescript'; $password = 'google'; # # which collection to update the keymatches for. CASE IS IMPORTANT # for example: webpages $collection = 'WebPages'; # # full url of the location of the keymatch file # for example http://www.somehost.com/keymatches.csv # $keymatchurl = 'http://www.somehost.com/keymatches.csv'; #### END OF CONFIG # # #Dont edit below this line, unless you know what you are doing. # #load libwwwperl use HTML::TokeParser; use LWP::UserAgent; $ua = LWP::UserAgent->new; $ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'); #need cookie support; use HTTP::Cookies; $ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt", autosave => 1)); #get some magic cookies $req = HTTP::Request->new(GET => "http://$googlehost:8000/"); $req->header('Referrer' => ''); $res = $ua->request($req); #then load the login page (and get more cookies) $req = HTTP::Request->new(GET => "http://$googlehost:8000/EnterpriseController"); $req->header('Referrer' => ''); $res = $ua->request($req); #let's log into the box :) $req = HTTP::Request->new(POST => "http://$googlehost:8000/EnterpriseController"); $req->header('Referrer' => "http://$googlehost:8000/EnterpriseController"); $req->content_type('application/x-www-form-urlencoded'); $req->content("actionType=authenticateUser&userName=$username&password=$password&login=Login"); $res = $ua->request($req); # check the outcome if (!($res->is_success)) { print "Error: " . $res->status_line . "\n"; die; } #submit the url to the updated keywords $req = HTTP::Request->new(POST => "http://$googlehost:8000/EnterpriseController"); $req->header('Referrer' => "http://$googlehost:8000/EnterpriseController"); $req->content_type('application/x-www-form-urlencoded'); $req->content("keyMatchImportURL=$keymatchurl&keyMatchImportURLNow=%20Import%20KeyMatches%20Now%20&index=$collection&startRow=1&actionType=keyMatchImport&search="); $res = $ua->request($req); if ($res->content =~ /File imported successfully/){ print "import sucessful ($googlehost)\n"; } else { print "import failed ($googlehost)\n"; } #log out of the box $req = HTTP::Request->new(GET => "http://$googlehost:8000/EnterpriseController?actionType=logout"); $req->header('Referrer' => ''); $res = $ua->request($req);