Skip to content

Commit 34d2ef2

Browse files
author
richardyu
committed
#1 init repo, commit version 1.0
0 parents  commit 34d2ef2

File tree

4 files changed

+274
-0
lines changed

4 files changed

+274
-0
lines changed

.go.conf.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# IP USER:PASS Label
2+
192.168.1.7 user1:pass1 label:7
3+
192.168.1.8 user2:pass2 label:8
4+

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
Go
2+
==
3+
4+
What is Go?
5+
-----------
6+
7+
This Go is not the programming language from Google. This is a shell command which can help manage multiple remote servers SSH access easily, and do not need to remember so many accounts for those servers.
8+
9+
OS requirements
10+
---------------
11+
12+
The command tested on MacOSX, but should be working on most linux distributions. It depends on expect on your local system, and the remote servers need to enable SSH.
13+
14+
If you feels something wrong, please [file an issue](https://github.com/vipzhicheng/go/issues/new) on this project, because I do not test the scripts on every case, but I am happy to help you out.
15+
16+
Usage
17+
-----
18+
19+
I am just showing the usage for MacOSX users, other OS usage maybe different, but the idea is same.
20+
21+
### Check out code from Github
22+
23+
```
24+
$ mkdir ~/bin
25+
$ cd ~/bin
26+
$ git clone https://github.com/vipzhicheng/go.git
27+
$ cd go
28+
$ cp .go.conf.example ~/.go.conf
29+
$ chmod a+x go
30+
$ chmod a+x ssh-expect
31+
```
32+
33+
### Set PATH in .bash_profile
34+
35+
```
36+
export PATH=~/bin/go:~/bin:$PATH
37+
```
38+
39+
```
40+
$ source ~/.bash_profile
41+
```
42+
43+
### Set ~/.go.conf, you can see demo settings as follows.
44+
45+
```
46+
# IP USER:PASS LABEL
47+
48+
192.168.1.7:22000 user1:pass1 label:7
49+
192.168.1.8 user2:pass2 label:8
50+
192.168.1.9 user3::absolute_private_file_path label:9
51+
```
52+
53+
You can ignore port setting if you are using default port(22) in remote server.
54+
55+
### How to use this command
56+
57+
```
58+
$ go label
59+
60+
Found follow servers: (Which one do you want to connect?)
61+
[1] user1@192.168.1.7 label:7
62+
[2] user2@192.168.1.8 label:8
63+
Please choose by ID:
64+
1
65+
66+
Logging into user1@192.168.1.7 ...
67+
spawn ssh user1@192.168.1.7 -p 22000
68+
user1@192.168.1.7's password:
69+
Last login: Mon Mar 10 18:35:02 2014 from 192.168.1.6
70+
$
71+
```
72+
73+
### Options
74+
75+
There is only one option, -g, with this option, you can add -D7070 to the connection.
76+
77+
### Change log
78+
79+
2015-06-01
80+
1. Ignore comments lines
81+
2. Add support for secret file
82+
83+
Inspiration & Thanks
84+
--------------------
85+
86+
I know it must be somewhere about the situation of manage multiple SSH accesses via expect. Then I found [this](http://imbugs.com/blog/articles/99.html), which is written in Chinese. Thanks for the code, most of this project is from that code, but some features I need are missing. so I added them into this project.
87+
88+

go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/bin/bash
2+
3+
# Define config directory and config path
4+
GO_HOME=~
5+
AUTHFILE=$GO_HOME/.go.conf
6+
7+
# Parse options
8+
while getopts gh ARGS
9+
do
10+
case $ARGS in
11+
g)
12+
extra_options="-D7070"
13+
shift
14+
;;
15+
h)
16+
echo "usage: go [-gh] foo bar"
17+
exit 0;
18+
;;
19+
*)
20+
echo "Unknow option: $ARGS"
21+
echo "usage: go [-gh] foo bar"
22+
exit 1;
23+
;;
24+
esac
25+
done
26+
27+
# Config search function
28+
# Support AND logic, separated by blank char
29+
GREP()
30+
{
31+
32+
if [ -z "$*" ]; then
33+
var="^[^#].*"
34+
tmp_var=`cat $AUTHFILE | grep -i $var`
35+
echo "$tmp_var"
36+
return
37+
fi
38+
39+
index=1;
40+
for var in $@
41+
do
42+
if [[ "$var" =~ ^- ]];then
43+
index=$((index+1));
44+
continue
45+
fi
46+
47+
if [ "$var" = "${!index}" ];then
48+
var="^[^#].*$var.*"
49+
tmp_var=`grep -i $var $AUTHFILE`
50+
else
51+
var="^[^#].*$var.*"
52+
tmp_var=`echo "$tmp_var" | grep -i $var`
53+
fi
54+
done
55+
56+
echo "$tmp_var"
57+
}
58+
59+
# Get choice
60+
GET_CHAR()
61+
{
62+
read choice
63+
echo $choice;
64+
}
65+
66+
# Support input keywords by arguments or read from command line input
67+
if [ -z $1 ];then
68+
echo -n "Please input the server IP or Label: "
69+
read target
70+
else
71+
target=$@
72+
fi
73+
74+
# Parse config search result
75+
count=`GREP $target | wc -l`
76+
targetfullname=`GREP $target | awk '{print $1}' | awk -F ':' '{print $1}'`
77+
port=`GREP $target | awk '{print $1}' | awk -F ':' '{if ($2 > "") {print $2} else {print 22}}'`
78+
passwd=`GREP $target | awk '{print $2}' | awk -F ':' '{if ($2 > "") {print $2} else {print "-"}}'`
79+
sshkey=`GREP $target | awk '{print $2}' | awk -F ':' '{if ($3 > "") {print $3} else {print "-"}}'`
80+
user=`GREP $target | awk '{print $2}' | awk -F ':' '{print $1}'`
81+
label=`GREP $target | awk '{print $3}'`
82+
83+
# Process the case of more than one items in search results.
84+
if [ $count -gt 1 ];then
85+
echo -e 'Found follow servers: (\033[0;31mWhich one do you want to connect?\033[0m)'
86+
87+
arrtarget=($targetfullname)
88+
arruser=($user)
89+
arrpasswd=($passwd)
90+
arrlabel=($label)
91+
arrport=($port)
92+
arrsshkey=($sshkey)
93+
94+
length=${#arrtarget[@]}
95+
96+
for ((i=0; i<$length; i++))
97+
do
98+
echo -e '[\033[4;34m'$(($i+1))'\033[0m]' "${arruser[$i]}@${arrtarget[$i]}:${arrport[$i]} ${arrlabel[$i]}"
99+
done
100+
101+
# Choose one from search results
102+
echo -n "Please choose by ID: "
103+
choice=`GET_CHAR`
104+
echo ""
105+
106+
echo $choice;
107+
108+
if [[ "$choice" =~ ^[0-9]+$ ]]; then
109+
echo '';
110+
else
111+
exit 1;
112+
fi
113+
114+
targetfullname=${arrtarget[$(($choice-1))]}
115+
passwd=${arrpasswd[$(($choice-1))]}
116+
user=${arruser[$(($choice-1))]}
117+
label=${arrencoding[$(($choice-1))]}
118+
port=${arrport[$(($choice-1))]}
119+
sshkey=${arrsshkey[$(($choice-1))]}
120+
fi
121+
122+
# Bad cases
123+
if [ -z $targetfullname ] || [ -z $user ];then
124+
echo "No matching server~";
125+
exit 1;
126+
fi
127+
target=$targetfullname
128+
129+
# Any key value should not be empty
130+
if [ -z $port ]; then
131+
port=22
132+
fi
133+
134+
if [ -z $passwd ]; then
135+
passwd=-
136+
fi
137+
138+
if [ -z $extra_options ]; then
139+
extra_options=-
140+
fi
141+
142+
if [ -z $sshkey ]; then
143+
sshkey=-
144+
fi
145+
146+
echo "Logging into ${user}@${target} ${label}..."
147+
148+
ssh-expect $user $target $passwd $port $extra_options $sshkey

ssh-expect

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/expect
2+
set USER [lindex $argv 0]
3+
set TARGET [lindex $argv 1]
4+
set PASSWD [lindex $argv 2]
5+
set PORT [lindex $argv 3]
6+
set OPTS [lindex $argv 4]
7+
set SSHKEY [lindex $argv 5]
8+
9+
trap {
10+
set rows [stty rows]
11+
set cols [stty columns]
12+
stty rows $rows columns $cols < $spawn_out(slave,name)
13+
} WINCH
14+
15+
if { $OPTS == "-" } {
16+
set OPTS ""
17+
}
18+
19+
if {$SSHKEY != "-"} {
20+
spawn ssh $USER@$TARGET -p $PORT -i $SSHKEY $OPTS
21+
expect {
22+
"yes/no" { send "yes\r"; exp_continue }
23+
}
24+
} else {
25+
spawn ssh $USER@$TARGET -p $PORT $OPTS
26+
expect {
27+
"yes/no" { send "yes\r"; exp_continue }
28+
"password:" { send "$PASSWD\r" }
29+
}
30+
}
31+
32+
interact {
33+
timeout 60 { send " "}
34+
}

0 commit comments

Comments
 (0)