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.

0

1 Answer 1

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 [.

4
  • 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
    Commented Dec 2, 2018 at 14:11
  • Why are you running it with sudo?
    – Jeff Schaller
    Commented Dec 2, 2018 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
    Commented Dec 2, 2018 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
    Commented Dec 6, 2018 at 23:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .