Skip to content

Instantly share code, notes, and snippets.

@hartfordfive
Last active September 12, 2016 14:15
Show Gist options
  • Select an option

  • Save hartfordfive/2fbcad191ff907fe684aae6fa5022d67 to your computer and use it in GitHub Desktop.

Select an option

Save hartfordfive/2fbcad191ff907fe684aae6fa5022d67 to your computer and use it in GitHub Desktop.
SSH into all servers from an auto-scaling group via iTerm on Mac
#!/usr/bin/php
<?php
$sshKey = "~/.ssh/id_rsa";
$sshUser = getenv('USER'); // Get the user from your environment variables
$autoScalingGroups = $argv[1]; // A blob pattern of the auto scalling group name to match
$userPrivateIp = true; // Use the VPC private IP to connect?
$profile = getenv('WORKENV');
$cmd = "aws --profile $profile ec2 describe-instances --filter \"Name=instance-state-name,Values=running\" \"Name=tag-key,Values=aws:autoscaling:groupName\" \"Name=tag-value,Values=$autoScalingGroups\"";
echo "Running: $cmd\n";
$instances = json_decode(`$cmd`, true);
$count = 0;
$ascript_body = "";
$name = "";
foreach($instances['Reservations'] as $instance) {
foreach($instance['Instances'] as $inst) {
$ip = $userPrivateIp ? $inst['PrivateIpAddress'] : $inst['PublicIpAddress'];
foreach($inst['Tags'] as $tag) {
if ($tag['Key'] == "Name") {
$name = $tag['Value'];
break 1;
}
}
echo "Openning ssh to $ip (Name: $name)\n";
$ascript_body = $ascript_body."
launch session \"Default Session\"
tell the last session
set name to \"$name\"
do shell script \"workenv $profile\"
write text \"ssh $sshUser@$ip -i $sshKey\"
write text \"sudo su -\"
do shell script \"/bin/sleep 0.01\"
end tell\n
";
$count++;
}
}
passthru(
"/usr/bin/osascript <<-EOF
set curr_tab to 1
tell application \"iTerm\"
set myterm to (make new terminal)
tell myterm
$ascript_body
end tell
end tell
EOF
"
);
echo "---------------------------------------\n";
echo "Connected to $count instances\n";
echo "---------------------------------------\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment