Skip to content

Commit

Permalink
Merge pull request #10 from h3poteto/iss-9
Browse files Browse the repository at this point in the history
closes #9 Add option which specify private key path when use proxy
  • Loading branch information
h3poteto authored Apr 10, 2018
2 parents 47e52f6 + 6433482 commit e4abbca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,19 @@ Please write shortcut setting in your `~/.zshrc`:
AWS_PROFILE_NAME=production
AWS_DEFAULT_REGION=ap-northeast-1
SSH_USER=h3poteto
SSH_PRIVATE_KEY_PATH=/var/opt/keys/.ssh/private.pem
SSH_PORT=22
AWS_PROXY_PROFILE=proxy
SSH_PROXY_USER=proxy-login-user
SSH_PRIVATE_KEY_PATH=$HOME/.ssh/id_rsa
SSH_PROXY_PRIVATE_KEY_PATH=$HOME/.ssh/id_rsa
SSH_PROXY_PORT=22

function zsh-ec2ssh-production-proxy() { zsh-ec2ssh-with-proxy $AWS_PROFILE_NAME $AWS_DEFAULT_REGION $SSH_USER $AWS_PROXY_PROFILE $SSH_PROXY_USER $SSH_PRIVATE_KEY_PATH $SSH_PROXY_PORT }
function zsh-ec2ssh-production-proxy() { zsh-ec2ssh-with-proxy $AWS_PROFILE_NAME $AWS_DEFAULT_REGION $SSH_USER $SSH_PRIVATE_KEY_PATH $SSH_PORT $AWS_PROXY_PROFILE $SSH_PROXY_USER $SSH_PROXY_PRIVATE_KEY_PATH $SSH_PROXY_PORT }
zle -N zsh-ec2ssh-production-proxy
bindkey '^p' zsh-ec2ssh-production-proxy # Ctrl + p
```
Your machine --(ssh using `SSH_PROXY_USER`, `SSH_PRIVATE_KEY_PATH` and `SSH_PROXY_PORT`)--> proxy server --(ssh using `SSH_USER` and `id_rsa`)--> target server
Your machine --(ssh using `SSH_PROXY_USER`, `SSH_PROXY_PRIVATE_KEY_PATH` and `SSH_PROXY_PORT`)--> proxy server --(ssh using `SSH_USER`, `SSH_PRIVATE_KEY_PATH` and `SSH_PORT`)--> target server
## Arguments
### zsh-ec2ssh
Expand All @@ -105,9 +107,11 @@ Your machine --(ssh using `SSH_PROXY_USER`, `SSH_PRIVATE_KEY_PATH` and `SSH_PROX
|AWS_PROFILE_NAME|AWS profile name which target instances are located.|`AWS_DEFAULT_PROFILE` environment variables|
|AWS_DEFAULT_REGION|AWS region name which target instances are located.|`AWS_DEFAULT_REGION` environment variables|
|SSH_USER|User name to login target server.|`USER` environment variables|
|SSH_PRIVATE_KEY_PATH|Private key path which is used login to target server.|`$HOME/.ssh/id_rsa`|
|SSH_PORT|Port which is used login to target server.|22|
|AWS_PROXY_PROFILE|AWS profile name which proxy server are located.|`AWS_DEFAULT_PROFILE` environment variables|
|AWS_PROXY_USER|User name to login proxy server.|`USER` environment variables|
|SSH_PRIVATE_KEY_PATH|Private key path to login proxy server.|`$HOME/.ssh/id_rsa`|
|SSH_PROXY_USER|User name to login proxy server.|`USER` environment variables|
|SSH_PROXY_PRIVATE_KEY_PATH|Private key path to login proxy server.|`$HOME/.ssh/id_rsa`|
|SSH_PROXY_PORT|Port which is used ssh login to proxy server.|22|
## License
Expand Down
57 changes: 33 additions & 24 deletions zsh-ec2ssh.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,22 @@ function _load_port() {
function zsh-ec2ssh() {
local aws_profile_name=$1
local aws_region=$2
local ssh_user=$3
local ssh_private_key_path=$4
local ssh_port=$5
local ssh_proxy=$6
local target_user=$3
local target_private_key_path=$4
local target_port=$5
local proxy_host=$6
local proxy_user=$7
local proxy_key_path=$8
local proxy_port=$9

aws_profile_name=`_load_aws_profile $aws_profile_name`
aws_region=`_load_aws_region $aws_region`
ssh_user=`_load_user $ssh_user`
ssh_private_key_path=`_load_ssh_private_key_path $ssh_private_key_path`
ssh_port=`_load_port $ssh_port`
target_user=`_load_user $target_user`
target_private_key_path=`_load_ssh_private_key_path $target_private_key_path`
target_port=`_load_port $target_port`
proxy_user=`_load_user $proxy_user`
proxy_key_path=`_load_ssh_private_key_path $proxy_key_path`
proxy_port=`_load_port $proxy_port`

if [ -z "${aws_profile_name}" ]; then
echo "AWS profile name is required. Please call this function with aws profile name or set AWS_DEFAULT_REGION in evironment variables."
Expand All @@ -68,18 +73,18 @@ function zsh-ec2ssh() {
return
fi

if [ -z "${ssh_user}" ]; then
if [ -z "${target_user}" ]; then
echo "User is required. Please call this function with user or set USER in environment variables."
return
fi

echo "Fetching ec2 host..."
local selected_host=$(myaws ec2 ls --profile=${aws_profile_name} --region=${aws_region} --fields='InstanceId PublicIpAddress LaunchTime Tag:Name Tag:attached_asg' | sort -k4 | peco | cut -f2)
if [ -n "${selected_host}" ]; then
if [ -z "${ssh_proxy}" ]; then
BUFFER="ssh -i ${ssh_private_key_path} -p ${ssh_port} ${ssh_user}@${selected_host} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
if [ -z "${proxy_host}" ]; then
BUFFER="ssh -i ${target_private_key_path} -p ${target_port} ${target_user}@${selected_host} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
else
BUFFER="ssh -i ${ssh_private_key_path} -p ${ssh_port} -t ${proxy_user}@${ssh_proxy} ssh ${ssh_user}@${selected_host} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
BUFFER="ssh -i ${proxy_key_path} -p ${proxy_port} -t ${proxy_user}@${proxy_host} ssh ${target_user}@${selected_host} -i ${target_private_key_path} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
fi
if zle; then
zle accept-line
Expand All @@ -96,21 +101,25 @@ zle -N zsh-ec2ssh
function zsh-ec2ssh-with-proxy() {
local aws_profile_name=$1
local aws_region=$2
local ssh_user=$3
local ssh_proxy_profile=$4
local proxy_user=$5
local ssh_private_key_path=$6
local ssh_proxy_port=$7
local target_user=$3
local target_private_key_path=$4
local target_port=$5
local proxy_profile=$6
local proxy_user=$7
local proxy_key_path=$8
local proxy_port=$9

aws_profile_name=`_load_aws_profile $aws_profile_name`
aws_region=`_load_aws_region $aws_region`
ssh_user=`_load_user $ssh_user`
ssh_proxy_profile=`_load_aws_profile $ssh_proxy_profile`
target_user=`_load_user $target_user`
target_private_key_path=`_load_ssh_private_key_path $target_private_key_path`
target_port=`_load_port $target_port`
proxy_profile=`_load_aws_profile $proxy_profile`
proxy_user=`_load_user $proxy_user`
ssh_private_key_path=`_load_ssh_private_key_path $ssh_private_key_path`
ssh_proxy_port=`_load_port $ssh_proxy_port`
proxy_key_path=`_load_ssh_private_key_path $proxy_key_path`
proxy_port=`_load_port $proxy_port`

if [ -z "${aws_profile_name}" -o -z "${ssh_proxy_profile}" ]; then
if [ -z "${aws_profile_name}" -o -z "${proxy_profile}" ]; then
echo "AWS profile name is required. Please call this function with aws profile name or set AWS_DEFAULT_REGION in evironment variables."
return
fi
Expand All @@ -120,15 +129,15 @@ function zsh-ec2ssh-with-proxy() {
return
fi

if [ -z "${ssh_user}" -o -z "${proxy_user}" ]; then
if [ -z "${target_user}" -o -z "${proxy_user}" ]; then
echo "User is required. Please call this function with user or set USER in environment variables."
return
fi

echo "Fetching ec2 host..."
local selected_proxy=$(myaws ec2 ls --profile=${ssh_proxy_profile} --region=${aws_region} --fields='InstanceId PublicIpAddress LaunchTime Tag:Name Tag:attached_asg' | sort -k4 | peco | cut -f2)
local selected_proxy=$(myaws ec2 ls --profile=${proxy_profile} --region=${aws_region} --fields='InstanceId PublicIpAddress LaunchTime Tag:Name Tag:attached_asg' | sort -k4 | peco | cut -f2)
if [ -n "${selected_proxy}" ]; then
zsh-ec2ssh $aws_profile_name $aws_region $ssh_user $ssh_private_key_path $ssh_proxy_port $selected_proxy $proxy_user
zsh-ec2ssh $aws_profile_name $aws_region $target_user $target_private_key_path $target_port $selected_proxy $proxy_user $proxy_key_path $proxy_port
fi
if zle; then
zle clear-screen
Expand Down

0 comments on commit e4abbca

Please sign in to comment.