Skip to content

Instantly share code, notes, and snippets.

@odd-poet odd-poet/gw2ssh-install.sh

Last active Dec 16, 2015
Embed
What would you like to do?
SKP용 GW스크립트
#!/bin/bash
gw2ssh_url="https://gist.github.com/odd-poet/5418622/raw/gw2ssh.sh"
filepath=$HOME/gw2ssh.sh
profile=$HOME/.bash_profile
echo "Download gw2ssh.sh script..."
curl -s $gw2ssh_url > $filepath
echo " done. "
installed=$(cat $profile | grep ". $filepath" | wc -l)
if [ $installed == 0 ]; then
echo "" >> $profile
echo "##############################" >> $profile
echo "# GW2SSH config " >> $profile
echo ". $filepath" >> $profile
echo "GATEWAY_ID= # set your id" >> $profile
echo "GATEWAY_PW= # set your pw" >> $profile
echo "LDAP_ID= # set your id" >> $profile
echo "LDAP_PW= # set your pw" >> $profile
fi
echo " "
echo "----------------------------------"
echo "set your account info in ~/.bash_profile"
echo "and execute 'source ~/.bash_profile' "
echo " - gw2ssh by <yunsang.choi@sk.com>"
# 서버로그인 스트립트 for SKP
#===============================
# @author : yunsang.choi@sk.com
# @version : 0.9
#
# * 환경
# - only for mac
# - bash
# - ivpn 접속된 상태 (Network Connector 이용)
# * 기능
# - 자동로그인
# - 서버목록 확인
# - 호스트명/IP 탭 자동완성
# * 사용법
# - gw2ssh : gateway를 통해 서버에 ssh로 로그인 (탭 자동완성 지원)
# - gw2list : 호스트목록 출력
# * 설치 및 설정
#
GW2SSH_VERSION=0.9
GATEWAY_IP=172.19.108.230
GATEWAY_PORT=20022
HOSTNAMES=()
IPADDRS=()
GATEWAY_ID=
GATEWAY_PW=
LDAP_ID=
LDAP_PW=
#=============================
# gateway 서버에 접근가능지 확인
# 성공: OK / 실패 : ERROR
#=============================
_check_gateway() {
output=$(expect -c "
set timeout 3
spawn ssh -p $GATEWAY_PORT $GATEWAY_ID@$GATEWAY_IP
expect \"password:\"
send \"$GATEWAY_PW\r\"
expect \"Input Device\"
send \"q\r\"
exit
")
result=$(echo $output | grep -o "Input Device" | wc -l)
if [ $result == 0 ]; then echo "ERROR"; fi
if [ $result == 1 ]; then echo "OK"; fi
}
#=============================
# HOSTNAMES, IPADDRS를 읽어온다.
# 전역변수에 로딩
#=============================
_load_server_list() {
output=$(expect -c "
set timeout 3
expect_after {
timeout { puts \"Cannot connect to gateway server($GATEWAY_IP)\"; exit }
}
spawn ssh -p $GATEWAY_PORT $GATEWAY_ID@$GATEWAY_IP
expect \"password:\"
send \"$GATEWAY_PW\r\"
expect \"Input Device\"
send \"q\r\"
exit
")
HOSTNAMES=()
IPADDRS=()
while read line; do
host=`echo $line | awk '{print $1}'`
ip=`echo $line | awk '{print $2}'`
HOSTNAMES+=($host)
IPADDRS+=($ip)
done < <(echo "${output[@]}" | grep -oE "[a-zA-Z0-9_-]+\\s+\\d+\\.\\d+\\.\\d+\\.\\d+")
}
_load_server_list_if_empty() {
if [ ${#HOSTNAMES[@]} == 0 ]; then
_load_server_list
fi
}
#=============================
# 호스트명을 IP로 변환한다.
# 일치하는 호스트명이 없으면
# 입력값을 그대로 리턴한다.
#=============================
_host_to_ip() {
local hostname=$1
host_count=${#HOSTNAMES[@]}
for (( i=0 ; i < $host_count ; i++ )) ; do
if [[ "${HOSTNAMES[i]}" == "$hostname" ]]; then
echo ${IPADDRS[i]}
return
fi
done
echo $hostname
}
#===================================
# gateway를 통해서 서버에 ssh로 로그인
#===================================
gw2ssh() {
echo "gw2ssh ver$GW2SSH_VERSION"
if [[ $(_check_gateway) != "OK" ]]; then
echo "Cannot connect to Gateway..."
return
fi
local target_server_ip=$1
if [[ "$target_server_ip" == "" ]]; then
echo "Usage: gw2ssh hostname(or ipaddress)"
return 0
fi
_load_server_list_if_empty
target_server_ip=$(_host_to_ip $target_server_ip)
echo "Try to connet to $target_server_ip as $LDAP_ID ..."
expect -c "
set timeout 5
log_user 0;
expect_after {
timeout { log_user 1; puts \"Error!!!!\"; 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_server_ip\r\"
expect \"Input Service\"
send \"s\r\"
expect \"Login:\"
send \"$LDAP_ID\r\"
expect \"Password:\"
send \"$LDAP_PW\r\"
log_user 1
interact
"
}
#===========================
# 서버 목록을 출력한다
#===========================
gw2list() {
_load_server_list_if_empty
host_count=${#HOSTNAMES[@]}
echo "Server List : $host_count"
for (( i=0; i < $host_count ; i++ )); do
echo " ${HOSTNAMES[i]} -> ${IPADDRS[i]}"
done
}
# 탭 자동완성
_host_completion() {
_load_server_list_if_empty
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
if [[ ${#COMP_WORDS[@]} == 2 ]] ; then
local hosts="${HOSTNAMES[@]} ${IPADDRS[@]}"
COMPREPLY=( $(compgen -W "$hosts" -- ${cur}) )
return 0
fi
}
complete -F _host_completion gw2ssh
@fupfin

This comment has been minimized.

Copy link

fupfin commented Jul 31, 2013

curl -s $gw2ssh_url > $filepath 다음에 chmod a+x $filepath 넣어 주세요.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.