Skip to content

Instantly share code, notes, and snippets.

@sethryder
Created April 12, 2016 15:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sethryder/eb6b88398990dc7cff330bc8ff38597e to your computer and use it in GitHub Desktop.
Save sethryder/eb6b88398990dc7cff330bc8ff38597e to your computer and use it in GitHub Desktop.
a quickly hacked together script to generate a .ssh/config from aws instances
<?php
$ssh_instances = [];
$ssh_user = 'root';
require 'vendor/autoload.php';
$ec2 = new Aws\Ec2\Ec2Client([
'version' => 'latest',
'region' => 'us-east-1'
]);
$describe_instances = $ec2->DescribeInstances();
foreach ($describe_instances['Reservations'] as $reservation) {
foreach ($reservation['Instances'] as $instance) {
if (isset($instance['PublicIpAddress'])) {
$public_ip = $instance['PublicIpAddress'];
}
foreach ($instance['Tags'] as $tag) {
if ($tag['Key'] == 'Name') {
//Strip any white spaces/comments that may be in the name.
$name = strtok($tag['Value'],' ');
$instance_name = $name;
}
}
if (isset($instance_name) && isset($public_ip)) {
$ssh_instances[] = [
'instance_name' => $instance_name,
'public_ip' => $public_ip,
];
}
if (isset($public_ip)) unset($public_ip);
if (isset($instance_name)) unset($instance_name);
}
}
/*
Host example
HostName 54.100.100.100
User ubuntu
IdentityFile ~/.ssh/example.aws.pem
*/
$ssh_config = '';
foreach ($ssh_instances as $ssh_instance) {
$instance_name = $ssh_instance['instance_name'];
$public_ip = $ssh_instance['public_ip'];
$ssh_config .= "Host $instance_name\n";
$ssh_config .= " HostName $public_ip\n";
$ssh_config .= " User $ssh_user\n";
$ssh_config .= "\n";
}
print_r($ssh_config);
@sethryder
Copy link
Author

again, hacked this up really quick at work.

could be improved greatly and would like to do so in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment