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

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。

expect的安装与使用的更多相关文章

  1. [转]expect的安装

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

  2. linux下expect环境安装以及简单脚本测试

    expect是交互性很强的脚本语言,可以帮助运维人员实现批量管理成千上百台服务器操作,是一款很实用的批量部署工具!expect依赖于tcl,而linux系统里一般不自带安装tcl,所以需要手动安装 下 ...

  3. expect神器安装和使用

    安装: mdkir /data/tools cd /data/tools wget http://prdownloads.sourceforge.net/tcl/tcl8.5.19-src.tar.g ...

  4. expect交互式安装软件

    公司一些宿主机需要安装软件,吴老师要求写一个安装脚本: 脚本思路:首先要把安装的包拷贝到每台机器上,然后要让每台机器都运行一次安装命令:就想到了应用scp.ssh命令,但这两个命令需要输入对端密码,需 ...

  5. Linux Tcl和Expect的安装

    一.先安装Tcl 1.下载:tcl版本 8.4.19 http://sourceforge.net/projects/tcl/files/Tcl/8.4.19/tcl8.4.19-src.tar.gz ...

  6. Linxu下 expect的安装与使用

    expect学习 1.什么是except        Expect是基于Tcl的一个相对简单的免费脚本文件语言工具,用于实现自动和交互式程序进行通信            is a software ...

  7. Linux expect的安装与使用

    Expect是在Tcl的基础上创建的,可以用来做一些Linux下无法做到交互的命令操作,可用于远程管理服务器. 一.安装Tcl: 1.下载源码包: wget http://nchc.dl.source ...

  8. expect离线安装

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

  9. expect 安装使用

    expect 命令相当于crt远程连接,可用于脚本化实现多服务器巡检功能. 一.expect 命令安装: 1.rpm 文件下载:百度云链接:http://pan.baidu.com/s/1sl1wSU ...

随机推荐

  1. KVM 网络虚拟化基础

    网络虚拟化是虚拟化技术中最复杂的部分,学习难度最大. 但因为网络是虚拟化中非常重要的资源,所以再硬的骨头也必须要把它啃下来. 为了让大家对虚拟化网络的复杂程度有一个直观的认识,请看下图 这是 Open ...

  2. Resin Thread Dump

    [2015/08/25 20:50:13.254] {ThreadLauncher2[ThreadPool[system]]-1} Thread Dump generated Tue Aug 25 2 ...

  3. matlab安装及破解

    Matlab安装及破解: 笔者最近要做一些和建模相关的事,故此需要安装Matlab2017版.在此做下笔记. 网盘链接: 链接:https://pan.baidu.com/s/1lN8C7TDFjSV ...

  4. Mybatis详解

    SqlSession(SqlSessionDaoSupport类) SqlSessionDaoSupportSqlSessionDaoSupport是一个抽象的支持类,用来为你提供SqlSession ...

  5. cef network-settings

    Network Settings 目录 1 System network settings 2 Preference service for network settings 3 Command-li ...

  6. Java 复制字符串到系统剪贴板

    原文:http://www.open-open.com/code/view/1453188435933 public static void main(String[] args) { StringS ...

  7. Angularjs: call other scope which in iframe

    Angularjs: call other scope which in iframe -------------------------------------------------------- ...

  8. 转:为什么Uber宣布从Postgres切换到MySQL?

    转: http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=2653547609&idx=1&sn=cbb55ee823dd ...

  9. 关于Java基础的一些笔试题总结

    针对近期腾讯.京东.网易等公司的笔试,遇到一些有关Java基础的问题,在此总结,希望能通过这几道经典问题题发散,举一反三,借此打牢基础!自己总结,望提出宝贵意见! 一.关于null的一道小题  先开开 ...

  10. iOS知识点全梳理-b

    感谢分享 原文链接:http://www.jianshu.com/p/5d2163640e26 序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了.今年,找过工作人可能会更深刻地体会 ...