本例是以 Centos 6.4 为环境
1. 安装 expect 软件
# yum install expect
2. 使用expect 远程执行脚本
例子:
#!/usr/bin/expect -f
foreach ip {
192.168.1.11
192.168.1.14
。。。。。#多个IP地址
} {
spawn ssh $IP
expect "login*"
send "db2inst1\r"
expect "Password:"
send "db2inst1\r"
expect "*>*"
send "cd /home/db2inst1/weihu/activemobile/sorting\r"
expect "*>*"
send "rm 1.txt\r"
expect "*>*"
send "exit\n"
expect eof
}
可以通过SSH方式顺序登陆多台主机执行命令
3. 在 shell 中调用 expect
例子:
#!/bin/bash
expect<<!
spawn ssh $IP
expect "login*"
send "db2inst1\r"
expect "Password:"
send "db2inst1\r"
expect "*>*"
send "cd /home/db2inst1/weihu/activemobile/sorting\r"
expect "*>*"
send "rm 1.txt\r"
expect "*>*"
send "exit\n"
expect eof
!
cd /etc