expect的安装与使用

expect的安装与使用

是什么

expect 是用来进行自动化控制和测试的工具。主要是和交互式软件telnet ftp ssh 等进行自动化的交互。

如何安装

1.检测是否安装

ls /usr/bin |grep expect

如果不存在,则进行安装

2.安装

sudo apt-get install expect
$ ls /usr/bin |grep expect
autoexpect
expect
expect_autoexpect
expect_autopasswd
expect_cryptdir
expect_decryptdir
expect_dislocate
expect_ftp-rfc
expect_kibitz
expect_lpunlock
expect_mkpasswd
expect_multixterm
expect_passmass
expect_rftp
expect_rlogin-cwd
expect_timed-read
expect_timed-run
expect_tknewsbiff
expect_tkpasswd
expect_unbuffer
expect_weather
expect_xkibitz
expect_xpstat

具体使用

案例一,进入ssh脚本

spawn是进入expect环境后才可以执行的expect内部命令。expect是一种脚本语言,它能够代替我们实现与终端的交互,我们不必再守候在电脑旁边输入密码,或是根据系统的输出再运行相应的命令。

1.创建脚本

#! /usr/bin/expect

# 设置超时时间
set timeout 3
# fork一个子进程执行ssh
spawn ssh root@xxx.xxx.xxx.xxx

# 捕获到密码
expect "*password*"
# 输入密码并回车
send "xxxxxx\r"

# 捕获#
expect "*#"
# 进入常用目录下
send "cd /home/wwwroot/default\r"

# 允许用户进行交互
interact          

2.创建权限

sudo chmod +x xxx.sh

3.执行脚本

./xxx.sh
jiqing@Ubuntu:~/sh$ ./xxx.sh 
spawn ssh root@xxx
root@xxx's password: 
Last login: Thu Jun 21 15:07:03 2018 from 218.93.209.10

Welcome to Alibaba Cloud Elastic Compute Service !

[root@iZuf6ingetk3pr7xgv1vq1Z ~]# cd /home/wwwroot/default
[root@iZuf6ingetk3pr7xgv1vq1Z default]# 

优化通用版本,支持第一次yes判断,支持ip输入

#! /usr/bin/expect
set ip [lindex $argv 0]
set password [lindex $argv 1]
if {$ip == ""} {
    puts "请输入ip"
    exit 
}

if {$password == ""} {
    set password "123456"
}

# 设置超时时间
set timeout 3
# fork一个子进程执行ssh
spawn ssh root@$ip

expect {  
    "*yes/no*" { send "yes\r"; exp_continue}
    "*password*" { send "$password\r" }
}  

# 捕获到密码
# expect "*password*"
# 输入密码并回车
# send "$password\r"

# 捕获#
expect "*#"
# 进入常用目录下
send "cd /home/wwwroot/default\r" 

# 允许用户进行交互
interact

继续升级成昵称,比ip更好用

#! /usr/bin/expect
set pro_name [lindex $argv 0]
set password [lindex $argv 1]
if {$pro_name == ""} {
    puts "请输入名称"
    exit 
}

switch $pro_name {
    "meiren" -
    "yanglu" -
    "wenbo" {
        set ip "ip1"
    }
    "siemens" {
        set ip "ip2"
    }
    "tqmp" {
        set ip "ip3"
    }
    default {
        puts "请输入正确的名称"
        exit
    }
}

if {$password == ""} {
    set password "xxx"
}

# 设置超时时间
set timeout 3
# fork一个子进程执行ssh
spawn ssh root@$ip

expect {  
    "*yes/no*" { send "yes\r"; exp_continue}
    "*password:*" { send "$password\r" }
}  

# 捕获到密码
# expect "*password*"
# 输入密码并回车
# send "$password\r"

# 捕获#
expect "*#"
# 进入常用目录下
send "cd /home/wwwroot/default\r" 

# 允许用户进行交互
interact

422101-20180622150305506-1793129647.png

666 ,一个脚本,一键链接ssh。

案例二,进行cd操作
#! /usr/bin/expect
# 跳转到项目目录下
set pro_name [lindex $argv 0]
spawn bash
if {$pro_name != ""} {
    set target_dir "/home/wwwroot/default/$pro_name"
} else {
    set target_dir "/home/wwwroot/default"
}
# 判断目录是否存在
if {![file isdirectory $target_dir]} {
    puts "项目目录不存在"
    set target_dir "/home/wwwroot/default"
}
send "cd $target_dir\r"
interact

ps:expect的语法与shell脚本有点不同,多用用就习惯了。运用起来,让他们帮助你更好的工作。

更多功能,需要在工作中去探索和使用。我爱linux。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值