autossh版本說明
目前發現同樣叫autossh的,有好幾個項目
- yum,brew中的自動重啟ssh會話和隧道
- FeeiCN的autossh
- islenbo的autossh
后面兩個的主要是用於記錄密碼,遠程連接
Feeicn版安裝使用
安裝依賴:
Linux
yum install expect
apt-get install expect
安裝autossh
$ git clone https://github.com/FeeiCN/autossh.git
$ sudo cp autossh/autossh /usr/local/bin/
autossh依賴expect,所以要先安裝expect.
$ cat ~/.autosshrc
server_name|192.168.1.110|root|password|port|is_bastion
demo:
wufeifei|192.168.1.1|root|password|22|1
用法
➜ autossh 1
############################################################
# [AUTO SSH] #
# #
# #
# [1] dev - 18.16.200.110:root #
# #
# #
############################################################
spawn ssh -p22 -l root 18.16.200.110
Last login: Mon Sep 21 23:55:59 2020 from 18.16.1.11
[root@server110 ~]#
islenbo版安裝使用
一鍵安裝
- 下載與當前操作系統相匹配的壓縮包並解壓,運行install腳本,程序將自動安裝至/autossh目錄,並在/.bash_profile中更新PATH環境變量,使autossh能在任意目錄生效。
手動安裝
- 下載與當前操作系統相匹配的壓縮包,解壓到自定義目錄,Mac/Linux 用戶推薦解壓到 ~/autossh 目錄。
- 修改環境變量,在PATH中添加 ~/autossh
配置文件說明
show_detail: bool <是否顯示服務器詳情(用戶、IP)>
options:
ServerAliveInterval: int <是否定時發送心跳包,與SSH ServerAliveInterval屬性用法相同>
servers:
- name: string <顯示名稱>
ip: string <服務器IP>
port: int <端口>
user: string <用戶名>
password: string <密碼>
method: string <鑒權方式,password-密碼 key-密鑰>
key: string <私鑰路徑>
options:
ServerAliveInterval: int <與根節點ServerAliveInterval用法相同,該值會覆蓋根節點的值>
alias: string <別名>
log:
enable: bool <是否啟用日志>
filename: string <日志路徑, 如 /tmp/%n-%u-%dt.log 支持變量請參考下方《日志變量》章節>
mode: string <遇到同名日志的處理模式,cover-覆蓋 append-追加,默認為cover>
groups:
- group_name: string <組名>
prefix: string <組前綴>
servers: array <服務器列表,配置與servers相同,配置說明略>
collapse: bool <是否折疊>
proxy:
type: string <代理方式,目前僅支持SOCKS5>
server: string <代理服務器地址>
port: int <端口號>
user: string <用戶,若無留空>
password: string <密碼,若無留空>
日志變量
| 變量 | 說明 | 示例 |
|---|---|---|
| %g | 組名(group_name) | MyGroup1 |
| %n | 顯示名稱(name) | vagrant |
| %u | 用戶名(user) | root |
| %a | 別名(alias) | vagrant |
| %dt | 日期時間 | 20190821223010 |
| %d | 日期 | 20190821 |
常規用法
將autossh放入PATH環境變量后,可在任意目錄執行autossh命令,彈出菜單,輸入相應的服務器編號或別名即可自動登錄。
指定配置文件
autossh 默認讀取同目錄下的 config.json 文件,你也可以指定其他配置,使用 -c 或 -config 參數,如:
autossh -c=file
autossh -config=file
快捷登錄
當你已經記住某一台服務器的編號或別名之后,可直接輸入 autossh 編號/別名 實現快捷登錄,如:
autossh 1
autossh vagrant
cp 文件傳輸
文件傳輸的用法類似scp,但使用的是sftp協議,支持 local <-> remote 和 remote <-> remote 傳輸。
local -> remote
# 1:/tmp/1.txt 表示config.json中編號為1的服務器的/tmp/1.txt文件
autossh cp ./test.txt 1:/tmp/1.txt
# vagrant 表示config.json中別名為vagrant的服務器
autossh cp ./test.txt vagrant:/tmp/1.txt
remote -> local
autossh cp 1:/tmp/1.txt ./test.txt
remote -> remote
autossh cp 1:/tmp/1.txt 2:/tmp/1.txt
多文件傳輸
autossh cp ./1.txt ./2.txt 1:/tmp/
autossh cp 1:/tmp/1.txt 1:/tmp/2.txt ./
文件夾傳輸
autossh cp -r ./dir 1:/tmp
更新檢測
autossh upgrade
參考
Mac上使用Item2+lrzsz+autossh管理Linux服務器
