温馨提示×

Ubuntu expect脚本如何读取外部文件

小樊
98
2024-07-17 14:32:52
栏目: 智能运维

要在Ubuntu上使用expect脚本读取外部文件,可以使用以下步骤:

  1. 创建一个包含所需输入的外部文件,例如input.txt。

  2. 编写一个expect脚本,使用spawn命令启动需要输入的程序,并使用expect命令读取外部文件中的输入。

例如,假设我们有一个需要输入用户名和密码的程序,并且我们想使用input.txt文件中的用户名和密码。以下是一个示例expect脚本:

#!/usr/bin/expect

set input_file "input.txt"
set file [open $input_file r]
gets $file username
gets $file password
close $file

spawn ./your_program

expect "Enter username:"
send "$username\r"

expect "Enter password:"
send "$password\r"

interact
  1. 保存并运行上述expect脚本,确保将your_program替换为您要运行的实际程序。

这样,expect脚本将读取外部文件中的用户名和密码,并将其发送到需要输入的程序中。

0