Linux – SSH to server with xargs

linuxscriptingsshxargs

I have a pretty easy problem I'm trying to resolve here. I'm constantly running the (host) command on domains, to get their IPs, then (host) again on those IPs to get their PTRs, then I'm SSHing to that server represented in the PTR:

[root@box ~]$ host DomainIWant.com
DomainIWant.com has address 123.123.123.123

[root@box ~]$ host 123.123.123.123
123.123.123.123.in-addr.arpa domain name pointer vps2010.DomainIWantHosts.com

[root@box ~]$ ssh vps2010.DomainIWantHosts.com

Simple enough right? It's just tedious doing this over and over, so as with everything Linux I want to speed it up by automating it:

[root@box ~]$ host DomainIWant.com | awk '{print $4}' | xargs host | awk '{print $5}' | xargs ssh -tt

The problem that I'm having is when I call ssh via xargs, I do get SSH'd into the remote server but with the error [tcgetattr: Invalid argument]. I'm sitting at the remote server's shell but when I try to run any command it just hangs and eventually I have to Ctrl-C to break out of it.

If I don't tack on the (ssh -tt) then I get a [Pseudo-terminal will not be allocated because stdin is not a terminal] error. This seems like such a simply problem so I'm hoping someone more familiar with ssh via xargs can let me know if it's even possible.

Best Answer

If you're using a shell that supports backticks- or $()-style command substitution (most shells do), then in your case, you can avoid using xargs entirely, like this:

ssh $(host -t PTR $(host -t A DomainIWant.com | awk '{print $4}') | awk '{print $5}')

(I added the -t flags to the host commands, to ensure they only emit 1 line of output.)