diff --git "a/book/\346\211\247\350\241\214\346\214\207\344\273\244\351\233\206/01\350\267\250\344\270\273\346\234\272\345\271\266\350\241\214\346\211\247\350\241\214shell.md" "b/book/\346\211\247\350\241\214\346\214\207\344\273\244\351\233\206/01\350\267\250\344\270\273\346\234\272\345\271\266\350\241\214\346\211\247\350\241\214shell.md" index c8971711..caae229f 100644 --- "a/book/\346\211\247\350\241\214\346\214\207\344\273\244\351\233\206/01\350\267\250\344\270\273\346\234\272\345\271\266\350\241\214\346\211\247\350\241\214shell.md" +++ "b/book/\346\211\247\350\241\214\346\214\207\344\273\244\351\233\206/01\350\267\250\344\270\273\346\234\272\345\271\266\350\241\214\346\211\247\350\241\214shell.md" @@ -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 @@ -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 diff --git "a/book/\346\211\253\346\217\217\346\214\207\344\273\244\351\233\206/01\346\211\253\346\217\217\347\263\273\347\273\237\344\277\241\346\201\257.md" "b/book/\346\211\253\346\217\217\346\214\207\344\273\244\351\233\206/01\346\211\253\346\217\217\347\263\273\347\273\237\344\277\241\346\201\257.md" new file mode 100644 index 00000000..45f239b9 --- /dev/null +++ "b/book/\346\211\253\346\217\217\346\214\207\344\273\244\351\233\206/01\346\211\253\346\217\217\347\263\273\347\273\237\344\277\241\346\201\257.md" @@ -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) \ No newline at end of file diff --git "a/book/\346\211\253\346\217\217\346\214\207\344\273\244\351\233\206/images/scan-os-result.png" "b/book/\346\211\253\346\217\217\346\214\207\344\273\244\351\233\206/images/scan-os-result.png" new file mode 100644 index 00000000..a254c8bf Binary files /dev/null and "b/book/\346\211\253\346\217\217\346\214\207\344\273\244\351\233\206/images/scan-os-result.png" differ diff --git a/cmd/track/asset/executor.yaml b/cmd/track/asset/executor.yaml index c3686a97..05715980 100644 --- a/cmd/track/asset/executor.yaml +++ b/cmd/track/asset/executor.yaml @@ -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 diff --git a/pkg/scan/os.go b/pkg/scan/os.go index d3828c67..77ed5c4a 100644 --- a/pkg/scan/os.go +++ b/pkg/scan/os.go @@ -5,7 +5,6 @@ import ( strings2 "github.com/weiliang-ms/easyctl/pkg/util/strings" "io" "os" - // _ "embed" "fmt" @@ -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) @@ -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)} } // 获取操作系统信息 @@ -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{ @@ -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]) + } } } @@ -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()