0

I am using the nc command to upload a file to a device, this is how I am using it:

nc -i1 "device ip" "port" < /path/to/file

this command works fine on Scientific Linux 6.4 32-bit, but after upgrading to CentOS 7.4 64-bit, the command returns

Ncat: Idle timeout expired (1000 ms)

I realized there is a bugzilla item on RedHat website here, they are saying that nc is being replaced by ncat and it did not work. I even tried cat the file then pipe it to nc or ncat and it did not work. Any tips on resolving this issue?

  • Have you tried increasing the time out? – Raman Sailopal Feb 14 '18 at 13:11
  • Yes, 10 seconds and it did not work. – 3bdalla Feb 14 '18 at 13:12
  • Try using the verbosity flags. – Raman Sailopal Feb 14 '18 at 13:18
  • It just shows the version and connected, then the same error message – 3bdalla Feb 14 '18 at 13:23
  • If you are just worried about the connection getting stuck, you could prefix the command with timeout 99 to get the process killed after 99 seconds. Or you could try the socat command which has a possible working -T option for idle timeout: socat -T 1 - TCP:ipaddress:port <file – meuh Feb 14 '18 at 13:54
2

The problem most surely is the -i option. At least three different versions of nc do exist. openbsd-netcat, gnu-netcat and nmap-ncat.

You probably switched from one of the nc versions where -i option means buffer interval to the nmap one where -i means timeout. So either drop the -i option or replace it by -d.

Below I've put together manual lines for the -i option of each nc version mentioned. I also included -d for the nmap version.

OpenBSD:

-i interval        Specifies a delay time interval between lines of text sent and received. Also causes a delay time between connections to multiple ports.

GNU:

-i SECS
--interval SECS    sets the buffering output delay time. This affects all the current modes and makes the connection sock to buffer outgoing data. This means that in tunnel mode everything received from the listening socket is buffered for the connect socket.

NMAP:

-i, --idle-timeout <time>  Idle read/write timeout

-d time, --delay time (Specify line delay)
       Set the delay interval for lines sent. This effectively limits
       the number of lines that Ncat will send in the specified period.
       This may be useful for low-bandwidth sites, or have other uses
       such as coping with annoying iptables --limit options.
1

This is a design flaw in the nmap version of ncat that is used in RHEL7. I am looking at the source code of version 7.70 (RHEL7 is 7.50, Fedora 28 is 7.60 and I don't think this aspect of the code will have changed much between that and 7.70).

In ncat_connect.c I can see code to:

  • exit after EOF on stdin in --send-only mode,
  • exit after EOF on the socket in --recv-only mode,

but, if in neither --send-only nor --recv-only mode, there is nothing in ncat_connect.c to handle exit when we've seen EOF on both stdin and the socket.

So we look at the lower-level code in nsock_core.c's nsock_loop function. This loop will quit if ms->quit it set (by the above-mentioned --send-only and --recv-only code) or if there are no events_pending.

But here's the bug: the timeout counts in events_pending. So nsock_loop goes "OK, I know stdin has been EOF'd and the socket has been EOF'd, but I'm still not going to quit yet, because there's still an event pending" (the timeout), so it just sits there until the timeout fires and then nc error-exits, even if everything has actually worked just fine.

So I think we should submit a bug report directly to nmap (not to your distro: they are likely to say "that's upstream behaviour we are not qualified to evaluate", whereas upstream maintainers will be able to evaluate it): we point out that the above design effectively means -i is useless unless either (1) used along with --send-only or --recv-only or (2) used in a situation where nc's error code is ignored.

Meanwhile, one of the following workarounds might help:

  • Use --send-only or --recv-only (if appropriate to your use-case),
  • Wrap the nc command in a timeout command instead of using -i,
  • Manually install a different version of nc, such as GNU Netcat or the original nc-1.10—but to do this on RHEL7 or Fedora 28 you will need to install manually, as packages (currently) provide only nmap-ncat.
0

I have started a centos7 container and could reproduce your error:

[root@34630a8f0b7c /]# nc 192.168.1.210 1234 < sarassa 
[root@34630a8f0b7c /]# nc -i 1 192.168.1.210 1234 < sarassa 
Ncat: Idle timeout expired (1000 ms).

The -i option only adds a timeout at the end of the file transfer, so it is not clear what you want to achieve with it

  • After successful nc, I should be able to view the file contents directly from the device. This only works with 32-bit systems. – 3bdalla Mar 6 '18 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.