ssh到ec2实例中。
ssh-aws的Python项目详细描述
awssh
awssh
使用boto3列出aws实例,并基于
“密钥名”。它支持aws cli--profile
和--region
选项以及环境变量
(aws_default_region、aws_default_profile等)指定配置选项和凭据。
➜ ~ awssh
Querying AWS for EC2 instances in default region...
Name Instance ID Public IP Private IP Zone Key Name
0 - JumpHost-1 i-06755dff7 3.13.18.215 10.120.1.119 us-east-2a project-100715
1 - WinStack i-088fbe001 None 10.120.1.40 us-east-2a project-100715
2 - JumpHost-2 i-00498e224 48.14.28.186 10.120.0.8 us-east-2a project-101018
3 - PCoIP i-0b79c1a8f None 10.120.1.46 us-east-2a project-101018
4 - Dyna-dyna i-03ed2e9df None 10.120.1.7 us-east-2a project-101018
5 - ECS Instance i-01b2877c1 None 10.120.1.55 us-east-2a project-101018
6 - MySQL-PROD i-01b2877c1 None 10.120.1.55 us-east-2a project-100715
7 - codeCommit i-06755dff7 None 10.120.1.119 us-east-2a project-100715
Enter server number: 7
Connecting to codeCommit via JumpHost-1:
[centos@code-commit ~]$
安装
首先,安装aws cli和boto3库:
pip3 install awscli --upgrade --user pip3 install boto3
接下来,设置aws凭据、默认配置文件和默认区域:
aws configure
注意:配置
AWS Profiles
才能使用--profile
选项和aws_default_profile环境变量。
安装awssh
实用程序:
pip3 install ssh-aws --user # or pip3 install ssh-aws
最后,根据您的python版本,确保$HOME/Library/Python/<version>/bin
是path的一部分。
echo$PATH
note:awssh
实用程序安装在$HOME/Library/Python/<version>/bin
中,可能不在您的路径上。
版本更新
echo y | pip3 uninstall ssh-aws pip3 install ssh-aws --no-cache --user # or pip3 install --no-cache ssh-aws awssh --version
要求
- Python
- boto3
- aws-cli
- OpenSSH 7.3
功能
awssh
实用程序为您提供了一个aws实例列表和带有正确键和用户的预配置。
可以按实例名称筛选。如果它只匹配一个实例,您将登录到它。如果一个实例
如果未选择外部IP,该实用程序将尝试查找其跳转服务器和带有ProxyJump的ssh
配置指令(例如ssh -A -J user@<jump.host> user@<target.host>
)。代理转发应该是
小心使用。awssh
还允许本地和远程端口转发用于ssh隧道
(例如ssh -L 9000:imgur.com:80 user@<ip_address>
)。
用法
usage: awssh [-h] [--users USERS [USERS ...]] [--profile PROFILE]
[--region REGION] [-i KEY_PATH] [-c COMMAND]
[-r REMOTE_HOST] [-p REMOTE_PORT] [-l LOCAL_PORT]
[--keys KEYS] [--timeout TIMEOUT] [--console-output]
[--version] [--verbose]
[filter]
SSH into AWS instances. "awssh --profile prod-acc-2 --users fduran --region
us-east-2 instance-name". The default user list is centos, ubuntu, and
ec2-user. "awssh --profile prod-acc-2" will attempt ssh with default users.
Due to the nature of nargs, "awssh --users user1 user2 instance-name" will not
be parsed properly; instead try "awssh instance-name --users user1 user2". If
available, a "JumpHost" will be automatically chosen when the instance
selected has no external IP. You can also explicitly direct the JumpHost by
providing two selections from the list, i.e. Enter server number: <jump>
<target>).
positional arguments:
filter Optional instance name or key word as a filter. If
only one instance is found, it will connect to it
directly.
optional arguments:
-h, --help show this help message and exit
--users USERS [USERS ...]
Users to try (centos, ubuntu, and ec2-user are
defaults).
--profile PROFILE Use a specific profile from your credentials file.
--region REGION AWS region (User default if none is provided).
-i KEY_PATH, --key-path KEY_PATH
Specific key path, overrides, --keys
-c COMMAND, --command COMMAND
Translates to ssh -t <COMMAND>
-r REMOTE_HOST, --remote-host REMOTE_HOST
Open a tunnel. Equivalent to ssh -L <local-port
>:<remote-host>:<remote-port> <selected-aws-host>
-p REMOTE_PORT, --remote-port REMOTE_PORT
Port to use on the remote host (default is 5432).
-l LOCAL_PORT, --local-port LOCAL_PORT
Port to use on the local host. Get overwritten by
remote port if not defined.
--keys KEYS Directory of the private keys (~/.ssh by default).
--timeout TIMEOUT SSH connection timeout.
--console-output, -o Display the instance console out before logging in.
--version, -v Returns awssh's version.
--verbose, -V Verbose, prints instance details.
Examples:
awssh
awssh --profile prod-acc-2
awssh --users fduran --profile prod-acc-2 --region us-east-2 -c top
awssh --users user1 user2 --region us-east-2 --keys '~/.ssh' instance-name
awssh --users user1 user2 -c 'df -h' --verbose