#!/usr/bin/php &1`; echo "."; if (preg_match("/OKAY/s",$out)) { break; } } echo " ok.\n"; // Install the TinyProxy software. No further installation/setup needed! // Parse the output of 'apt-get' for 'Now installed'/... echo "[".date("H:i:s")."] Installing TinyProxy via SSH ... "; $out = `ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $LOGIN@{$machine['dns']} -i "$AWS_KEYPAIR_FILE" sudo apt-get install tinyproxy 2>&1`; if (preg_match("/Starting tinyproxy: tinyproxy./s",$out)) { echo "Now installed.\n"; } else if (preg_match("/tinyproxy is already the newest version./s",$out)) { echo "Already installed.\n"; } else { die("Unknown output. ABORTING.\n$out\n"); } // Open SSH tunnel in the background echo "[".date("H:i:s")."] Starting SSH Tunnel to Proxy at localhost:$LOCAL_PORT ... "; echo `ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $LOGIN@{$machine['dns']} -i "$AWS_KEYPAIR_FILE" -L $LOCAL_PORT:localhost:$LOCAL_PORT -N >/dev/null 2>&1 &`; echo "started.\n"; // Test proxy by locally 'wget'ing the Google website via the proxy echo "[".date("H:i:s")."] Testing proxy localhost:$LOCAL_PORT ..."; while (true) { $out = `http_proxy=http://localhost:$LOCAL_PORT wget -O /dev/null http://www.google.com/ 2>&1`; echo "."; if (!preg_match("/Connection refused/s",$out)) { break; } sleep(1); } echo " works!\n"; } // OPTION 'stop' else if ($argv[1] == "stop") { $instance = findInstance(); // Not running if (!$instance) { exit; } // Running: Now call 'ec2-terminate-instances' echo "[".date("H:i:s")."] Terminating instance {$instance['instance']} ... "; $out = `ec2-terminate-instances {$instance['instance']}`; if (preg_match("/{$instance['instance']} running shutting-down/s",$out)) { echo "Shutting down.\n"; } else { echo "Unknown status.\n$out"; } } // OPTION 'status' else if ($argv[1] == "status") { $instance = findInstance(); } // Invalid usage else { showUsage(); } exit; /// HELPER FUNCTIONS /////////////////////////////////////////////////////////// function findInstance($instance = false, $silent = false) { global $AMI; if ($instance) { if (!$silent) echo "[".date("H:i:s")."] Check for running instance $instance ..."; } else { if (!$silent) echo "[".date("H:i:s")."] Check for running instance of $AMI ..."; $instance = "\S+"; } // Already running? if (preg_match("/INSTANCE ($instance) $AMI (\S+) \S+ running/s",`ec2-describe-instances`,$m)) { $instance = $m[1]; $dns = $m[2]; if (!$silent) echo " $instance running at $dns\n"; return array( "instance" => $instance, "dns" => $dns ); } if (!$silent) echo " Not running.\n"; return false; } function showUsage() { die("Usage: {$_SERVER['PHP_SELF']} (start|stop|status)\n"); } ?>