|
#################################################### |
|
# @author : [email protected] / [email protected] |
|
# @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 [email protected])" |
|
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 |
|
} |
|
|