0

这里是 Linux 新手,所以请多多包涵。

我正在为学校编写一个 Bash 脚本(在 CentOS 8 VM 上),并且我正在尝试将 Siege(负载测试器)的输出保存到一个变量中,以便我可以比较值。

这是我遇到的问题:“服务器现在处于围攻状态”和“解除服务器围攻...”之间的 HTTP 行是存储在变量中的内容,而不是“解除服务器围困”之后的漂亮小摘要服务器围攻……”

[root@prodserver siege-4.1.1]# siege -c 1 -t 1s 192.168.1.3
** SIEGE 4.1.1
** Preparing 1 concurrent users for battle.
The server is now under siege...
HTTP/1.1 200     0.00 secs:    6707 bytes ==> GET  /
HTTP/1.1 200     0.01 secs:    2008 bytes ==> GET  /assets/images/taste_bug.gif
HTTP/1.1 200     0.00 secs:    2579 bytes ==> GET  /assets/images/backpack_bug.gif
HTTP/1.1 200     0.00 secs:    2279 bytes ==> GET  /assets/images/desert_bug.gif
HTTP/1.1 200     0.00 secs:    1653 bytes ==> GET  /assets/images/calm_bug.gif
HTTP/1.1 200     0.00 secs:    1251 bytes ==> GET  /assets/javascripts/menus.js
...Shortened for readability...
HTTP/1.1 200     0.00 secs:    1251 bytes ==> GET  /assets/javascripts/menus.js
HTTP/1.1 200     0.00 secs:    2579 bytes ==> GET  /assets/images/backpack_bug.gif
HTTP/1.1 200     0.00 secs:    2279 bytes ==> GET  /assets/images/desert_bug.gif
HTTP/1.1 200     0.00 secs:    1653 bytes ==> GET  /assets/images/calm_bug.gif
HTTP/1.1 200     0.00 secs:    1251 bytes ==> GET  /assets/javascripts/menus.js
HTTP/1.1 200     0.01 secs:    2008 bytes ==> GET  /assets/images/taste_bug.gif
HTTP/1.1 200     0.00 secs:    2579 bytes ==> GET  /assets/images/backpack_bug.gif
HTTP/1.1 200     0.00 secs:    2279 bytes ==> GET  /assets/images/desert_bug.gif
HTTP/1.1 200     0.00 secs:    1653 bytes ==> GET  /assets/images/calm_bug.gif

Lifting the server siege...
Transactions:                149 hits
Availability:             100.00 %
Elapsed time:               0.22 secs
Data transferred:           3.95 MB
Response time:              0.00 secs
Transaction rate:         677.27 trans/sec
Throughput:            17.97 MB/sec
Concurrency:                1.00
Successful transactions:         149
Failed transactions:               0
Longest transaction:            0.01
Shortest transaction:           0.00

目前,这就是我尝试将变量存储在 bash 中的方式:

SIEGE="$(siege -c $1 -t $2 [ip])"

如前所述,当我回显 $SIEGE 时,事实证明该变量存储了所有 HTTP 行,而不是“解除围城...”之后的摘要

我的问题是如何将该摘要存储在变量中。

4

1 回答 1

1

注意:我不熟悉,siege所以我不知道是否所有输出都将变为stdout或某些输出是否会变为stderr.

假设所有siege输出都将stdout......取决于需要忽略哪些行的几个想法:

# grab lines from `^Lifting` to end of output:

SIEGE="$(siege -c $1 -t $2 [ip] | sed -n '/^Lifting/,$ p')"

# ignore all lines starting with `^HTTP`

SIEGE="$(siege -c $1 -t $2 [ip] | grep -v '^HTTP')"

如果结果是某些输出被发送到stderr,请将siege调用更改为重定向stderrstdout

# from

siege -c $1 -t $2 [ip]

# to

siege -c $1 -t $2 [ip] 2>&1

虽然我可能会选择将所有输出保存到文件中,然后根据需要解析文件,ymmv ...

于 2021-09-25T22:50:24.203 回答