#!/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 $PRIVATE_KEY 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 $PRIVATE_KEY -L $LOCAL_PORT:localhost:8888 -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"); } /// DEMO OUTPUT FOR PARSING //////////////////////////////////////////////////// /* $ ec2-run-instances $AMI -K keypair RESERVATION r-3a48a857 690724809999 default INSTANCE i-cf3f87a3 ami-2d4aa444 pending 0 m1.small 2011-01-28T00:40:38+0000 us-east-1b aki-754aa41c monitoring-disabled instance-store $ ec2-describe-instances RESERVATION r-907a9afd 690724809999 default INSTANCE i-7f4ff713 ami-2d4aa444 terminated keypair 0 m1.small 2011-01-28T00:19:38+0000 us-east-1c aki-754aa41c monitoring-disabled instance-store RESERVATION r-3a48a857 690724809999 default INSTANCE i-cf3f87a3 ami-2d4aa444 ec2-50-16-143-122.compute-1.amazonaws.com domU-12-31-39-03-44-65.compute-1.internal running 0 m1.small 2011-01-28T00:40:38+0000 us-east-1b aki-754aa41c monitoring-disabled 50.16.143.122 10.249.71.147 instance-store $ ec2-terminate-instances i-fe8ce087 INSTANCE i-fe8ce087 running shutting-down $ sudo apt-get install tinyproxy Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: tinyproxy 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 60.1 kB of archives. After this operation, 200 kB of additional disk space will be used. Get:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ precise/universe tinyproxy i386 1.8.3-1 [60.1 kB] Fetched 60.1 kB in 0s (311 kB/s) Selecting previously unselected package tinyproxy. (Reading database ... 25420 files and directories currently installed.) Unpacking tinyproxy (from .../tinyproxy_1.8.3-1_i386.deb) ... Processing triggers for initramfs-tools ... update-initramfs: Generating /boot/initrd.img-3.2.0-23-virtual Processing triggers for man-db ... Processing triggers for ureadahead ... Setting up tinyproxy (1.8.3-1) ... Starting tinyproxy: tinyproxy. $ sudo apt-get install tinyproxy Reading package lists... Done Building dependency tree Reading state information... Done tinyproxy is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. $ http_proxy=http://localhost:8888 wget -O /dev/null http://www.google.com/ 2>&1 --2012-06-16 00:40:22-- http://www.google.com/ Resolving localhost (localhost)... 127.0.0.1 Connecting to localhost (localhost)|127.0.0.1|:8888... failed: Connection refused. */ ?>