Skip to content

Instantly share code, notes, and snippets.

@odd-poet
Last active December 18, 2015 02:29
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 odd-poet/5711863 to your computer and use it in GitHub Desktop.
Save odd-poet/5711863 to your computer and use it in GitHub Desktop.
gw2ssh for zsh(oh-my-zsh)

gw2ssh for zsh

oh-my-zsh 플러그인으로 만들어진 zsh용 gw2ssh

설치방법

curl https://gist.github.com/odd-poet/5711863/raw/install.sh | sh 

설치후 설정

  • 설치후에 .gw2sh 파일에 자신의 계정정보를 입력한다.
  • .zshrc의 plugins 설정에 gw2ssh를 넣는다.
plugins=(gw2ssh)

사용법

  • gw2ssh --load-hosts
    • 호스트 정보를 로딩한다. 한번 로딩하고 나면 <TAB> 자동완성으로 호스트명을 사용할 수 있다.
  • gw2ssh host
    • 주어진 호스트에 Gateway를 통해 ssh 연결을 한다.
#compdef gw2ssh
_arguments "1: :($(echo ${HOST_IP_LIST[@]}))"
####################################################
# @author : yunsang.choi@sk.com / oddpoet@gmail.com
# @source : https://gist.github.com/odd-poet/5711863
####################################################
GATEWAY_IP=172.19.108.230
GATEWAY_PORT=20022
HOST_IP_LIST=(--load-hosts)
__GW2SSH_VERSION="1.1.0-for-zsh"
source ~/.gw2ssh
gw2ssh() {
local target_host_ip=$1
echo "${fg[red]}GW2SSH ver${__GW2SSH_VERSION}${reset_color} (by yunsang.choi@sk.com)"
if [[ $target_host_ip == '' ]]; then
echo "Usage: gw2ssh host_or_ip"
return
fi
if [[ $target_host_ip == '--load-hosts' ]]; then
_load_gw2_host_list
return
fi
local target_ip=`echo $target_host_ip | awk -F '/' '{print $2}'`
if [[ $target_ip == '' ]]; then
target_ip=`echo $target_host_ip | awk -F '/' '{print $1}'`
fi
echo "-----"
echo "Connect to ${fg[green]}${LDAP_ID}@${fg[red]}$target_ip $reset_color through the Gateway"
expect -c "
set timeout 5
log_user 0;
expect_after {
timeout { log_user 1; puts \"Timeout Error1\"; exit}
}
spawn ssh -p $GATEWAY_PORT $GATEWAY_ID@$GATEWAY_IP
expect \"password:\"
send \"$GATEWAY_PW\r\"
expect \"Input Device\"
send \"i\r\"
expect \"Input ipaddress\"
send \"$target_ip\r\"
expect \"Input Service\"
send \"s\r\"
expect \"Login:\"
send \"$LDAP_ID\r\"
expect \"Password:\"
send \"$LDAP_PW\r\"
log_user 1
interact
"
}
_load_gw2_host_list() {
HOST_IP_LIST=()
output=$(expect -c "
set timeout 3
expect_after {
timeout { puts \"Cannot connect to gateway server($GATEWAY_IP)\"; exit }
}
puts \"ssh -p $GATEWAY_PORT $GATEWAY_ID@$GATEWAY_IP\"
spawn ssh -p $GATEWAY_PORT $GATEWAY_ID@$GATEWAY_IP
expect \"password:\"
send \"$GATEWAY_PW\r\"
expect -re \"page : (\[0-9\]+)\/(\[0-9\]+)\"
set pages [expr \$expect_out(2,string)]
expect \"Input Device\"
for {set i 1} {\$i < \$pages} {incr i 1} {
send \"n\r\"
expect \"Input Device\"
}
send \"q\r\"
exit
")
echo "Hosts: "
for line in $(echo "${output[@]}" | grep -oE "[a-zA-Z0-9-]+\s+\d+\.\d+\.\d+\.\d+" | awk '{print $1"/"$2}' | sort | uniq); do
HOST_IP_LIST+=$line
host=`echo $line | awk -F "/" '{print $1}'`
ip=`echo $line | awk -F "/" '{print $2}'`
display="${fg[red]}${host}${reset_color}/${ip}"
echo " > $display"
done
}
#!/bin/sh
gist_baseurl="https://gist.github.com/odd-poet/5711863/raw/"
plugin_path=$HOME/.oh-my-zsh/plugins/gw2ssh
mkdir -p $plugin_path
echo "Download gw2ssh plugin ..."
curl -s $gist_baseurl/gw2ssh.plugin.zsh > $plugin_path/gw2ssh.plugin.zsh
curl -s $gist_baseurl/_gw2ssh > $plugin_path/_gw2ssh
echo "done. "
config_file="$HOME/.gw2ssh"
if [[ ! -f $config_file ]] ; then
echo "# account info for gw2ssh " > $config_file
echo "GATEWAY_ID= # set your id" >> $config_file
echo "GATEWAY_PW= # set your pw" >> $config_file
echo "LDAP_ID= # set your id" >> $config_file
echo "LDAP_PW= # set your pw" >> $config_file
fi
echo " "
echo "----------------------------------"
echo "How to configure"
echo " 1. Set your account info in ~/.gw2ssh"
echo " 2. Enable gw2ssh plugin (edit ~/.zshrc)"
echo " 3. Execute 'source ~/.zshrc' "
echo " "
echo " >>> gw2ssh-for-zsh by <yunsang.choi@sk.com>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment