spawn命令自动安装oracle,Linux Expect多台批量执行命令

expect是建立在tcl基础上的一个工具,它用来让一些需要交互的任务自动化地完成。

因为expect是基于tcl的,所以需要你的系统中安装有tcl

如何检查?

[root@dev ~]# whereis tcl

tcl: /usr/lib/tcl8.4 /usr/share/tcl8.4

如果看不到结果,请先安装tcl

安装

> 安装tcl

解压tcl安装包后

cd tcl8.4.11/unix/

./configure --prefix=/usr/local/tcl/ --enable-shared

make && make install

> 安装expect

解压expect安装包后

cd expect-5.43

./configure --prefix=/usr/local/expect/ --with-tcl=/usr/local/tcl/lib/ --with-tclinclude=/opt/tcl8.4.11/generic/ --enable-shared

make && make install

注意:指定的/opt/tcl8.4.11/generic/ 为我们上面解压的tcl目录

> 创建连接符号

ln -s /usr/local/expect/bin/expect /usr/bin/expect

> 查看连接符号

ls -l /usr/bin/expect

lrwxrwxrwx. 1 root root 28 9月   8 11:21 /usr/bin/expect -> /usr/local/expect/bin/expect

这个符号链接将在编写expect脚本文件时用到,例如在expect文件头部会指定用于执行该脚本的shell

#!/usr/bin/expect

> 测试

[root@localhost opt]# expect

expect1.1> exit

[root@localhost opt]#

这样就可以开始运行expect脚本了。

用法

1. [#!/usr/bin/expect]

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

注意:这一行需要在脚本的第一行。

2. [set timeout 30]

基本上认识英文的都知道这是设置超时时间的,现在你只要记住他的计时单位是:秒。timeout -1 为永不超时,默认情况下,timeout是10秒;

3. [spawn ssh -l username 192.168.1.1]

spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。所以不要用 “which spawn“之类的命令去找spawn命令。好比windows里的dir就是一个内部命令,这个命令由shell自带,你无法找到一个dir.com 或 dir.exe 的可执行文件。

它主要的功能是给ssh运行进程加个壳,用来传递交互指令。

spawn后面加上需要执行的shell命令,比如说spawn sudo touch testfile

4. [expect "password:"]

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

5. [send "ispass\r"]

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

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

6. [interact]

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

7. $argv 参数数组

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

其中,$argc为命令行参数的个数,$argv0为脚本名字本身,$argv为命令行参数。[lrange $argv 0 0]表示第1个参数,[lrange $argv 0 4]为第一个到第五个参数。与c语言不一样的地方在于,$argv不包含脚本名字本身。

8. send和send_user

send会将expect脚本中需要的信息发送给spawn启动的那个进程,而send_user只是回显用户发出的信息,类似于shell中的echo而已。

9. 如果你在第一行(#!那行)使用-d (debug参数),可以在运行的时候输出一些很有用的信息

比如你会看见

argv[0] = /usr/bin/expect argv[1] = -d argv[2] = ./launch.exp argv[3] = 1 argv[4] = 2 argv[5] = 3

使用这些也可以完成参数传递

10. exp_continue的用法

expect {

-re "Permission denied, please try again." {

send_user "Error:Permission denied.\n"

exit

}

-re "Are you sure you want to continue connecting (yes/no)?" {

send "yes\r";exp_continue

}

-re "assword:" {

send "$loginpass\r";exp_continue

}

-re "Connection refused" {

exit

}

timeout {

exit

}

eof {

exit

}

}

使用exp_continue后,会重新从当前expect块的开始重新执行,可以简单理解问while循环的continue

例子

下面的脚本例子,实现了:登录一个Linux服务器,执行 df -h 等命令

vi expectExample.sh 后输入下面的脚本内容保存后,使用chmod +x expectExample.sh 命令为脚本赋予可执行权限。

#!/usr/bin/expect -f

#-------------------------------------------- set the variable,you can modify the value

set ipaddr [lrange $argv 0 0]

set port [lrange $argv 1 1]

set loginuser [lrange $argv 2 2]

set loginpass [lrange $argv 3 3]

set cmd_prompt "]#|~]?"

# 超时时间,单位(秒)

set timeout 3600

#-------------------------------------------- login by ssh

spawn ssh -p $port $loginuser@$ipaddr

expect {

-re "Permission denied, please try again." {

send_user "Error:Permission denied.\n"

exit

}

-re "Connection refused" {

send_user "Error:Connection refused.\n"

exit

}

timeout {

exit

}

eof {

exit

}

-re "Are you sure you want to continue connecting (yes/no)?" {

send "yes\r"

exp_continue

}

-re "assword:" {

send "$loginpass\r"

exp_continue

}

-re $cmd_prompt {

send "\r"

}

}

#-------------------------------------------- now,we do some commands

expect {

-re $cmd_prompt {

send "df -h\r"

}

}

expect {

-re $cmd_prompt {

send "free -m\r"

}

}

expect {

-re $cmd_prompt {

send "uptime\r"

}

}

send "cd /opt\r"

send "mkdir shanhy123\r"

send "cd shanhy123\r"

send "echo 123 > 123.txt\r"

send "/opt/xs.sh\r"

send "exit\r"

expect eof

#interact

----------------------------------------------

其中 xs.sh 为我写的一个测试的 shell 脚本,里面故意做了一个sleep 模拟耗时操作,代码如下:

#!/bin/bash

sleep 10

echo abc > /opt/abc.txt

注意:expect脚本必须以interact或expect eof结束,执行自动化任务通常expect eof就够了。

实际环境例子

#!/bin/bash

cat iplist|while read line

do

a=($line)

/usr/bin/expect<

spawn ssh root@${a[0]}

expect {

"*yes/no"{ send "yes\r"; exp_continue}     #首次需要,第二次登录需要去掉。

#exp_continue 附加于某个 expect 判断项之后,可以使该项被匹配后,还能继续匹配该 expect 判断语句内的其他项。

"*password:"{send "${a[1]}\r" }

}

expect "#"

send "userdel -rf username\r"    #-r删除home下的目录,-f强制删除用户

send "exit\r"

expect eof

EOF

done

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值