Expect

Expect、Command、管道和 Gzip

  • June 6, 2018

我正在嘗試使用 expect 命令,一切正常,直到我嘗試將命令的結果通過管道傳輸到 Gzip 文件中。這是 bash 文件的片段:

XYZ=$(expect -c "
spawn python log-connector.py -s $strt -e $end -i 600 -a https://server:9000 -u someaccount -f ./conf/firewall-fields2.txt -q ./conf/query.txt -z | gzip >> /data/sources/results-$strt2-$end2.json.gz

expect \"Password:\"

send \"$pass\r\"

interact

")

echo "$XYZ"

$start- 變數(開始日期)

$end - 變數(結束日期)

$strt2- 變數(開始日期為 00:00)

$end2 - 變數(結束日期為 23:59)

$pass- 使用者之前輸入的密碼。

如果我刪除

| gzip >> /data/sources/results-$strt2-$end2.json.gz

從上面它可以按預期工作。當我添加它時,我收到以下錯誤:

send: spawn id exp7 not open
   while executing

usage: API [-h] [-s START] [-e END] [-d DAY] [-i INTERVAL]
                   [-a LOGGER] [-l LOGFILE] [-f FIELDS] [-q QUERY]
                   [-o OUTPUT] [-t FORMAT] [-u USER] [-p PASS] [-z]
API: error: unrecognized arguments: | gzip >> /data/sources/results-20180604-20180604.json.gz

您可能想使用 shell 來解釋管道和重定向:我將使用 heredoc 使引用更容易

XYZ=$(expect <<END_EXPECT
   set timeout -1
   spawn sh -c {python log-connector.py -s $strt -e $end -i 600 -a https://server:9000 -u someaccount -f ./conf/firewall-fields2.txt -q ./conf/query.txt -z | gzip >> /data/sources/results-$strt2-$end2.json.gz}
   expect "Password:"
   send "$pass\r"
   expect eof
END_EXPECT
)

echo "$XYZ"

引用自:https://unix.stackexchange.com/questions/448053