Skip to content

Commit

Permalink
version 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
freedomkk-qfeng committed Jan 19, 2018
1 parent c1c790e commit 87e2079
Show file tree
Hide file tree
Showing 13 changed files with 636 additions and 314 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.txt
*.json
*.exe
*.exe
*.key
298 changes: 222 additions & 76 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

一个简单的并行 SSH 工具,可以批量的对主机通过 SSH 执行命令组合。

支持:
- 并发执行
- 单次执行多条命令
- ip 地址段自动匹配主机(192.168.0.1-192.168.0.100)
- ssh 用户名/密码认证
- ssh key 认证
- json 格式输出
- 输出到文本,文件名为 host.txt

#### 编译
```
go get ./...
Expand All @@ -17,64 +26,73 @@ https://github.com/shanghai-edu/multissh/releases/

#### 命令体系
```
./multissh -h
-cmd string
cmds // 需要执行的命令组合,多条命令以 ; 分割
# ./multissh -h
Usage of ./multissh:
-c string
cfg File Path
-ciphers string
ciphers
-cmdfile string
cmdfile path //需要执行的命令组合文件,文件内命令按行分割
cmdfile path
-cmds string
cmds
-hostfile string
hostfile path // 需要执行的主机列表文件,主机列表在文件内按行分割
hostfile path
-hosts string
host address list //需要执行的主机列表,多个主机以 ; 分割
host address list
-ipfile string
hostfile path //需要执行的主机(IP)列表文件,IP可以以地址段的方式逐行写在文本内
hostfile path
-j print output in json format
-k string
ssh private key
-l In linux mode,multi command combine with && ,such as date&&cd /opt&&ls (default true)
-n int
max execute number (default 20)
-outTxt
write result into txt
-p string
password // 主机的 SSH 密码
password
-port int
ssh port (default 22) //主机的 SSH 端口,默认 22
-u string
username //主机的 SSH 用户名
-j string
jsonFile //保存大量主机,包括主机地址,SSH用户名,SSH密码,SSH端口,所需执行的cmd指令文件地址
-outTxt bool
outTxt (default false) //是否允许把结果保存到文件中,文件名为 ssh 连接的主机名(host 或 ip),true为允许 false为默认值
ssh port (default 22)
-t int
timeLimit (default 30) //单个 ssh 会话的最大时间,超过时间命令未执行完则超时 默认为30s
-n int
numLimit (default 20) //最大并发访问量 默认为20
max timeout (default 30)
-u string
username
-v show version
```
**cmdfile 示例**
```
show clock
```
**hostfile 示例**
```
192.168.15.101
192.168.31.21
192.168.15.102
```
**ipfile 示例**
```
192.168.15.101-192.168.15.102
192.168.15.101-192.168.15.103
192.168.31.21-192.168.31.22
```

**ssh.json 示例**
```
{
"SshHosts": [
{
"Host": "192.168.15.101",
"Port": 22,
"Username": "admin",
"Password": "admin",
"CmdFile": "cmd1.txt.example"
},
"Host": "192.168.31.51",
"Port": 22,
"Username": "admin",
"Password": "admin",
"cmds":"show clock;show clock"
},
{
"Host": "192.168.83.40",
"Port": 22,
"Username": "root",
"Password": "root",
"Host": "192.168.80.131",
"Port": 22,
"Username": "root",
"Password": "",
"key": "./server.key",
"linuxMode": true,
"CmdFile": "cmd2.txt.example"
}
]
Expand All @@ -84,78 +102,206 @@ show clock
## 用法
#### cmd string & host string
```
./multissh -cmd "show clock" -hosts "192.168.15.101;192.168.15.102" -u admin -p admin
# ./multissh -cmds "show clock" -hosts "192.168.31.21;192.168.15.102" -u admin -p password
2018/01/17 14:01:28 Multissh start
2018/01/17 14:01:31 Multissh finished. Process time 2.867808673s. Number of active ip is 2
host: 192.168.31.21
========= Result =========
192.168.15.101 ssh start
sw-1#show clock
05:26:40.649 UTC Tue Jun 6 2017
sw-1#exit
******************************************************************************
* Copyright (c) 2004-2016 Hangzhou H3C Tech. Co., Ltd. All rights reserved. *
* Without the owner's prior written consent, *
* no decompiling or reverse-engineering shall be allowed. *
******************************************************************************
192.168.15.101 ssh end
<sw-h3c>show clock
14:01:31 CN Wed 01/17/2018
Time Zone : CN add 08:00:00
<sw-h3c>exit
192.168.15.102 ssh start
sw-2#show clock
05:24:38.708 UTC Tue Jun 6 2017
sw-2#exit
host: 192.168.15.102
========= Result =========
sw-cisco#show clock
05:50:24.935 UTC Wed Jan 17 2018
sw-cisco#exit
192.168.15.102 ssh end
```

#### cmdfile & hostfile
```
./multissh -cmdfile cmd.txt -hostfile host.txt -u admin -p admin
# ./multissh -cmdfile cmd1.txt.example -hostfile host.txt.example -u admin -p password
2018/01/17 14:01:28 Multissh start
2018/01/17 14:01:31 Multissh finished. Process time 2.867808673s. Number of active ip is 2
host: 192.168.31.21
========= Result =========
******************************************************************************
* Copyright (c) 2004-2016 Hangzhou H3C Tech. Co., Ltd. All rights reserved. *
* Without the owner's prior written consent, *
* no decompiling or reverse-engineering shall be allowed. *
******************************************************************************
192.168.15.101 ssh start
sw-1#show clock
05:29:43.269 UTC Tue Jun 6 2017
sw-1#exit
<sw-h3c>show clock
14:01:31 CN Wed 01/17/2018
Time Zone : CN add 08:00:00
<sw-h3c>exit
192.168.15.101 ssh end
host: 192.168.15.102
========= Result =========
192.168.15.102 ssh start
sw-2#show clock
05:27:41.332 UTC Tue Jun 6 2017
sw-2#exit
sw-cisco#show clock
05:50:24.935 UTC Wed Jan 17 2018
sw-cisco#exit
192.168.15.102 ssh end
```

#### ipfile
```
./multissh -cmdfile cmd.txt -ipfile ip.txt -u admin -p admin
# ./multissh -cmdfile cmd1.txt.example -ipfile ip.txt.example -u admin -p password
2018/01/17 14:25:26 Multissh start
2018/01/17 14:25:29 Multissh finished. Process time 2.847347642s. Number of active ip is 5
host: 192.168.15.101
========= Result =========
sw-cisco-1#show clock
06:17:49.422 UTC Wed Jan 17 2018
sw-cisco-1#exit
host: 192.168.15.102
========= Result =========
sw-cisco-2#show clock
06:14:22.445 UTC Wed Jan 17 2018
sw-cisco-2#exit
host: 192.168.15.103
========= Result =========
sw-cisco-3#show clock
06:19:14.487 UTC Wed Jan 17 2018
sw-cisco-3#exit
host: 192.168.31.21
========= Result =========
******************************************************************************
* Copyright (c) 2004-2016 Hangzhou H3C Tech. Co., Ltd. All rights reserved. *
* Without the owner's prior written consent, *
* no decompiling or reverse-engineering shall be allowed. *
******************************************************************************
<sw-h3c>show clock
14:25:29 CN Wed 01/17/2018
Time Zone : CN add 08:00:00
<sw-h3c>exit
host: 192.168.31.22
========= Result =========
sw-cisco-4#show clock
14:25:27.639 beijing Wed Jan 17 2018
sw-cisco-4#exit
```
#### ssh key-based Auth and linuxMode
```
# ./multissh -hosts "192.168.80.131" -cmds "date;cd /opt;ls" -u root -k "server.key"
2018/01/17 14:33:55 Multissh start
2018/01/17 14:33:56 Multissh finished. Process time 960.367764ms. Number of active ip is 1
host: 192.168.80.131
========= Result =========
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-98-generic x86_64)
192.168.15.101 ssh start
sw-1#show clock
05:29:43.269 UTC Tue Jun 6 2017
sw-1#exit
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
192.168.15.101 ssh end
System information as of Wed Jan 17 14:33:55 CST 2018
192.168.15.102 ssh start
sw-2#show clock
05:27:41.332 UTC Tue Jun 6 2017
sw-2#exit
System load: 0.0 Processes: 335
Usage of /: 10.0% of 90.18GB Users logged in: 0
Memory usage: 2% IP address for eth0: 192.168.80.131
Swap usage: 0% IP address for docker0: 172.17.0.1
Graph this data and manage this system at:
https://landscape.canonical.com/
0 个可升级软件包。
0 个安全更新。
New release '17.10' available.
Run 'do-release-upgrade' to upgrade to it.
You have new mail.
Last login: Wed Jan 17 14:29:39 2018 from 202.120.80.201
root@ubuntu-docker-node3:~# 201817:33:56 CST
root@ubuntu-docker-node3:~# root@ubuntu-docker-node3:/opt# cisco
composer.json
composer.phar
example-oauth2-server
getting-started-with-mmdb
gitlab
gitlab-ce_8.0.4-ce.1_amd64.deb
oauth2-demo-php
oauth2-server-php
python_test
rsyslog-maxminddb
root@ubuntu-docker-node3:/opt# 注销
# ./multissh -hosts "192.168.80.131" -cmds "date;cd /opt;ls" -u root -k "server.key" -l
2018/01/17 14:34:02 Multissh start
2018/01/17 14:34:02 Multissh finished. Process time 842.465643ms. Number of active ip is 1
host: 192.168.80.131
========= Result =========
201817:34:02 CST
cisco
composer.json
composer.phar
example-oauth2-server
getting-started-with-mmdb
gitlab
gitlab-ce_8.0.4-ce.1_amd64.deb
oauth2-demo-php
oauth2-server-php
python_test
rsyslog-maxminddb
192.168.15.102 ssh end
```

#### ssh.json
```
./multissh -j ssh.json -t 30 -n 20 -outTxt
192.168.15.101 ssh start
sw-1#show clock
05:29:43.269 UTC Tue Jun 6 2017
sw-1#exit
./multissh -c ssh.json.example
2018/01/17 14:29:38 Multissh start
2018/01/17 14:29:41 Multissh finished. Process time 2.922928532s. Number of active ip is 2
host: 192.168.31.51
========= Result =========
******************************************************************************
* Copyright (c) 2004-2016 Hangzhou H3C Tech. Co., Ltd. All rights reserved. *
* Without the owner's prior written consent, *
* no decompiling or reverse-engineering shall be allowed. *
******************************************************************************
192.168.15.101 ssh end
<sw-h3c>show clock
14:29:41 CN Wed 01/17/2018
Time Zone : CN add 08:00:00
<WenKe-5F-Stack-2>show clock
14:29:41 CN Wed 01/17/2018
Time Zone : CN add 08:00:00
<WenKe-5F-Stack-2>exit
192.168.83.40 ssh start
2017年 06月 09日 星期五 09:33:11 CST
2017年 06月 09日 星期五 09:33:14 CST
192.168.83.40 ssh end
host: 192.168.80.131
========= Result =========
cisco
composer.json
composer.phar
example-oauth2-server
getting-started-with-mmdb
gitlab
gitlab-ce_8.0.4-ce.1_amd64.deb
oauth2-demo-php
oauth2-server-php
python_test
rsyslog-maxminddb
```
#### TODO
增加使用证书认证的支持

#### LICENSE
Apache License 2.0
6 changes: 3 additions & 3 deletions cmd2.txt.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
date
sleep 3
date
cd /opt
sleep 2
ls
Loading

0 comments on commit 87e2079

Please sign in to comment.