Skip to content

Commit

Permalink
添加scan os指令
Browse files Browse the repository at this point in the history
  • Loading branch information
weiliang-ms committed Jan 1, 2022
1 parent bc71f68 commit d0fd87c
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 40 deletions.
16 changes: 0 additions & 16 deletions book/执行指令集/01跨主机并行执行shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ INFO[0000] 生成配置文件样例, 请携带 -c 参数重新执行 -> config.y

```yaml
server:
# - host: 10.79.165.[11:13]
# username: root
# password: neusoft
# port: 22
# - host: 10.79.164.[61:63]
# username: root
# password: neusoft
# port: 22
- host: 10.10.10.[1:3]
username: root
privateKeyPath: ~/.ssh/id_rsa
Expand Down Expand Up @@ -73,14 +65,6 @@ $ easyctl exec shell -c config.yaml --debug

```yaml
server:
# - host: 10.79.165.[11:13]
# username: root
# password: neusoft
# port: 22
# - host: 10.79.164.[61:63]
# username: root
# password: neusoft
# port: 22
- host: 10.10.10.[1:3]
username: root
privateKeyPath: ~/.ssh/id_rsa
Expand Down
127 changes: 127 additions & 0 deletions book/扫描指令集/01扫描系统信息.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
## 扫描系统信息

**版本支持:**

- [v0.7.18-alpha以上](https://github.com/weiliang-ms/easyctl/releases/)

**兼容性:**

- [x] `CentOS6`
- [x] `CentOS7`

**扫描内容:**

- 基本信息:
- `IP`地址
- 主机名
- 系统版本
- 内核版本
- `CPU`信息
- `CPU`线程数(逻辑核数)
- `CPU`主频
- `CPU`型号
- `CPU`平均负载
- 内存信息
- 总内存大小
- 已用内存大小
- 内存使用率
- 磁盘信息
- 系统盘名称
- 根分区使用率
- 使用率超过`90%`的分区

### 使用方式

> 参考以下链接进行安装
- [安装说明文档](../-安装文档/README.md)

> 生成默认配置文件
```shell
$ easyctl scan os
INFO[0000] 生成配置文件样例, 请携带 -c 参数重新执行 -> config.yaml
```

> 修改配置文件
`config.yaml`, 修改主机列表。`easyctl`根据主机列表`ssh`远程至目标主机进行扫描

```yaml
server:
- host: 10.10.10.[1:3]
username: root
privateKeyPath: "" # ~/.ssh/id_rsa,为空默认走password登录;不为空默认走密钥登录
password: 123456
port: 22
excludes:
- 192.168.235.132
```
> 执行
添加`--debug`可以输出详细内容。

```shell
$ easyctl scan os -c config.yaml --debug
```

### 配置项说明

- 主机配置段:该段配置远程执行`shell`的主机信息,字段说明如下
- `host: 10.10.10.[1:3]` 主机地址段,适用于`ip`连续场景。分隔符可以为`[1:3]`、`1-2`、`[1-2]`、`1:2`
- `username`: 远程主机`ssh`用户名称,缺省值为`root`
- `password`: 对应`username`的密码
- `privateKeyPath`: `ssh`私钥路径
- `port`: `ssh`端口,默认`22`
- `excludes`: 排除`host`地址段内的`ip`地址列表

`privateKeyPath`优先级高于`password`:

1. `privateKeyPath`为空,取`password`值,`ssh`使用密码登录方式
2. `privateKeyPath`非空,取`privateKeyPath`值,`ssh`使用密钥登录方式

```yaml
server:
- host: 10.10.10.[1:3]
username: root
privateKeyPath: ~/.ssh/id_rsa
password: ""
port: 22
excludes:
- 192.168.235.132
```

### 配置样例

> 1.扫描主机: `10.10.10.1-10.10.10.10`信息

```yaml
server:
- host: 10.10.10.[1:10]
username: root
privateKeyPath: ~/.ssh/id_rsa
password: ""
port: 22
excludes:
- 192.168.235.132
```

> 2.扫描主机: `10.10.10.1`、`10.10.10.3`、`10.10.10.4`信息,使用密码登录方式

```yaml
server:
- host: 10.10.10.[1:4]
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "123456"
port: 22
excludes:
- 10.10.10.2
```

### 扫描样例

执行完扫描指令后,会生成`system.xlsx`文件,内容大致如下:

![](images/scan-os-result.png)
Binary file added book/扫描指令集/images/scan-os-result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 0 additions & 8 deletions cmd/track/asset/executor.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
server:
# - host: 10.79.165.[11:13]
# username: root
# password: neusoft
# port: 22
# - host: 10.79.164.[61:63]
# username: root
# password: neusoft
# port: 22
- host: 10.79.166.[1:45]
username: root
privateKeyPath: D:\github\easyctl\asset\id_rsa
Expand Down
32 changes: 16 additions & 16 deletions pkg/scan/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
strings2 "github.com/weiliang-ms/easyctl/pkg/util/strings"
"io"
"os"

//
_ "embed"
"fmt"
Expand Down Expand Up @@ -75,26 +74,27 @@ func OS(item command.OperationItem) command.RunErr {
return command.RunErr{Err: err, Msg: "解析异常"}
}

serversOut, _ := format.Object(servers)
item.Logger.Debugf("列表信息:%s", &serversOut)

var result OSInfoSlice

ch := make(chan OSInfo, len(servers))
wg := sync.WaitGroup{}
wg.Add(len(servers))

for _, v := range servers {
go func(s runner.ServerInternal) {
wg.Add(1)
ch <- osInfo(s, item.Logger)
defer wg.Done()
}(v)
}

go func() {
for v := range ch {
result = append(result, v)
}
}()

wg.Wait()
close(ch)
for v := range ch {
result = append(result, v)
}

// 排序
sort.Sort(result)
Expand All @@ -104,9 +104,7 @@ func OS(item command.OperationItem) command.RunErr {
}
item.Logger.Infof("系统信息:\n%v", out.String())

SaveAsExcel(result)

return command.RunErr{}
return command.RunErr{Err: SaveAsExcel(result)}
}

// 获取操作系统信息
Expand All @@ -125,7 +123,9 @@ func osInfo(s runner.ServerInternal, logger *logrus.Logger) OSInfo {
}); re.Err != nil {
panic(re.Err)
} else {
baseInfo.Hostname = strings.TrimSuffix(re.StdOut, "\n")
hostname := strings.TrimSuffix(re.StdOut, "\n")
logger.Debugf("[%s] 主机名为:%s", s.Host, hostname)
baseInfo.Hostname = hostname
}

if re := s.ReturnRunResult(runner.RunItem{
Expand Down Expand Up @@ -231,7 +231,9 @@ func NewCPUInfoItem(content string) CPUInfo {
reg := regexp.MustCompile("^model name")
if reg.MatchString(v) && c.CPUModeNum == "" && c.CPUClockSpeed == "" {
c.CPUModeNum = strings.TrimSpace(strings.Split(strings.Split(v, ":")[1], "@")[0])
c.CPUClockSpeed = strings.TrimSpace(strings.Split(strings.Split(v, ":")[1], "@")[1])
if re := strings.Split(strings.Split(v, ":")[1], "@"); len(re) > 1 {
c.CPUClockSpeed = strings.TrimSpace(re[1])
}
}
}

Expand Down Expand Up @@ -365,9 +367,7 @@ func SaveAsExcel(data []OSInfo) error {
}

for k, v := range maps {
f.SetSheetRow(sheet, k, &[]interface{}{
v,
})
f.SetSheetRow(sheet, k, &[]interface{}{v})
}

return f.Save()
Expand Down

0 comments on commit d0fd87c

Please sign in to comment.