Bash: Remote SSH ncat command requires sigint to exit

Viewed 197

I have a bit of an interesting situation that I can't figure out.

I need the ability to issue a command remotely over SSH, which then pipes the results into an ncat tunnel back to the original server.

My commands are a little more complex than this (involving innobackupex, a MySQL backup utility), but this minimal example shows the same issue:

ncat -l 9970 &
ssh db-202 "echo 'testing' | ncat $BACKUPSERVER 9970"
... other commands

The issue is that after the echo command completes (or whatever is run), the script just hangs and the later commands don't run. I need to send a ctrl-c (SIGINT) to continue. Obviously this isn't ideal in the context of a bash script, where lots of things may need to happen after this command completes automatically.

Not sure if it's relevant, but sending a sigint doesn't always properly terminate the ncat tunnel on both sides. Sometimes I need to kill -9 it, probably due to the sigint messing something up.

Can anybody explain this behavior and how I can get around it? Or a better way to do what I want?

Thanks!

1 Answers

Finally figured it out.

I added the --recv-only and --send-only arguments to each side of the tunnel. According to the manpage:

--send-only Only send data, ignoring received; quit on EOF

This may explain it, but I though that any EOF would terminate the tunnel. Maybe without these arguments, both sides need to send an EOF?

Anyway, here are the commands that work.

ncat --recv-only -l 9970 &
ssh db-202 "echo 'testing' | ncat --send-only $BACKUPSERVER 9970"

Hopefully this helps someone in the future.

Related