«

expect实用示例

时间:2024-2-18 10:27     作者:韩俊     分类: Linux


1. 自动登录开发环境服务器

#!/usr/bin/expect

set timeout 30
#获取输入的第一个参数,赋值给变量docker_which
set docker_which [lindex $argv 0]
#如果docker_which无值,则默认值设置为0
if {"$docker_which"==""} {
    set docker_which 0
}
#登录149
spawn sudo ssh -p 8044 yhm@123.56.71.70
expect "*password*"
send "maopiaopiao.com\r"
expect "*login:*"
#切换账号
send "su b\r"
expect "*密码*"
send "b\r"
#进入docker
send "ssh docker\r"
expect "login:"
#进入docker c环境
send ". gotoc $docker_which\r"
expect "*root@*"
#进入数据库环境
send "db\r"
expect "*mysql>*"
send "show databases;\r"
expect "*rows in set*"
send "use phparticle_db;\r"
interact

2. 查看日志

#!/usr/bin/expect

set timeout 30
set date [ clock format [ clock seconds ] -format "%Y-%m-%d" ]
set env_which [lindex $argv 0]
set file_dir /data/t/xnw/laravel/storage/logs/
if {"$env_which"==""} {
    set env_which local
}
set file [lindex $argv 1]
if {"$file"==""} {
    #set file "$file_dir"laravel-$date.log
    set file /data/t/xnw/laravel/storage/logs/laravel-$date.log
}
switch -- $env_which {
    local {
        spawn gotoc
        expect "*oot@*"
    }
    xdev0 {
        spawn sudo ssh -p 8044 yhm@192.168.2.149
        expect "*password*"
        send "123456\r"
        expect "*login:*"
        send "su b\r"
        expect "*密码*"
        send "b\r"
        send "ssh docker\r"
        expect "login:"
        send ". gotoc 0\r"
        expect "*root@*"
    }
    demo0 {
        spawn sudo ssh -p 8044 yhm@192.168.2.149
        expect "*password*"
        send "123456\r"
        expect "*login:*"
        send "su b\r"
        expect "*密码*"
        send "b\r"
        send "ssh demo\r"
        expect "login:"
        send ". gotoc 0\r"
        expect "*root@*"
    }
}
send "tail -f $file\r"
interact

3. 自动打开今天的日志

#!/usr/bin/expect

set timeout 30
set date [ clock format [ clock seconds ] -format "%Y-%m-%d" ]
spawn sudo ssh -p 8044 yhm@192.168.2.149
expect "*password*"
send "123456\r"
expect "*login:*"
send "su b\r"
expect "*密码*"
send "b\r"
send "ssh w167\r"
expect "uploader@"
send "cd /data/t/xnw\r"
expect "uploader@"
send "tail -f laravel/storage/logs/laravel-$date.log\r"
interact

4. 自动登录到服务器的docker环境 (2020年09月27日09:56:53 更新)

#!/usr/bin/expect

set timeout 30
set docker_which [lindex $argv 0]
if {"$docker_which"==""} {
set docker_which 0
}
set date [ clock format [ clock seconds ] -format "%Y-%m-%d" ]
spawn sudo ssh -p 8044 yhm@192.168.2.149
expect "*password*"
send "123456\r"
expect "*login:*"
send "su b\r"
expect "*密码*"
send "b\r"
send "ssh docker\r"
expect "login:"
send ". gotoc $docker_which\r"
expect "*root@*"
send "cd /data/t/xnw/laravel\r"
expect "*root@*"
send "tail -f storage/logs/laravel-$date.log\r"
interact

标签: linux

热门推荐