事倍功半是蠢蛋36 3分钟完成 xshell自动登陆脚本

需求:每次xshell登陆都要输入su切root 麻烦,自动化让连接上就让他是root
实现(3分钟完成):
1.新建auto_su.exp文件,用xftp上传到登陆后的目录
2.安装expect 工具
3.配置xshell自动执行脚本命令,让他自动执行.exp 然后切成root

第一步:

#!/usr/bin/expect -f
set timeout 10
set su_password "root"

spawn su -

expect {
    "Password:" {
        send "$su_password\r"
        expect {
            "#" {
                send_user "\n=== 已成功切换到 root 用户 ===\n"
                interact
            }
            "su: Authentication failure" {
                send_user "\n错误:密码验证失败\n"
                exit 1
            }
        }
    }
}

第二步:

# 检查 expect 是否安装
which expect

# 如果没有安装,使用以下命令安装
sudo apt update
sudo apt install expect

第三步:
image

posted @ 2025-07-21 14:38  空心橙子  阅读(41)  评论(0)    收藏  举报