Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1baf6cf

Browse files
author
andesli
committed
project init
0 parents  commit 1baf6cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+19761
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#gitignore file#
2+
log/*
3+
ip.txt
4+
5+
#other rules
6+
#!bin/gossh
7+
#!bin/passtool

README.md

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
# gossh
2+
3+
## 1.gossh是什么
4+
5+
gossh是一个极简的ssh工具,使用go语言开发,被编译一个二进制程序,无任何依赖,真正开箱即用。用于远程管理linux(类unix)机器:包括远程执行命令和推拉文件,支持单机和批量并行执行模式。
6+
7+
## 2.gossh能干什么
8+
9+
gossh提供3种核心功能:
10+
11+
1. 连接到远程主机执行命令。
12+
2. 推送本地文件或者文件夹到远程主机。
13+
3. 拉取远程主机的文件到本地。
14+
15+
![功能](https://github.com/andesli/gossh/raw/master/docs/images/gossh_function.png)
16+
17+
## 3.gossh运行模式
18+
19+
gossh支持单机模式和批量并行模式,也就是可以一次向一台机器发送命令执行,也可以一次向成千上万台台机器批量发送命令。批量并行模式也是gossh最大的一个特点,充分利用go在并发执行方面的优势。
20+
21+
1. 单机模式。
22+
单机模式支持上文说的三种功能:远程执行命令,推送文件或者目录,拉取文件。
23+
24+
2. 批量模式。
25+
26+
可以通过-i 参数指定ip文件,通过-c 指定并发度。
27+
批量并行模式同样支持上文说的三种功能:远程执行命令,推送文件或者目录,拉取文件。
28+
29+
### 并行和串行执行
30+
31+
1. 批量模式默认通过-c控制并发度,如果-c 设置为1默认是串行执行模式, -c 的值大于1是并行执行模式。
32+
2. 并行执行模式下某台机器连不上或者执行失败不会自动退出,串行模式也一样,但是串行模式通过-s 参数可以使gossh执行过程中出错立即退出。
33+
34+
并行模式下没有提供出错退出的原因是,并行执行下,很难立即停止整个任务的执行,串行模式比较容易控制,在日常使用中,可以先使用串行模式验证功能,然后开启并行模式提升效率。
35+
36+
37+
## 4.gossh用法
38+
39+
### 4.1程序获取
40+
41+
1.源码编译。
42+
43+
```
44+
#需要有go编译环境
45+
cd $GOPATH/src && git clone https://github.com/andesli/gossh.git
46+
cd gossh
47+
48+
//gossh工具
49+
go build gossh.go
50+
51+
//密码加解密工具
52+
go build passtool.go
53+
54+
//编译脚本编译amd64 386体系结构下windows和linux版本,放到./bin目录下,如果有其他体系结构需要使用也可以修改脚本执行编译。
55+
./build.sh
56+
57+
```
58+
59+
2.如果不想从源码编译,编译好的二进制程序放bin/目录下。
60+
61+
得益于go语言优秀的跨平台特性,在./bin下已经为大家生成了amd64和386体系结构下windows和linux共计4个版本程序。
62+
63+
```
64+
bin
65+
|-- 386
66+
| |-- linux
67+
| | |-- gossh
68+
| | `-- passtool
69+
| `-- windows
70+
| |-- gossh.exe
71+
| `-- passtool.exe
72+
`-- amd64
73+
|-- linux
74+
| |-- gossh
75+
| `-- passtool
76+
`-- windows
77+
|-- gossh.exe
78+
`-- passtool.exe
79+
```
80+
81+
[点击立即下载](https://github.com/andesli/gossh/blob/master/bin)
82+
83+
84+
### 4.2参数说明
85+
86+
- gossh
87+
88+
```
89+
#gossh -h
90+
flag needs an argument: -h
91+
Usage of gossh:
92+
93+
-t string
94+
running mode: cmd|push|pull (default "cmd")
95+
运行模式:cmd 远程执行命令,默认值;push 推送文件到远程; pull拉取远程文件到本地。
96+
97+
-h string
98+
ssh ip
99+
100+
-P string
101+
ssh port (default "22")
102+
ssh端口
103+
104+
-u string
105+
ssh user (default "root")
106+
ssh用户名
107+
108+
-p string
109+
ssh password
110+
密码
111+
112+
113+
-i string
114+
ip file when batch running mode (default "ip.txt")
115+
批量执行是指定ip文件,有关文件格式见下文。
116+
117+
-c int
118+
the number of concurrency when b (default 30)
119+
批量并发执行的并发度,默认值是30,如果指定为1,gossh是串行执行。
120+
121+
-s if -s is setting, gossh will exit when error occurs
122+
-s是个bool型,只有到-c被指定为1时才有效,用来控制串行执行报错后是否立即退出。
123+
124+
-e password is Encrypted
125+
如果密码传递的是密文,使用-e标记。-e适用于通过-p传递的密码和-i 指定的文件中存放的密码字段。
126+
127+
-key string
128+
aes key for password decrypt and encryption
129+
密码加解密使用的key,gossh有一个默认的加密key,可以通过-key=xxx指定一个加解密的key. passtool密码加解密工具同样支持该-key选项.
130+
131+
-f force to run even if it is not safe
132+
如果遇到危险命令gossh默认是不执行,危险命令目前收录的有("rm", "mkfs", "mkfs.ext3", "make.ext2", "make.ext4", "make2fs", "shutdown", "reboot", "init", "dd"),可以通过-f强制执行,-f 是bool型参数,不指定默认为false。
133+
134+
-s if -s is setting, gossh will exit when error occurs
135+
如果-c=1,即并发度为1串行执行时,默认出错后会退出,使用-s标记不要退出,继续执行,在-c>1时,无论是否指定-s都不会出错退出。
136+
137+
-l string
138+
log level (debug|info|warn|error (default "info")
139+
日志级别
140+
141+
-logpath string
142+
logfile path (default "./log/")
143+
日志存放目录,默认是./log/
144+
145+
```
146+
- passtool密码工具
147+
148+
```
149+
./passtool -h
150+
Usage of ./passtool:
151+
-d 指定密码密文生成明文
152+
-e 指定密码明文生成密文
153+
-key string
154+
AES加密密钥
155+
```
156+
157+
158+
### 4.3 批量运行时IP文件格式
159+
160+
-i ipfile 指定批量操作的ip文件,ipfile文件每行有4个字段ip|port|user|password,字段之间使用|分隔,四个字段分别是:机器IP,ssh端口,ssh用户名,ssh密码。其中ip字段是必须的,其他三个字段是选填的。
161+
下面的配置都是合法的。
162+
163+
```
164+
ip|port|user|password
165+
ip|port|user|
166+
ip|port|user
167+
ip|port|
168+
ip|port
169+
ip|
170+
ip
171+
```
172+
如果没有提供可选字段,gossh 默认通过-u -p -P参数从命令行参数获取,如果没有指定命令行参数,默认取命令行参数的默认值。
173+
gossh 当前参数的默认值:
174+
175+
```
176+
-u 默认值是root
177+
-P 默认值是22
178+
-p 默认值是空
179+
-t 默认值是cmd
180+
181+
```
182+
**说明**
183+
184+
- 密码字段如果是空,gossh默认从db插件中查找相关流程参考第5章。
185+
- 如果密码字段加密了,需要指定-e标记。-e是个整体开关:密码文件中的密码要么全部加密,要么不加密。
186+
187+
### 4.4 详细示例
188+
189+
点击[示例](https://github.com/andesli/gossh/blob/master/docs/example.md)查看详情。
190+
191+
### 4.5 输出和日志
192+
193+
点击[输出和日志](https://github.com/andesli/gossh/blob/master/docs/output_format.md)查看详情。
194+
195+
196+
## 5.密码管理
197+
198+
点击[密码管理](https://github.com/andesli/gossh/blob/master/docs/password.md)查看详情。
199+
200+
## 6.安全性
201+
202+
gossh从多种角度保证执行安全,包括密码的加密存放、命令黑名单、以及文件传递过程中的检查、日志记录等,详情
203+
点击[gossh安全管理](https://github.com/andesli/gossh/blob/master/docs/safe.md)查看详情。
204+
205+
## 7.不是重复造轮子
206+
207+
gossh不是重复制造一个像ansible的轮子,gossh的核心目标是提供给运维人员一个极度简洁的ssh工具,方便运维人员远程批量并行的初始化和管理机器。
208+
209+
有很多同学说ansible已经够好的了,为什么还要搞gossh?这是一个误区,请问ansible怎么批量安装到机器上?python环境怎么批量安装?这里有一个“先有鸡还是先有蛋”的问题,gossh就是第一个会下蛋的鸡。gossh使用go语言开发,静态编译为二进制程序,只要你的机器有ssh环境,并且能密码可以登录,理论上都能使用gossh进行管理。
210+
211+
gossh核心目标就是解决机器交付后“最初一公里-机器初始化的工作”。此时机器除了ssh,可能没有任何其他运行环境,此时通过gossh能够方便的快速的初始化机器。比如安装python,mysql等。
212+
213+
当然gossh也提供了扩展,可以方便进行二次开发,将其改造为远程执行引擎,集成到公司的自动化系统中。
214+
215+
## 8.gossh适用场景。
216+
217+
1. 大规模机器的首次初始化。
218+
公司来了几百台机器,只有ssh环境,除了初始用户名和密码,没有其他的安装。此时使用gossh对机器进行初始化,建立基本的环境。(gossh当初写的时候就是为了解决腾讯支付DB几千台机器的环境初始化)。
219+
220+
2. 命令行式批量远程管理。
221+
不是每个公司都是BAT,建立起自动化的运维管理系统。占绝大多数的中小企业的运维人员是通过脚本在远程管理机器,他们迫切需要一个拿来就用,不需要任何依赖的ssh工具。gossh就是为这种人准备的,gossh不需要任何配置文件,没有任何依赖,真正做到拿来即用。
222+
223+
3. 将gossh二次开发,改造为一个远程执行引擎。目前gossh还没有实现,没有实现的原因有两点:
224+
225+
- 这和最初将gossh设计为极简的ssh工具背道而驰。
226+
- 没有实际场景需求,大部分公司使用ansible或者salt stack, gossh无意重复造轮子。如果你的公司是一家使用go开发所有基础设施的云计算公司,可以在此基础上开发出一个远程执行引擎。
227+
228+
**gossh目标**
229+
230+
1.第一阶段目标是提供一个极简、好用、无任何依赖、可并发执行的ssh命令行工具。
231+
232+
2.第二阶段目标是是实现一个高度可集成的远程执行引擎,对外提供API服务,完善点对点的文件传递功能,很大概率不会在现有的gossh程序上改造,避免gossh变的臃肿,而是提供一个gossh2或者gosshweb的工具,专门做这件事情,遵照go的哲学,少即是多,一次只做一件事情。
233+
234+
现阶段之关注第一阶段目标,第二阶段目标还在酝酿中。
235+
236+
## 9. FAQ
237+
238+
[FAQ](https://github.com/andesli/gossh/blob/master/docs/faq.md)
239+
240+
任何问题请联系 [email protected]

auth/auth.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2018 gossh Author. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
// Author: andes
15+
16+
17+
package auth
18+
19+
import (
20+
"errors"
21+
"gossh/auth/driver"
22+
"sort"
23+
"sync"
24+
)
25+
26+
var (
27+
driversMu sync.RWMutex
28+
drivers = make(map[string]driver.GetPassworder)
29+
)
30+
31+
//Register Password Source Driver
32+
func Register(name string, d driver.GetPassworder) {
33+
driversMu.Lock()
34+
defer driversMu.Unlock()
35+
36+
if d == nil {
37+
panic("Register driver is nil")
38+
}
39+
if _, dup := drivers[name]; dup {
40+
panic("Register called twice for driver " + name)
41+
}
42+
drivers[name] = d
43+
}
44+
45+
//List Password Source Drivers
46+
func Drivers() []string {
47+
driversMu.RLock()
48+
defer driversMu.RUnlock()
49+
var list []string
50+
for name := range drivers {
51+
list = append(list, name)
52+
}
53+
sort.Strings(list)
54+
return list
55+
}
56+
57+
func unregisterAllDrivers() {
58+
driversMu.Lock()
59+
defer driversMu.Unlock()
60+
// For tests.
61+
drivers = make(map[string]driver.GetPassworder)
62+
}
63+
64+
// get password
65+
func GetPassword(driverName, ip, user string) (string, error) {
66+
driversMu.Lock()
67+
defer driversMu.Unlock()
68+
d, ok := drivers[driverName]
69+
70+
if !ok {
71+
return "", errors.New("unknown password driver: " + driverName)
72+
}
73+
return d.GetPassword(ip, user)
74+
}

0 commit comments

Comments
 (0)