本文介绍了tcpreplay后读取失败,并出现错误:0:资源暂时不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的脚本可以运行.它调用tcpreplay,然后要求用户输入一些内容.然后读取将失败,并显示以下内容:读取错误:0:资源暂时不可用.

I have a very simple script to run. It calls tcpreplay and then ask the user to type in something. Then the read will fail with read: read error: 0: Resource temporarily unavailable.

这是代码

#!/bin/bash

tcpreplay -ieth4 SMTP.pcap

echo TEST
read HANDLE
echo $HANDLE

输出为

[root@vse1 quick_test]# ./test.sh
sending out eth4
processing file: SMTP.pcap
Actual: 28 packets (4380 bytes) sent in 0.53 seconds.       Rated: 8264.2 bps, 0.06 Mbps, 52.83 pps
Statistics for network device: eth4
        Attempted packets:         28
        Successful packets:        28
        Failed packets:            0
        Retried packets (ENOBUFS): 0
        Retried packets (EAGAIN):  0
TEST
./test.sh: line 6: read: read error: 0: Resource temporarily unavailable

[root@vse1 quick_test]#

我想知道在运行tcpreplay之后是否需要关闭或清理任何句柄或管道?

I am wondering if I need to close or clear up any handles or pipes after I run tcpreplay?

推荐答案

显然,tcpreplay会在stdin上设置O_NONBLOCK,然后再将其删除.我会说这是tcpreplay中的错误.要解决此问题,您可以使用从/dev/null重定向的stdin运行tcpreplay.像这样:

Apparently tcpreplay sets O_NONBLOCK on stdin and then doesn't remove it. I'd say it's a bug in tcpreplay. To work it around you can run tcpreplay with stdin redirected from /dev/null. Like this:

tcpreplay -i eth4 SMTP.pcap </dev/null

附加:请注意,此tcpreplay行为仅破坏非交互式外壳.

Addition: note that this tcpreplay behavior breaks non-interactive shells only.

另一个补充:或者,如果您确实需要tcpreplay来接收您的输入时,您可以编写一个简短的程序来重置O_NONBLOCK.像这个(reset-nonblock.c):

Another addition: alternatively, if you really need tcpreplay to receive yourinput you can write a short program which resets O_NONBLOCK. Like this one(reset-nonblock.c):

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int
main()
{
    if (fcntl(STDIN_FILENO, F_SETFL,
              fcntl(STDIN_FILENO, F_GETFL) & ~O_NONBLOCK) < 0) {
        perror(NULL);
        return 1;
    }
    return 0;
}

使用"make reset-nonblock"进行设置,然后将其放入您的PATH中并按以下方式使用:

Make it with "make reset-nonblock", then put it in your PATH and use like this:

tcpreplay -i eth4 SMTP.pcap
reset-nonblock

这篇关于tcpreplay后读取失败,并出现错误:0:资源暂时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:52