Expect是一个用来实现自动交互功能的软件套件 (Expect [is a] software suite for automating interactive tools)。使用它系统管理员可以创建脚本用来实现对命令或程序提供输入,而这些命令和程序是期望从终端(terminal)得到输入,一般来说这些输入都需要手工输入进行的。 Expect则可以根据程序的提示模拟标准输入提供给程序需要的输入来实现交互程序执行。

需要安装expect、tcl包:

yum install expect tcl

示例脚本:

 #!/usr/bin/expect、
set timeout
set username [lindex $argv ]
set password [lindex $argv ]
set host [lindex $argv ]
spawn ssh $username@$host
expect "assword:"
send "$password\r"
expect "#"
send "touch a.txt\r"
send "touch b.txt\r"
send "touch c.txt\r"
send "touch d.txt\r"
send "exit\r"
expect eof
exit
#interact

说明:

#!/usr/bin/expect

这一行告诉操作系统脚本里的代码用那一个shell来执行。这里的expect其实和linux下的bash、windows下的cmd是一类东西。

set timeout 10

设置超时时间的,计时单位是:秒。timeout -1 为永不超时

set username [lindex $argv 0]

set password [lindex $argv 1]

set host [lindex $argv 2]

$argv 参数数组,expect脚本可以接受从bash传递过来的参数.可以使用[lindex $argv n]获得,n从0开始,分别表示第一个,第二个,第三个....参数

spawn ssh $username@$host

spawn是进入expect环境后才可以执行的expect内部命令。它主要的功能是给ssh运行进程加个壳,用来传递交互指令。

expect "assword:"

这里的expect也是expect的一个内部命令, expect的shell命令和内部命令是一样的,但不是一个功能。这个命令的意思是判断上次输出结果里是否包含“password:”的字符串,如果有则立即返回,否则就等待一段时间后返回,这里等待时长就是前面设置的10秒 .

send "$password\r"

这里就是执行的交互动作,与手工输入密码的动作等效。

注意: 命令字符串结尾别忘记加上“\r”,如果出现异常等待的状态可以核查一下。

expect "]#"

登录成功后匹配的bash提示符。

send "touch a.txt\r"

send "touch b.txt\r"

send "touch c.txt\r"

send "touch d.txt\r"

在远程主机登录成功后执行的命令。

send "exit\r"

退出登录的远程终端的shell。

expect eof

exit

结束expect的交互。

interact

执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。如果你只是登录过去执行

执行脚本方法:

./auto_ssh.sh $user $password $ipaddr

$user :登录远程终端的用户名

$password:远程终端用户的密码

$ipaddr:远程主机的IP地址

执行脚本:

 [root@host122 test]#./auto_ssh.sh  root    192.168.68.239
spawn ssh root@192.168.68.239
root@192.168.68.239's password:
Last login: Thu Oct :: from 192.168.68.232
[root@host129 ~]# touch a.txt
[root@host129 ~]# touch b.txt
[root@host129 ~]# touch c.txt
[root@host129 ~]# touch d.txt
[root@host129 ~]# exit
logout
Connection to 192.168.68.239 closed.

验证:

 [root@host129 ~]# ls -l
总用量
-rw-r--r-- root root 10月 : a.txt
-rw-r--r-- root root 10月 : b.txt
-rw-r--r-- root root 10月 : c.txt
-rw-r--r-- root root 10月 : d.txt

遇到下面的错误提示:

spawn: command not found

解决方法,给脚本加上执行权限,并且不要用sh auto_ssh.sh的方式执行:

Chmod +x auto_ssh.sh

./auto_ssh.sh

最新文章

  1. 20161025__Oracle10g双机备份
  2. Android5.0新特性——兼容性(support)
  3. MongoDB(一)
  4. Asp.Net Core- 配置组件详解
  5. Java访问kafka的时候java.nio.channels.ClosedChannelException解决办法
  6. Swift中类的初始化器与继承
  7. Noip2008提高组总结
  8. LAMP和LNMP,你更愿意选择谁,为什么?
  9. Mybatis框架基础支持层——反射工具箱之实体属性Property工具集(6)
  10. Mysql增量写入Hdfs(一) --将Mysql数据写入Kafka Topic
  11. SpringCloud使用Nacos服务发现实现远程调用
  12. callable函数 stride的意义 Math.round(),Math.ceil(),Math.floor()用法
  13. Codeforces 452E Three strings 字符串 SAM
  14. 【PAT】B1050 螺旋矩阵(25 分)
  15. Python实现文件备份
  16. java的构造方法链
  17. Qt QByteArray或者Char转十六进制 QString
  18. PHP_EOL 写入字符串换行 , php获取毫秒 microtime
  19. json解析2
  20. ubuntu16下面 redis 无法链接到客户端问题

热门文章

  1. QTP安装和破解
  2. SMG12232ZK标准字符点阵型液晶显示模块的演示程序[C51编程语言][MCS51并行接口方式]
  3. PHP算法之二分查找和顺序查找
  4. leetcode@ [289] Game of Life (Array)
  5. Visual Studio 2008 – ASP.NET “System.Runtime.InteropServices.COMException”
  6. linux大于2T的磁盘使用GPT分区方式
  7. Java Serializable(序列化)的理解和总结、具体实现过程(转)
  8. (二)Bootstrap CSS 概览
  9. Stage3D学习笔记(六):旋转动画效果
  10. Oracle- plsql developer如何查询SQL语句执行历史记录