# gosh **Repository Path**: jf_linux/gosh ## Basic Information - **Project Name**: gosh - **Description**: 基于ssh协议的运维工具,支持批量远程执行命令,下发文件、启动代理 - **Primary Language**: Go - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 11 - **Created**: 2023-05-28 - **Last Updated**: 2023-05-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 0. 介绍 Gosh 是由golang编写的集成运维常用功能的小工具,基于ssh协议完成远程主机批量执行命令、下发文件、ssh端口转发功能。 ``` [root@duduniao ~]# gosh --help 基于ssh协议的批处理工具 Usage: gosh [command] Available Commands: cmd 连接远程服务器并执行shell命令 completion Generate the autocompletion script for the specified shell help Help about any command login login remote host then open terminal proxy 将远程服务TCP端口代理到本地 push 发送文件到远程主机 Flags: -h, --help help for gosh -v, --version version for gosh Use "gosh [command] --help" for more information about a command. ``` # 1. 配置命令补全功能 ``` [root@duduniao ~]# gosh completion Generate the autocompletion script for gosh for the specified shell. See each sub-command's help for details on how to use the generated script. Usage: gosh completion [command] Available Commands: bash Generate the autocompletion script for bash fish Generate the autocompletion script for fish powershell Generate the autocompletion script for powershell zsh Generate the autocompletion script for zsh Flags: -h, --help help for completion ``` 以Linux的Bash为例: ``` # 将生成的补全脚本放入 /etc/bash_completion.d 下 [root@duduniao ~]# gosh completion bash > /etc/bash_completion.d/gosh # 退出当前shell后,重新打开终端,可以使用 tab 完成命令补全 [root@duduniao ~]# gosh cmd (连接远程服务器并执行shell命令) login (login remote host then open terminal) completion (Generate the autocompletion script for the specified shell) proxy (将远程服务TCP端口代理到本地) help (Help about any command) push (发送文件到远程主机) ``` # 2. 批量执行shell命令 Gosh 支持指定跳板机,但是目前只支持一个跳板机,暂时不支持多级跳板机操作 ``` [root@duduniao ~]# gosh cmd --help 连接远程服务器并执行shell命令 Usage: gosh cmd {-i hosts.txt|-H host01,host02,...} [flags] commands... Flags: --bastion.host string 指定跳板机IP --bastion.key string 指定跳板机ssh私钥 (default "~/.ssh/id_rsa") --bastion.key.passphrase string 指定跳板机ssh私钥的密码 --bastion.password string 指定跳板机密码 --bastion.port int 指定跳板机端口 (default 22) --bastion.user string 指定跳板机用户,默认当前用户 -t, --connect.timeout duration ssh连接超时时间 (default 5s) -f, --fork int 指定并发数量 (default 10) -h, --help help for cmd -H, --host string 指定主机列表,使用逗号分隔 -i, --inventory string 指定主机清单列表文件,# 开头为注释 -k, --key string 指定ssh私钥 (default "~/.ssh/id_rsa") --key.passphrase string ssh私钥的密码 -P, --password string 远程ssh用户的密码 -p, --port int 指定远程主机ssh的端口 (default 22) --print.field strings 选择打印的消息字段 (default [ip,stdout,stderr]) --print.status strings 选择打印成功或者失败的消息 (default [failed,succeed]) -u, --user string 远程ssh用户,默认当前用户 ``` ## 2.1. 案例一 绿色表示返回值为0,红色表示执行失败或者返回值为非0
![Image](https://gitee.com/linux_duduniao/gosh/raw/master/images/img-01.png) ## 2.2. 案例二 筛选出成功的主机IP和输出 ![Image](https://gitee.com/linux_duduniao/gosh/raw/master/images/img-07.jpg) ## 2.3. 案例三 筛选出失败的主机IP地址 ![Image](https://gitee.com/linux_duduniao/gosh/raw/master/images/img-08.jpg) # 3. 批量下发文件 Gosh支持基于sftp协议下发多个文件或者目录到远程主机,虽然支持下发目录,但是对于有特殊权限的目录,建议打包后下发到目标主机后修改权限
``` [root@duduniao ~]# gosh push --help 发送一个或者多个文件到远程主机 Usage: gosh push {-i hosts.txt|-H host01,host02,...} [flags] localFile... remoteDirectory Flags: --bastion.host string 指定跳板机IP --bastion.key string 指定跳板机ssh私钥 (default "~/.ssh/id_rsa") --bastion.key.passphrase string 指定跳板机ssh私钥的密码 --bastion.password string 指定跳板机密码 --bastion.port int 指定跳板机端口 (default 22) --bastion.user string 指定跳板机用户,默认当前用户 -t, --connect.timeout duration ssh连接超时时间 (default 5s) -f, --fork int 指定并发数量 (default 10) -h, --help help for push -H, --host string 指定主机列表,使用逗号分隔 -i, --inventory string 指定主机清单列表文件,# 开头为注释 -k, --key string 指定ssh私钥 (default "~/.ssh/id_rsa") --key.passphrase string ssh私钥的密码 -P, --password string 远程ssh用户的密码 -p, --port int 指定远程主机ssh的端口 (default 22) --print.field strings 选择打印的消息字段 (default [ip,stdout,stderr]) --print.status strings 选择打印成功或者失败的消息 (default [failed,succeed]) -u, --user string 远程ssh用户,默认当前用户 ``` ## 3.1. 案例一 ![Image](https://gitee.com/linux_duduniao/gosh/raw/master/images/img-02.png) ## 3.2. 案例二 ![Image](https://gitee.com/linux_duduniao/gosh/raw/master/images/img-03.png) # 4. 端口转发 ``` [root@duduniao ~]# gosh proxy --help 将远程服务TCP端口代理到本地,方便服务调测。使用ssh工具实现本地端口转发和远程端口转发 可参考文档:https://www.yuque.com/duduniao/linux/nwsn6p#Kt3rn Usage: gosh proxy -H host [flags] [-l local] -r remote Flags: -j, --bastion.host string 指定跳板机IP --bastion.key string 指定跳板机ssh私钥 (default "~/.ssh/id_rsa") --bastion.key.passphrase string 指定跳板机ssh私钥的密码 --bastion.password string 指定跳板机密码 --bastion.port int 指定跳板机SSH端口 (default 22) --bastion.user string 指定跳板机SSH用户,默认当前用户 -h, --help help for proxy -H, --host string 转发请求的远程主机 -k, --key string 指定ssh私钥 (default "~/.ssh/id_rsa") --key.passphrase string ssh私钥的密码 -l, --local string 本地监听端口 (default "127.0.0.1:10080") -P, --password string 远程ssh用户的密码 -p, --port int 指定远程主机ssh的端口 (default 22) --protocol string 远程服务器的通宵协议,tcp,tcp4,tcp6,unix (default "tcp4") -r, --remote string 目标服务地址 -t, --timeout duration 连接超时时间,ssh连接和proxy转发超时 (default 5s) --type string 指定端口转发类型,local:本地端口转发,remote:远程端口转发 (default "local") -u, --user string 远程ssh用户,默认当前用户 ``` ## 4.1. 本地端口转发 运维在日常服务部署和调测中,很多场景中因为网络隔离导致无法通过浏览器去访问目标服务的Web UI, 比如在腾讯云虚拟机部署的prometheus,但是这个机器没有公网IP,无法在公司PC去访问prometheus的web UI,会影响故障定位。 ### 4.1.1. 转发vmalert端口到本地 ![image.png](https://gitee.com/linux_duduniao/gosh/raw/master/images/img-04.png) ## 4.2. 远程端口转发 一种常见的场景是,运维在本地vmware搭建了一个测试环境,需要让其它同事去访问,由于存在网络隔离,无法直接访问,此时可以将测试环境的端口映射到公司内网的服务器上,就可以通过服务器的端口去直接访问。 ### 4.2.1. 转发consul端口到远端 ``` [root@hys-dev-251 ~]# grep -i gateway /etc/ssh/sshd_config # 允许sshd作为gateway GatewayPorts yes ``` ![Image](https://gitee.com/linux_duduniao/gosh/raw/master/images/img-05.png) ![Image](https://gitee.com/linux_duduniao/gosh/raw/master/images/img-06.png) # 5. 交互式终端 ```text [root@duduniao ~]# gosh login --help login remote host then open terminal Usage: gosh login -H host [flags] Flags: -j, --bastion.host string 指定跳板机IP --bastion.key string 指定跳板机ssh私钥 (default "~/.ssh/id_rsa") --bastion.key.passphrase string 指定跳板机ssh私钥的密码 --bastion.password string 指定跳板机密码 --bastion.port int 指定跳板机SSH端口 (default 22) --bastion.user string 指定跳板机SSH用户,默认当前用户 -h, --help help for login -H, --host string 转发请求的远程主机 -k, --key string 指定ssh私钥 (default "~/.ssh/id_rsa") --key.passphrase string ssh私钥的密码 -P, --password string 远程ssh用户的密码 -p, --port int 指定远程主机ssh的端口 (default 22) -t, --timeout duration 连接超时时间,ssh连接和proxy转发超时 (default 5s) -u, --user string 远程ssh用户,默认当前用户 ``` ## 5.1. 登陆远程主机 ![Image](https://gitee.com/linux_duduniao/gosh/raw/master/images/img-09.jpg)