对于记不住的服务器密码且不愿用三方工具简单写了个脚本检测下最近shell脚本学习效果咋样)

expect处理交互的一种脚本语言spawn启动指定进程 -> expect获取指定关键字 -> send想指定进程发送指定指令 -> 执行完成后退出

1、安装工具

linux

yum install expect

mac

 brew install expect

2、脚本内容

 #!/usr/bin/expectset web1 服务器1ip
 set web2 服务器2ip
 ​
 puts "服务器列表"
 puts "1. 服务器1备注名称"
 puts "2. 服务器2备注名称"set timeout -1
 expect_user -re "(.*)n"set num $expect_out(1,string)switch -exact -- $num {
     1 {
        spawn ssh root@$web1
        expect "password:"
        send "服务器1密码r"
        interact
    }
     2 {
        spawn ssh root@$web2
        expect "password:"
        send "服务器2密码r"
        interact
    }
    default {
        puts "输入序号错误"
    }
 }

使用

gwh@GWdeMacBook-Pro [19:16:08] [~/Desktop]
-> % expect jumpServer.exp

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注