首页 > 怎么自动填写有交互的shell脚本

怎么自动填写有交互的shell脚本

在写一个shell脚本A,其中调用了另一个脚本,例如test.sh
但是test.sh中有交互的内容
需要用户依次输入 a 回车 b 回车 回车 回车
我想自动的实现它,请问应该在A中怎么写才会自动填入这些内容,前提是tesh.sh执行的过程还要让用户看到,不能把它重定向


expect就可以实现了


用管道输入,然后echo出来,给用户看
A.sh

echo -e a\nb\n\n\n | bash test.sh
echo a
echo b
echo
echo
echo

如果管道输入满足不了你的需求,就用expect,这个比较复杂了。。。


可以google here document~


用pexpect,以下代码供参考:

#!/usr/bin/python

import sys 
import pexpect

password = 'password'
expect_list = ['(yes/no)', 'password:']

p = pexpect.spawn('ssh username@localhost ls')
try:
    while True:
        idx = p.expect(expect_list)
        print p.before + expect_list[idx],
        if idx == 0:
            print "yes"
            p.sendline('yes')
        elif idx == 1:
            print password
            p.sendline(password)
except pexpect.TIMEOUT:
    print >>sys.stderr, 'timeout'
except pexpect.EOF:
    print p.before
    print >>sys.stderr, '<the end>'

输出:

username@localhost's password: password

Permission denied, please try again.
username@localhost's password: password

Permission denied, please try again.
username@localhost's password: password

Permission denied (publickey,gssapi-with-mic,password).

<the end>

为了能够快速解决眼前的问题,我查询了一些expect脚本相关的资料,它可以用来处理交互式的命令,因此可以用来实现自动登录和执行命令,并将执行结果打印到log文件中。
下面是我在生产环境中得到成功应用的一个expect脚本,为了让它更具有复用性,我将需要管理的主机和命令都写到了配置文件中,通过脚本读取的方式来执行。

整个脚本的构成如下:

其中,
config目录下存放的是commands.txt批处理命令与hosts.txt服务器配置列表;
log目录下存放的是运行的日志信息;
ssh-key目录下存放的是ssh私钥文件,权限必须为600;
expect-run.exp是expect脚本文件,需要可执行权限;
main-shell.sh是主执行程序,需要可执行权限,通过./main-shell.sh执行,用于从hosts文件循环取值并调用expect脚本。

整个脚本内容可通过这里下载。

下面,我将配置文件与脚本的相关内容展示给大家:
main-shell.sh

#!/bin/bash

for i in `cat config/hosts.txt`
do
	export server=`echo $i | awk -F "|" '{print $1}'`
	export port=`echo $i | awk -F "|" '{print $2}'`
	export user=`echo $i | awk -F "|" '{print $3}'`
	export passwd=`echo $i | awk -F "|" '{print $4}'`
	export rootpasswd=`echo $i | awk -F "|" '{print $5}'`

	export cmdfile="config/commands.txt"

	./expect-run.exp $server $port $user $passwd $rootpasswd $cmdfile
done

expect-run.exp

#!/usr/bin/expect -f
# heylinux.com

# Check
if { $argc<6 } {
        send_user "usage: $argv0 <server> <port> <user> <passwd> <rootpasswd> <cmdfile> \n"
        exit
}

# TIMEOUT
set timeout 20

# Login parameters
set server [lindex $argv 0]
set port [lindex $argv 1]
set user [lindex $argv 2]
set passwd [lindex $argv 3]
set rootpasswd [lindex $argv 4]
set cmdfile [ open [lindex $argv 5] ]

# Logfile
log_file log/run.log

# Login Server
spawn ssh -p $port $user@$server

## Enable this and Disable the "spawn ssh ..." above if you are using ssh-key.
#spawn ssh -i ssh-key/Identity.ppk -p $port $user@$server

expect {
	"yes/no)?\ " {send "yes\r";exp_continue}

	"*assword:\ " {send "$passwd\r"}

## Disable the "*assword:\ ..." above if you are using ssh-key, and Enable this if your ssh-key has passphrase.
#	"Identity.ppk':\ " {send "$passwd\r"}
}

# Login as Root
expect "*]$\ " {send "su - root\r"}
expect "*assword:\ " {send "$rootpasswd\r"}

# Run Commands
expect "*]#\ " {
	while {[gets $cmdfile cmd] >= 0} {
		send "$cmd\r"
		}
}

# Exit Root
expect "*]#\ " {send "exit\r"}

# Exit User
expect "*]$\ " {send "exit\r"}

# Close File
close $cmdfile

# Exit Expect
expect eof

hosts.txt

192.168.10.200|22|username|userpasswd|rootpasswd
444.333.222.111|22|username|userpasswd|rootpasswd
login.server.com|7033|username|userpasswd|rootpasswd

commands.txt

cd /opt
ls -l
sleep 5
tail -n 20 /var/log/message

最后,注意如果是通过ssh密钥来验证的话,需要修改expect-run.exp脚本中的Login部分(第27,30,35,38行);
而如果在脚本中还需要执行更多的交互式内容,如批量更新用户密码等,建议直接修改expect-run.exp脚本,在脚本中直接进行,总之这个脚本给我们的管理提供了一个模板,它并不适用于所有情况,但略加修改后是可以的。

【热门文章】
【热门文章】