assh
概述
一个透明的包装器,为SSH增加了对正则支持,别名,网关,动态主机名, graphviz , json输出, YAML配置等的支持。
lib-ssh将assh作为代理人;这意味着它与:
- SSH
- SCP
- rsync
- git
- 桌面应用程序取决于
lib-ssh或ssh(即,塔,atom.io,SSH隧道经理)
有关具体示例,请参见第三方集成
特征
配置功能
- 正则支持
-
别名
gate- >gate.domain.tld - 网关- >透明的SSH连接链
- 包括:在多个文件中的拆分配置
- 本地命令执行:最后是remoteCommand的逆转
- 模板:相当于主机,但您无法直接连接到模板,非常适合继承
- 继承:从主机或模板中制作主机继承
- 可变扩展:从环境中解析变量
-
智能proxyCommand :使用
netcat和socat作为默认后备时,RAW TCP连接 - 费率限制:配置每主机或全球利率限制
- JSON输出
- GraphViz :主机的GraphViz重复
使用命令行的网关
assh可以将ProxyCommand与Openssh透明的NetCat功能一起使用,也可以使用扩展配置的痛苦。
使用hostb作为网关连接到hosta 。
流程图
方向tb
是的[你]
一个[hosta]
b [hostb]
FW((防火墙))
样式FW填充:#F00,颜色:#FFF
y == x fw
fw .-> a
Y-> b
b-> a
$ ssh hosta/hostb
user@hosta $相当于ssh -o ProxyCommand="ssh hostb nc %h %p" hosta
使用hosta作为网关连接到hostb ,使用hostc作为网关。
流程图
方向tb
是的[你]
一个[hosta]
b [hostb]
C [hostc]
FW((防火墙))
样式FW填充:#F00,颜色:#FFF
y == x fw
FW ..-> a
Y-> c
C-> b
b-> a
$ ssh hosta/hostb/hostc
user@hosta $等效于ssh -o ProxyCommand="ssh -o ProxyCommand='ssh hostc nc %h %p' hostb nc %h %p" hosta
使用配置文件中的网关
您可以定义Openssh的“带有NetCat的ProxyCommand”功能,具有简单的语法,更高级的工作流和独特的后备功能。
让我们考虑以下assh .yml文件
hosts :
hosta :
Hostname : 1.2.3.4
hostb :
Hostname : 5.6.7.8
Gateways : hosta
hostc :
Hostname : 9.10.11.12
Gateways : hostb
hostd :
Hostname : 13.14.15.16
GatewayConnectTimeout : 2
Gateways :
- direct
- hosta-
ssh hosta>ssh 1.2.3.4 -
ssh hostbb->ssh -o ProxyCommand="ssh hostb nc %h %p" hosta -
ssh hostc>ssh -o ProxyCommand="ssh -o ProxyCommand='ssh hostc nc %h %p' hostb nc %h %p" hosta -
ssh hostd- >- assh将尝试进入
ssh 13.14.15.16 - 然后,在
ssh -o ProxyCommand="ssh hostd nc %h %p" hosta - 此方法使您可以在可能的情况下具有最佳的性能,但是如果您不在公司之外,请确保您的命令可以正常工作
- assh将尝试进入
引擎盖功能下
- 在需要时自动再生
~/.ssh/config文件 - 检查父进程确定日志级别(如果使用
ssh -vv, assh将在调试模式下自动运行) - 自动创建
ControlPath目录,因此您可以在ControlPath选项中使用SLASHES ,可以使用ControlMasterMkdir: true配置。
钩子
事件
beforeconnect
在assh试图连接到远程SSH端口之前, BeforeConnect被称为。
注意:每个SSH连接将调用BeforeConnect ;如果您使用多个网关,则将为每个网关调用,直到一个人成功连接。
Golang模板变量的示例:
// Host: http://god*o*c.org*/moul.io/assh/pkg/config/#Host
{{. Host . Name }} // localhost
{{. Host . HostName }} // 127.0.0.1
{{. Host . Port }} // 22
{{. Host . User }} // moul
{{. Host . Prototype }} // moul@127.0.0.1:22
{{. Host }} // {"HostName":"localhost","Port":22","User":"moul","ControlPersist":"yes",...}
{{ printf "%s:%s" . Host . HostName . Host . Port }} // localhost:22 连接
一旦assh连接到远程SSH端口, OnConnect就会立即调用。
注意: OnConnect不知道身份验证过程,并且将始终提出。
Golang模板变量的示例:
// Host: http://god*o*c.org*/moul.io/assh/pkg/config/#Host
{{. Host . Name }} // localhost
{{. Host . HostName }} // 127.0.0.1
{{. Host . Port }} // 22
{{. Host . User }} // moul
{{. Host . Prototype }} // moul@127.0.0.1:22
{{. Host }} // {"HostName":"localhost","Port":22","User":"moul","ControlPersist":"yes",...}
{{ printf "%s:%s" . Host . HostName . Host . Port }} // localhost:22
// Stats: http://godoc.o***rg/moul.io/assh/pkg/commands/#ConnectionStats
{{. Stats . ConnectedAt }} // 2016-07-20 11:19:23.467900594 +0200 CEST OnConnectError
当assh未能打开新的TCP连接时,请使用OnConnectError 。
Golang模板变量的示例:
// Host: http://god*o*c.org*/moul.io/assh/pkg/config/#Host
{{. Host . Name }} // localhost
{{. Host . HostName }} // 127.0.0.1
{{. Host . Port }} // 22
{{. Host . User }} // moul
{{. Host . Prototype }} // moul@127.0.0.1:22
{{. Host }} // {"HostName":"localhost","Port":22","User":"moul","ControlPersist":"yes",...}
{{ printf "%s:%s" . Host . HostName . Host . Port }} // localhost:22
// Error
{{. Error }} // dial tcp: lookup localhost: no such host OnDisconnect
当assh插座关闭时, OnDisconnect被称为。
警告:如果关闭SSH连接时看不到通知,则可能配置了ControlMaster ; OnDisconnect与ssh程序没有链接,而是与其插座相关联,即使退出ssh程序也可能会活着。
Golang模板变量的示例:
// Host: http://god*o*c.org*/moul.io/assh/pkg/config/#Host
{{. Host . Name }} // localhost
{{. Host . HostName }} // 127.0.0.1
{{. Host . Port }} // 22
{{. Host . User }} // moul
{{. Host . Prototype }} // moul@127.0.0.1:22
{{. Host }} // {"HostName":"localhost","Port":22","User":"moul","ControlPersist":"yes",...}
{{ printf "%s:%s" . Host . HostName . Host . Port }} // localhost:22
// Stats: http://godoc.o***rg/moul.io/assh/pkg/commands/#ConnectionStats
{{. Stats . ConnectedAt }} // 2016-07-20 11:19:23.467900594 +0200 CEST
{{. Stats . WrittenBytes }} // 3613
{{. Stats . WrittenBytesHuman }} // 3.6kb
{{. Stats . DisconnectAt }} // 2016-07-20 11:19:29,520515792 +0200 CEST
{{. Stats . ConnectionDuration }} // 6.052615198s
{{. Stats . ConnectionDurationHuman }} // 6s
{{. Stats . AverageSpeed }} // 596.933bps
{{. Stats . AverageSpeedHuman }} // 3.4kb/s beforeconfigwrite
在assh重写~/.ssh/config文件之前, BeforeConfigWrite被调用。
Golang模板变量的示例:
{{. SSHConfigPath }} // ~/.ssh/config 钩司机
执行驱动程序
Exec驱动程序使用Golang的模板系统执行shell命令
用法: exec <binary> [args...]
defaults :
Hooks :
OnConnect : exec echo '{{.Host}}' | jq .
# executes: `echo '{"HostName":"localhost","Port":"22","User":"moul","ControlPersist":"yes",...}' | jq .
# which results in printing a pretty JSON of the host
# {
# "HostName": "localhost",
# "Port": "22",
# "User": "moul",
# "ControlPersist": "yes",
# ...
# } defaults :
Hooks :
OnConnect : exec echo 'New SSH connection to {{.Host.Prototype}}.' | mail -s "SSH connection journal" m+ assh @42.am
# send an email with the connection prototype
defaults :
Hooks :
BeforeConfigWrite : exec cp {{.SSHConfigPath}} {{.SSHConfigPath}}.backup
# make a copy of ~/.ssh/config before being rewritten defaults :
Hooks :
AfterConfigWrite : ' exec echo "# date: `date`" >> {{.SSHConfigPath}} '
# Append a comment with the compilation date to the generated ~/.ssh/config file defaults :
Hooks :
AfterConfigWrite : ' exec cat /path/to/my/provider/generated/.ssh/config >> {{.SSHConfigPath}} '
# Append another .ssh/config file to the generated .ssh/config file exec命令正在阻止,计划了一个新的背景任务驱动程序。目前,您可以在这样的背景中运行一份工作:
defaults :
Hooks :
OnConnect :
- exec sleep 60 &
# execute the `sleep 60` command in background (non-blocking)
# if you quit your ssh connection, the process will continue in background. 写驱动程序
写驱动程序使用Golang的模板系统来写入数据
用法: write <line:string...>
defaults :
Hooks :
OnConnect :
- write New SSH connection to {{.Host.Prototype}}.
# writes: "New SSH connection to moul@127.0.0.1:22." on the terminal on connection defaults :
Hooks :
OnDisconnect :
- " write SSH connection to {{.Host.Name}} closed, {{ .Stats.WrittenBytes }} bytes written in {{ .Stats.ConnectionDuration }} ({{ .Stats.AverageSpeed }}) "
# writes: SSH connection to localhost closed, 40 bytes written. 通知驱动程序
通知驱动程序使用Golang的模板系统打开桌面通知。
- Mac OS X :内置支持
- Linux :取决于GNOTIFIER
- Windows :不支持
- BSD :不支持
用法: notify <line:string...>
defaults :
Hooks :
OnConnect : notify New SSH connection to {{.Host.Prototype}}. defaults :
Hooks :
OnDisconnect :
- " notify SSH connection to {{.Host.Name}} closed, {{ .Stats.WrittenBytes }} bytes written in {{ .Stats.ConnectionDuration }} ({{ .Stats.AverageSpeed }}) " 配置
assh现在管理~/.ssh/config文件,请注意备份~/.ssh/config文件。
~/.ssh/ assh .yml是一个包含:
- 一个
hosts字典,包含多个主机定义 - 一个包含全局标志
defaults部分 - 一个
includes包含其他配置文件路径的部分
hosts :
homer :
# ssh homer -> ssh 1.2.3.4 -p 2222 -u robert
Hostname : 1.2.3.4
User : robert
Port : 2222
bart :
# ssh bart -> ssh 5.6.7.8 -u bart <- direct access
# or ssh 5.6.7.8/homer -u bart <- using homer as a gateway
Hostname : 5.6.7.8
User : bart
Gateways :
- direct # tries a direct access first
- homer # fallback on homer gateway
maggie :
# ssh maggie -> ssh 5.6.7.8 -u maggie <- direct access
# or ssh 5.6.7.8/homer -u maggie <- using homer as a gateway
User : maggie
Inherits : bart # inherits rules from "bart"
bart-access :
# ssh bart-access -> ssh home.simpson.springfield.us -u bart
Inherits :
- bart-template
- simpson-template
lisa-access :
# ssh lisa-access -> ssh home.simpson.springfield.us -u lisa
Inherits :
- lisa-template
- simpson-template
marvin :
# ssh marvin -> ssh marvin -p 23
# ssh sad-robot -> ssh sad-robot -p 23
# ssh bighead -> ssh bighead -p 23
# aliases inherit everything from marvin, except hostname
Port : 23
Aliases :
- sad-robot
- bighead
dolphin :
# ssh dolphin -> ssh dolphin -p 24
# ssh ecco -> ssh dolphin -p 24
# same as above, but with fixed hostname
Port : 24
Hostname : dolphin
Aliases : ecco
RateLimit : 10M # 10Mbytes/second rate limiting
schooltemplate :
User : student
IdentityFile : ~/.ssh/school-rsa
ForwardX11 : yes
schoolgw :
# ssh school -> ssh gw.school.com -l student -o ForwardX11=no -i ~/.ssh/school-rsa
Hostname : gw.school.com
ForwardX11 : no
Inherits : schooltemplate
" expanded-host[0-7]* " :
# ssh somehost2042 -> ssh somehost2042.some.zone
Hostname : " %h.some.zone "
vm-*.school.com :
# ssh vm-42.school.com -> ssh vm-42.school.com/gw.school.com -l student -o ForwardX11=yes -i ~/.ssh/school-rsa
Gateways : schoolgw
Inherits : schooltemplate
# do not automatically create `ControlPath` -> may result in error
ControlMasterMkdir : true
" *.shortcut1 " :
ResolveCommand : /bin/sh -c "echo %h | sed s/.shortcut1/.my-long-domain-name.com/"
" *.shortcut2 " :
ResolveCommand : /bin/sh -c "echo $(echo %h | sed s/.shortcut2//).my-other-long-domain-name.com"
" *.scw " :
# ssh toto.scw -> 1. dynamically resolves the IP address
# 2. ssh {resolved ip address} -u root -p 22 -o UserKnownHostsFile=null -o StrictHostKeyChecking=no
# requires github.com/scaleway/scaleway-cli
ResolveCommand : /bin/sh -c "scw inspect -f {{.PublicAddress.IP}} server:$(echo %h | sed s/.scw//)"
User : root
Port : 22
UserKnownHostsFile : /dev/null
StrictHostKeyChecking : no
my-env-host :
User : user-$USER
Hostname : ${HOSTNAME}${HOSTNAME_SUFFIX}
templates :
# Templates are similar to Hosts; you can inherit from them
# but you cannot ssh to a template
bart-template :
User : bart
lisa-template :
User : lisa
simpson-template :
Host : home.simpson.springfield.us
defaults :
# Defaults are applied to each hosts
ControlMaster : auto
ControlPath : ~/tmp/.ssh/cm/%h-%p-%r.sock
ControlPersist : yes
Port : 22
User : bob
Hooks :
# Automatically backup ~/.ssh/config
BeforeConfigWrite :
- ' exec set -x; cp {{.SSHConfigPath}} {{.SSHConfigPath}}.bkp '
AfterConfigWrite :
# Concat another `ssh_config` file with the one just generated by ` assh `
- ' exec cat ~/.ssh/my-heroku-generated-config >> {{.SSHConfigPath}} '
# Alert me with a Desktop notification
- notify "{{.SSHConfigPath}} has been rewritten"
OnConnect :
# Log internal information to a file
- exec printf '{{.}}' | jq . >> ~/.ssh/last_connected_host.txt
# Alert me with a Desktop notification
- notify New SSH connection to {{.Host.Prototype}} at {{.Stats.ConnectedAt}}
# Write the host prototype to the terminal stderr
- write New SSH connection to {{.Host.Prototype}}
OnDisconnect :
# write on terminal and in a Desktop notification some statistics about the finished connection
- " write SSH connection to {{.Host.HostName}} closed, {{.Stats.WrittenBytes }} bytes written in {{.Stats.ConnectionDuration}} ({{.Stats.AverageSpeed}}bps) "
- " notify SSH connection to {{.Host.HostName}} closed, {{.Stats.WrittenBytes }} bytes written in {{.Stats.ConnectionDuration}} ({{.Stats.AverageSpeed}}bps) "
includes :
- ~/.ssh/ assh .d/*.yml
- /etc/ assh .yml
- $ENV_VAR/blah-blah-*/*.yml
assh BinaryPath : ~/bin/ assh # optionally set the path of assh
为了进一步的启发,这些assh .yml在公共GitHub项目上的文件可以教育您如何使用assh
用法
assh
NAME:
assh - advanced ssh config
USAGE:
assh [global options] command [command options] [arguments...]
VERSION:
2.8.0 (HEAD)
AUTHOR(S):
Manfred Touron <https://gi*thu**b.com/moul/assh>
COMMANDS:
ping Send packets to the SSH server and display statistics
info Display system-wide information
config Manage ssh and assh configuration
sockets Manage control sockets
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--config value, -c value Location of config file (default: "~/.ssh/ assh .yml") [$ assh _CONFIG]
--debug, -D Enable debug mode [$ assh _DEBUG]
--verbose, -V Enable verbose mode
--help, -h show help
--version, -v print the version
用法示例
assh config build
重写并替换现有的〜/.ssh/配置文件。
当检测配置更改时,通过assh自动完成此操作。运行此命令可用于设置assh或维修配置文件。
$ assh config build > ~ /.ssh/config assh config list
列出主机和选项。
$ assh config list
Listing entries
*.scw -> root@[hostname_not_specified]:22
StrictHostKeyChecking=no [custom options]
UserKnownHostsFile=/dev/null [custom options]
*.shortcut1 -> bob@[hostname_not_specified]:22
*.shortcut2 -> bob@[hostname_not_specified]:22
bart -> bart@5.6.7.8:22
bart-access -> bob@[hostname_not_specified]:22
dolphin -> bob@dolphin:24
expanded-host[0-7]* -> bob@%h.some.zone:22
homer -> robert@1.2.3.4:2222
lisa-access -> bob@[hostname_not_specified]:22
maggie -> maggie@[hostname_not_specified]:22
marvin -> bob@[hostname_not_specified]:23
my-env-host -> user-moul@[hostname_not_specified]:22
schoolgw -> bob@gw.school.com:22
ForwardX11=no [custom options]
schooltemplate -> student@[hostname_not_specified]:22
ForwardX11=yes [custom options]
IdentityFile=~/.ssh/school-rsa [custom options]
vm-*.school.com -> bob@[hostname_not_specified]:22
(*) General options:
ControlMaster: auto
ControlPath: ~/tmp/.ssh/cm/%h-%p-%r.sock
ControlPersist: yes
Port: 22
User: bob
assh config graphviz
生成主机的GraphViz图
$ assh config graphviz | dot -Tpng > assh -hosts.png
assh config search <keyword>
在主机和主机选项中搜索<keyword> 。
$ assh config search bart
Listing results for bart:
bart -> bart@5.6.7.8:22
bart-access -> moul@[hostname_not_specified]:22
assh info
显示全系统的信息。
$ assh info
Debug mode (client): false
CLI Path: /path/to/ assh
Go version: go1.6.2
OS/Arch: darwin/amd64
RC files:
- ~/.ssh/ assh .yml
- ~/.ssh/ assh .d/hosts.yml
- ~/.ssh/ assh .d/moul.yml
- ~/.ssh/ assh .d/test.yml
Statistics:
- 299 hosts
- 2 templates
- 4 included files
assh sockets list
列出主动控制插座。
$ assh sockets list
4 active control sockets in "~/.ssh/cm/":
- bart/homer/lisa-22-root.sock (14 minutes)
- bart/homer-22-root.sock (14 minutes)
- bart-22-root.sock (14 minutes)
- marge-22-bart.sock (1 hour)
assh sockets flush
关闭主动控制插座。
$ assh sockets flush
Closed 4 control sockets.
assh sockets master
创建一个主控制插座。
$ assh sockets master assh ping
将数据包发送到SSH服务器并显示统计数据。
$ assh ping -c 4 localhost
PING localhost (127.0.0.1) PORT 22 (ssh) PROTO tcp
Connected to 127.0.0.1: seq=0 time=321µs protocol=tcp port=22
Connected to 127.0.0.1: seq=1 time=501µs protocol=tcp port=22
Connected to 127.0.0.1: seq=2 time=550µs protocol=tcp port=22
Connected to 127.0.0.1: seq=3 time=641µs protocol=tcp port=22
--- localhost assh ping statistics ---
4 packets transmitted, 4 packets received, 0.00% packet loss
round-trip min/avg/max = 321µs/503.25µs/641µs
安装
使用GO获取最新版本(推荐方法):
go install moul.io/ assh /v2@latest注意:使用GO1.7或更高版本测试
使用Homebrew(Mac OS X)获取最新发布的版本:
brew install assh构建最新版本
brew install assh --HEAD获取发布版本:https://github.com/moul/assh/releases
使用ASDF-VM安装:
asdf plugin add assh
asdf install assh latest
asdf global assh latest
注册包装器(可选)
要在使用高级模式匹配时提高经验,请在.bashrc / .zshrc / config.fish的末尾添加以下内容:
alias ssh= " assh wrapper ssh -- "此步骤不是强制性的,但强烈建议。
注意: ssh不了解高级模式;为了绕过这一限制, assh维护已知主机的列表,并使用所有这些已知的已知主机再生~/.ssh/config 。
如果没有包装器,则首次连接到新主机时将过时的~/.ssh/config风险,您需要再次启动该命令。
使用包装器, ssh将始终使用更新的~/.ssh/config文件调用。
入门
- 备份您的旧
~/.ssh/config:cp ~/.ssh/config ~/.ssh/config.backup - 创建一个新的
~/.ssh/ assh .yml文件 - 运行
assh config build > ~/.ssh/config以验证您的~/.ssh/ assh .yml文件的语法,并自动构建您的~/.ssh/config文件 - 你准备好了!
WebApp
assh包含一个在Heroku上托管的实验Web应用程序:https://assh.herokuapp.com/
将assh .yml文件转换为ssh_config格式:
$ http --form POST https://assh-dev.h*erok**uapp.com/ assh -to-ssh assh _config=@~/.ssh/ assh .d/test.yml | jq -r .ssh_config
# This file was automatically generated by assh v2.8.0
# on 2018-07-03 21:06:56 +0000 UTC, based on ~ /.ssh/ assh .yml
#
# more info: https://gi*th*ub.*com/moul/assh
# host-based configuration
通过命令行克隆项目: