返回

跳板机工具Lssh技巧介绍

   
摘要GPT
摘要小助理暂时失联跑路啦……😜

项目:https://github.com/blacknon/lssh/

wiki: https://github.com/blacknon/lssh/wiki/Config

以下教程基于ubuntu20.04

安装 go

wget https://go.dev/dl/go1.22.2.linux-amd64.tar.gz  #下载go程序

rm -rf /usr/local/go && tar -C /usr/local -xzf go1.22.2.linux-amd64.tar.gz #安装

配置环境变量

sudo nano /etc/profile

末尾添加

export PATH=$PATH:/usr/local/go/bin

CTRL+X 保存,y确认,回车

使其立即生效

source /etc/profile

检测

go version #输出go version go1.22.2 linux/amd64 信息

安装 依赖

sudo apt install make
sudo apt update
sudo apt install build-essential

安装lssh

git clone https://github.com/blacknon/lssh
cd lssh
GO111MODULE=auto make && sudo make install
test -f ~/.lssh.conf||curl -s https://raw.githubusercontent.com/blacknon/lssh/master/example/config.tml -o ~/.lssh.conf

添加主机

编辑 /root/.lssh.conf 文件

[common]
port = "22"                       
user = "root"     
key  = "~/.ssh/id_rsa"  
pre_cmd = "echo 'SSH连接中。。。'"
post_cmd = "find /root/log/lssh -mindepth 2 -maxdepth 2 -type d -name 'logdir' -exec sh -c 'cd \"{}\" && ls -t | tail -n +2 | xargs rm -f' \\;"

[server.node1]
addr = "192.168.1.8"
port = "33"                       
user = "root"
pass = "" 
note = "node1主机"


[server.node3]
addr = "192.168.1.6"
port = "55"                       
user = "root"
pass = ""
note = "node3主机"


#[includes]
path = [
     "~/.lssh.conf.include1"
    ,"~/.lssh.conf.include2"
]

[log]
enable = true                       
timestamp = true                    
dirpath = "~/log/lssh/<Date>_<Hostname>/logdir"

写法

[common] 代表通用配置,主机配置可以覆盖通用配置

port = "22"          #假设所有主机默认ssh端口为22,可以是其他端口       
user = "root"        #默认用户root
key  = "~/.ssh/id_rsa"   #默认密钥路径
pre_cmd = "echo 'SSH连接中。。。'"  #连接前执行的命令,可以是其他命令
post_cmd = "find /root/log/lssh -mindepth 2 -maxdepth 2 -type d -name 'logdir' -exec sh -c 'cd \"{}\" && ls -t | tail -n +2 | xargs rm -f' \\;"  #退出后的命令

[server.主机昵称] 主机配置,主机昵称可以随意,不能是中文和重复,但前面必须加 server.

例如:[server.node3] 、[server.node2] 按照以下写法逐个添加

[server.node1]
addr = "192.168.1.8"  # ip地址
port = "33"           #ssh端口号33,如果与通用配置一样,则可以为空         
user = "root"         #如果与通用配置一样,可以留空  
pass = ""             #主机登录密码,如果使用密钥连接,可以为空
note = "node1主机"    #主机备注,方便查找

[includes] 配置,表示可以指定其他文件作为主机配置,写法与主机配置一样,一般不用

#[includes]
path = [
     "~/.lssh.conf.include1"
    ,"~/.lssh.conf.include2"
]

[log] 主机操作日志,所有在终端敲打的命令都会记录保存在 ~/log/lssh/日期_主机昵称/logdir目录

[log]
enable = true                       
timestamp = true                    
dirpath = "~/log/lssh/<Date>_<Hostname>/logdir"

这条命令表示只保留最新一个日志,在退出ssh连接时执行

post_cmd = "find /root/log/lssh -mindepth 2 -maxdepth 2 -type d -name 'logdir' -exec sh -c 'cd \"{}\" && ls -t | tail -n +2 | xargs rm -f' \\;"

使用方法

登录主机

lssh    #使用键盘上下方向键选择,也可以用鼠标,最上方可以输入关键字查找,回车登录

发送命令

lssh hostname  #可以按ALT+A全选,也可单独选一台主机,或者输入关键字查找

上面的命令只是例子,写法:lssh 后面加命令即可

并行运行命令

lssh -p uname -a

上面的命令只是例子,写法:lssh 后面加 -p

交互命令

lssh -s

scp命令

写法: 远程路径前面加 r: 不能单独使用 lscp 命令

lscp /path/to/123.txt r:/path/to/remote

例如:lcsp 123/uuu.txt r:./123 复制/root/123/uuu.txt到远程主机(选择后)

本地复制到远程

# lscp local => remote(multiple)
lscp /path/to/local... r:/path/to/remote

远程复制到本地(这里有个bug,会进入选择界面,直接回车)

# lscp remote(multiple) => local
lscp r:/path/to/remote... /path/to/local

远程到远程(先选择第一个主机,再选择第二个主机)

# lscp remote => remote(multiple)
lscp r:/path/to/remote... r:/path/to/local

lsftp命令 推荐好用

lsftp  #可选择多台主机

本地到远程

put /path/to/local ./

例如 put 123/xx.md ./123 复制xx.md文件到远程主机/root/123目录下

远程到本地

get /path/to/remote ./

知识共享许可证 CC BY-NC-SA 4.0
最后更新于 2024-07-17 18:28
使用 Hugo 构建
主题 hugo-magic小洋葱 魔改 由 Jimmy 设计
Written by Human, Not by AI