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.10Welcome 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@$ipexpect {  "*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@$ipexpect {  "*yes/no*" { send "yes\r"; exp_continue}"*password:*" { send "$password\r" }
}  # 捕获到密码
# expect "*password*"
# 输入密码并回车
# send "$password\r"# 捕获#
expect "*#"
# 进入常用目录下
send "cd /home/wwwroot/default\r" # 允许用户进行交互
interact

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。

转载于:https://www.cnblogs.com/jiqing9006/p/9209178.html

expect的安装与使用相关推荐

  1. zz Expect的安装

    转载一篇靠谱的文章,按照文章所述方法一次成功.只不过我的expect二进制文件最后实在tcl的bin目录下,而不是expect的bin目录下,这个令我有些疑惑,whatever,不算什么大问题,注意一 ...

  2. [转]expect的安装

    转自:http://blog.chinaunix.net/uid-20639775-id-2453085.html Expect是在Tcl基础上创建起来的,它还提供了一些Tcl所没有的命令,它可以用来 ...

  3. expect() php,Linux expect 的安装

    expect是建立在tcl基础上的一个工具,它还提供了一些Tcl所没有的命令,它可以用来做一些linux下无法做到交互的一些命令操作,在远程管理方面发挥很大的作用. 因为expect是基于tcl的,所 ...

  4. linux expect命令安装包,LINUX EXPECT的安装

    LINUX EXPECT的安装 2008-07-07 21:48:34 原创作品,允许转载,转载时请务必以超链接形式标明文章 今天同事要求在两台新装的LINUX系统上安装expect.说实话还真不知道 ...

  5. Expect的安装与应用,及实现自动检测另外一台服务器运行状态并重启,和使用expect脚本远程批量管理服务器与日志分析

    学习Expect Expect是什么? Expect是一个免费的编程工具语言,用来实现自动和交互式任务进行通信,而无需人的干预.  Expect是不断发展的,随着时间的流逝,其功能越来越强大,已经成为 ...

  6. expect 编译安装

    expect脚本 Expect 脚本 Expect是一个免费的编程工具语言,用来实现自动和交互式任务进行通信,而无需人的干预. Expect的作者Don Libes在1990年开始编写Expect时对 ...

  7. expect离线安装

    expect5.45.4.tar.gz和tcl8.4.11-src.tar.gz压缩包请前往以下链接下载: https://download.csdn.net/download/gangzi221/1 ...

  8. linux yum安装expect,CentOS安装expect

    CentOS安装expect 发布时间:2020-02-25 10:04:28 来源:51CTO 阅读:49 作者:844365389 expect是在tcl基础上创建起来的,因此在安装expect之 ...

  9. expect安装使用

    Expect是在Tcl基础上创建起来的,它还提供了一些Tcl所没有的命令,它可以用来做一些linux下无法做到交互的一些命令操作,在远程管理方面发挥很大的作用. spawn命令激活一个Unix程序来进 ...

  10. Linux工作笔记-解决spawn: not found与expect: not found问题(安装expect)

    首先要下载安装2个程序: 1.tcl:下载链接如下: 8.4.11版本:https://download.csdn.net/download/qq78442761/10783733 8.4.19版本( ...

最新文章

  1. mysql5.7复制集_mysql--replication复制集典型配置
  2. 专家首次释疑“接触传播”:手污染到病毒后,再揉眼睛可能会感染
  3. Qt Creator下载和安装(详细教程)以及如何发布可执行程序
  4. GitHub 上 57 款最流行的开源深度学习项目【转】
  5. PyTorch 加载超大 Libsvm 格式数据
  6. PHP artisan auth,Php artisan make:auth命令未定义
  7. DevExpress VCL Controls v15.1.5正式发布[附下载]
  8. spring学习(36):注入简单类型
  9. Python_操作txt、xls、csv、PDF
  10. oracle 11g空表不能exp导出问题解决方案
  11. Linux命令df,du 查看系统磁盘空间
  12. hadoop eclipse插件
  13. TCPIP详解卷一概述 学习记录 2020/4/13
  14. 朋友圈加粗字体数字_字体:新游黑体(游ゴシック)重大更新,精巧的日系字体~...
  15. 【嵌入式模块】DS18B20 数字温度传感器
  16. 树莓派所用到的软件工具及获取方法汇总
  17. 【报告分享】2021巨量引擎金融行业生态及用户洞察报告-巨量算数(附下载)
  18. 实验四 201771010101 白玛次仁
  19. 同行评审流程与撰写审稿意见要点
  20. oracle11g闪退 win7,win7 oracle11g

热门文章

  1. 亮剑:PHP,我的未来不是梦(6)
  2. 一些不错的理论[转载]
  3. 再见2006,奋斗2007
  4. samba (centos6.5)服务
  5. Android Studio右下角显示当前branch名称不一致
  6. 360的编码html怎么写,html5之meta charset网页字符编码简写
  7. JSP的Listener介绍
  8. 【渝粤教育】国家开放大学2018年春季 0161-22T教师职业道德 参考试题
  9. 【渝粤题库】陕西师范大学200261 复变函数 作业(专升本、高起本)
  10. CFileDialog用法详解