Created
July 29, 2011 14:56
-
-
Save toritori0318/1113977 to your computer and use it in GitHub Desktop.
EC2へのssh接続を楽にするスクリプト VM::EC2バージョン
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use strict; | |
| use warnings; | |
| use Getopt::Long qw/GetOptions :config auto_help/; | |
| use Config::Pit; | |
| use VM::EC2; | |
| use Term::ReadLine; | |
| my $pitname = 'aws_info'; | |
| my $region = ''; | |
| my $user = ''; | |
| my $tags = ''; | |
| # オプションの処理 | |
| GetOptions( | |
| 'pitname=s' => \$pitname, | |
| 'region=s' => \$region, | |
| 'user=s' => \$user, | |
| 'tags=s' => \$tags, | |
| ); | |
| # Config取得 | |
| my ($aws_access_key_id, $aws_secret_access_key, $pem_file, $conf_region, $conf_user) = do { | |
| @{ Config::Pit::get( $pitname, require => { | |
| 'aws_access_key_id' => 'AWS ACCESS KEY ID', | |
| 'aws_secret_access_key' => 'AWS SECRET ACCESS KEY', | |
| 'pem_file' => 'PEM FILE PATH', | |
| } ) }{ qw/aws_access_key_id aws_secret_access_key pem_file region user / }; | |
| }; | |
| # デフォルト値の設定(引数優先) | |
| $region = ($conf_region) ? $conf_region : 'ap-northeast-1' if !$region; | |
| $user = ($conf_user) ? $conf_user : 'ec2-user' if !$user; | |
| my $ec2 = VM::EC2->new( | |
| -access_key => $aws_access_key_id, | |
| -secret_key => $aws_secret_access_key, | |
| -placement_zone => $region, | |
| ); | |
| sub main{ | |
| my %filter = ( | |
| 'instance-state-name' => 'running', | |
| ); | |
| $filter{'tag:Name'} = $tags if $tags; | |
| my @running_instances = $ec2->describe_instances( | |
| -filter => \%filter, | |
| ); | |
| if(scalar @running_instances == 0) { | |
| print "Not Found Running Instance. \n"; | |
| exit; | |
| } elsif(scalar @running_instances == 1) { | |
| exec_ssh($pem_file, $user, $running_instances[0]->dns_name); | |
| exit; | |
| } | |
| my $no = 0; | |
| printf ("No.%2d %-23s %-14s %-14s %-15s %s\n" , ++$no, $_->tags->{Name}, $_->instance_id, $_->ip_address, $_->private_ip_address, $_->dns_name) for @running_instances; | |
| my $term = Term::ReadLine->new('Input Number'); | |
| while (1) { | |
| my $input = $term->readline('Input No > '); | |
| last if $input =~ /^(q|quit|exit)$/; | |
| if ($input !~ /\d+/ || !$running_instances[$input-1]) { | |
| print "Invalid Number. \n"; | |
| next; | |
| } | |
| exec_ssh($pem_file, $user, $running_instances[$input-1]->dns_name); | |
| last; | |
| } | |
| } | |
| sub exec_ssh { | |
| my ($pem_file, $loginuser, $dns_name) = @_; | |
| my @cmd = ('ssh', '-i', $pem_file, '-l', $loginuser, $dns_name); | |
| print join(' ', @cmd), "\n"; | |
| exec(join(' ', @cmd)); | |
| } | |
| main(); | |
| __END__ | |
| =head1 SYNOPSIS | |
| ssh_vmec2.pl [options] | |
| Options: | |
| --help display help message | |
| --pitname pit name (default : aws_info) | |
| --region ec2 region (default : ap-northeast-1 [tokyo]) | |
| --user ssh login user (default : osuser name) | |
| --tags ec2 instance tags | |
| Requies: | |
| Config::Pit | |
| Format | |
| "aws_info": | |
| "aws_access_key_id": 'AAAAAAAAAAAAAAAAAAA' | |
| "aws_secret_access_key": 'BBBBBBBBBBBBBBBBBB' | |
| "pem_file": '/home/user/.ec2/keypair.pem' | |
| "region": 'ap-northeast-1' (option) | |
| "user": 'appuser' (option) | |
| Create One Liner | |
| perl -MConfig::Pit -e'Config::Pit::set("aws_info", data=>{ aws_access_key_id => "AAAAAAAAAAAAAAAAAAA", aws_secret_access_key => "BBBBBBBBBBBBBBBBBB", pem_file => "/home/user/.ec2/keypair.pem" })'; | |
| =cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment