PHP连接MySQL数据库时expect,expect实用示例

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 "phpernote.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 "xnw.com\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 "xnw.com\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 "xxx.com\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 "xnw.com\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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值