0

I am trying to see if a port is opened for a list of hostnames/IP's, and I used a simple script like this one to ssh to each host and capture the output.

for i in `cat $1`
do ssh $i "sleep 5 | telnet 11.22.33.44 123"
done

This is throwing a "No environment-specific terminal time" error because Telnet is designed to be an interactive tool to interact with a smart terminal.

How can I go around this, knowing that I cannot use nc command as it's not available on Unix AIX systems?

Please note that some hosts will reply with Connected to 11.22.33.44. Escape character is ^] and that means there is connectivity while others will freeze at Trying ... meaning that there is no connection.

  • if I'm understanding your question correctly, you need to SSH to a list of hosts and then check their connectivity to host 11.22.33.44 on port 123; I've updated the title to reflect that. Please further update if I've gotten it wrong. – Jeff Schaller Nov 30 '18 at 11:58
  • @JeffSchaller Yes, that is correct – Stan K Nov 30 '18 at 12:00
1

Since perl is installed by default, one way would be to ssh to the intermediate host and ask it to run perl, with the perl program provided as a here-document:

for host in $(< "$1")
do

  ssh "$host" perl << 'EOF'
use IO::Socket;
my $handle = IO::Socket::INET->new(
        PeerAddr => '11.22.33.44',
        PeerPort => 123,
        Proto    => 'tcp',
        Timeout  => 5)
    or die "Failed\n";
close $handle;
print "Success\n";
EOF

done

The script attempts to connect to the given IP and port over TCP; if it fails, you'll get the Failed message, but if it succeeds, it immediately closes the connection and prints Success. This uses the $(< ...) syntax to read from the filename given by the first parameter; just like cat, this scheme assumes that none of the filenames have any characters from $IFS in them (spaces, tabs, or newlines by default). I've also quoted the loop variable (here, host), to prevent problems if any of the input file contents contain shell globbing characters like *, ?, or [.

  • Getting telnetTest[19] :/tmp/sh78123465.13: cannot create when running with my user and asking for password when running with sudo rights e.g. sudo ksh telnetTest hostList – Stan K Dec 2 '18 at 14:11
  • Why are you running it with sudo? – Jeff Schaller Dec 2 '18 at 14:13
  • To see what happens when it can write to /tmp because my user wasn't allowed. Using my nonroot user account to run this script should allow ssh access to the hostList through already stored keys on both machines – Stan K Dec 2 '18 at 14:23
  • Long story short, this was exactly what I was looking for. I have solved the permission issue by, well, adding permission. Still don't understand why this scrip tries to write on /tmp but at least it's working – Stan K Dec 6 '18 at 23:12

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.