Date | Version | Owner | Comments |
---|---|---|---|
2018.10.12 | v0.9.0 | bian | Created it. |
2018.10.15 | v1.0.0 | bian | Release. |
2018.10.18 | v1.0.1 | bian | Get users from config for server. |
2018.10.18 | v1.0.2 | bian | Fix bug. |
使用go语言实现的ssh命令执行器。
- 提供了两种调用方式。
- 一次性启动并执行。
- 启动为http服务。
- 实现了两个模块,包括shell和copy。
- shell: 直接执行shell命令
- copy: 远程copy文件
usage: ssh-executor[.exe] [<flags>]
ssh-executor
Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
--version Show application version.
--log.level="info" Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]
--log.format="logger:stderr"
Set the log target and format. Example: "logger:syslog?appname=bob&local=7" or "logger:stdout?json=true"
-c, --config.file="" application's configuration file path.
-T, --launch.type="DIRECT" server/direct;default direct. server will setup a http server. direct will execute command once.
-t, --ssh.timeout=30 timeout in ssh connection. default 30s.
-a, --web.listen_address="" [launch.type=server] Address to listen on for UI, API.
--telemetry.listen_address=""
[launch.type=server] Address to listen on for telemetry.
-i, --hosts="" [launch.type=direct] Hosts to connect by ssh. Combined by ','. Add hosts.file.
-f, --hosts.file="" [launch.type=direct] File of hosts to connect by ssh. One ip on line. Add hosts.
-u, --user.name="" [launch.type=direct] Username for ssh connection.
-p, --user.pass="" [launch.type=direct] Password for ssh connection.
-m, --module="SHELL" [launch.type=direct] Module to handle. like 'shell' 'copy'
-C, --command="" [launch.type=direct] Command to handle.
-
linux
chmod +x ssh-executor ./ssh-executor -i 10.99.70.35,10.99.70.38 -u user -p pass -C "/bin/sh --login -c 'ifconfig'"
-
windows
ssh-executor.exe command
./ssh-executor -i 10.99.70.35,10.99.70.38 -f host.txt -u user -p pass -C "/bin/sh --login -c 'ifconfig'" -t 10
./ssh-executor -T server -a localhost:9888
./ssh-executor -c config.yml
完整配置文件参考 config_example.yml
./ssh-executor -i 10.99.70.35,10.99.70.38 -u user -p pass -C "/bin/sh --login -c 'ifconfig'"
请求语法
POST /job HTTP/1.1
Content-Type: application/json
{
"user_flag": "test", //用户名和密码的标示。程序打包或者启动时带入。建议使用此项。
"hosts": [
"10.99.70.38",
"10.99.70.35"
],
"ssh_config": {
"timeout": 30,
"sh": "/bin/sh",
"username": "user", //调用时传入,否则使用user_flag指代对象
"password": "pass"
},
"tasks": [
{
"name": "ddd",
"module": "shell",
"args": {
"shell": {
"command": "ls", // 最终执行命令: sudo -H -S -n -u admin --login -c 'ls'
"chdir": "/tmp",
"login": true,
"become": true,
"become_user": "admin",
"become_method": "sudo"
}
}
},
{
"name": "dfafa",
"module": "copy",
"args": {
"copy": {
"ignore_err": true,
"become": true,
"become_user": "admin",
"become_method": "sudo",
"sftp_mode": "pull",
"copy_files": [
{
"src": "/tmp/foo.yml",
"dest": "foo.yml",
"owner": "foo",
"group": "foo",
"mode": 644,
"md5": "12313131",
"force": true,
"create_directory": true,
"recursive": true,
"directory_mode": 755
}
]
}
}
}
]
}
返回体
Content-Type: application/json
示例,与请求不对应,包括任务的成功和失败情况
{
"success": false,
"msg": "",
"detail": [
{
"host": "10.99.70.38",
"success": false,
"tasks": [
{
"name": "shell error task",
"module": "shell",
"success": false,
"result": {
"stdout": "ls: cannot open directory /home/abcedfg: Permission denied\r\n",
"exitcode": 1
},
"err": "Process exited with status 1"
},
{
"name": "shell success task",
"module": "shell",
"success": true,
"result": {
"stdout": "admin\r\n",
"exitcode": 0
},
"err": ""
},
{
"name": "copy task",
"module": "copy",
"sftpmode": "push",
"success": false,
"copyfiles": [
{
"src": "tmp/config.yml",
"dest": "/tmp/bian/config.web.yml",
"result": {
"changed": true
},
"err": ""
},
{
"src": "tmp/config.notexist.yml",
"dest": "/tmp/bian/config.web.yml",
"result": {
"changed": false
},
"err": "open tmp/config.notexist.yml: The system cannot find the file specified."
}
]
}
]
}
]
}
- direct:
- 命令行输入用户名、密码优先级最高
- 其次为配置文件中ssh_config中配置的username和password
- server:
- 用户输入参数中user_flag表示的用户名和密码,优先级最高
- 其次为用户输入参数中ssh_config中配置的username和password
- 再其次同上,和direct相同的配置
来自启动时的配置文件和程序打包时的文件web/user.go。配置文件的内容会添加或覆盖程序中打包的内容。
- 配置文件中变量为server.users,如下,
server:
users:
- type: abc
username: a
password: a
- type: test
username: "test"
password: "test"
- web/user.go文件可以从web/user.go.example复制修改。
package web
func initUser() {
UserMap = make(map[string]users)
UserMap["1"] = users{"1", "1"}
UserMap["admin"] = users{"admin", "admin"}
UserMap["test"] = users{"test", "test"}
}
需要将web/user.go.example复制到user.go,并且将需要的user_flag对应的用户名密码填入。 目前支持的user_flag包括: release/compare/software/loggrep/vpnmanage/dnsmanage
- 首次提交
- 支持直接调用执行shell命令
- 支持直接调用,读取文件的任务执行功能模块
- 支持作为http服务启动,调用执行功能模块
- 功能模块
- shell: 直接执行shell命令
- copy: 远程copy文件
- 作为server启动时,支持配置文件中配置用户名密码,根据用户类型调用。
- Fix bug.