0

I'm trying to combine two lists in the output of a bash script. One list is a list of machine names. The other list is a list of IP addresses. It then outputs whether or not it can telnet to the IP on port 3389. It's taking an inventory of which windows machines are responding on that port.

Here's what I came up with:

#!/bin/bash
file1="machine_list.txt"
file2="ip_list.txt"
HOSTS=$(cat "$file1")
IP=$(cat "$file2")

for h in $HOSTS
 do
for i in $IP
do
        echo "checking RDP port on host: $h"
        /usr/bin/telnet $i 3389
        echo
        sleep 2
done
done

The output of a successful connection should look like this:

checking RDP port on host: USAMZADP3000
Trying 10.1.233.65...
Connected to 10.1.233.65.
Escape character is '^]'

But what it's really doing is looping over the same machine name and giving a different IP address for the same machine name. I want the machine name and the IP address to be accurate and have a 1 to 1 correlation.

What am I doing wrong? How can I get the result I want?

bluethundr
  • 1,005
  • 17
  • 68
  • 141
  • Why are machine names and IP addresses in two different files? How do you correlate them? Also, are you running this on Windows? – codeforester Jan 13 '17 at 22:06
  • The machine names and IP addresses are in different files because I thought it would be easier to write the script this way. If you think it's easier to keep both in the same file, please let me know. And I'm running the script on a Centos72 (linux) server running on vagrant. But I'm on the same network as the windows machines and I have to test if they are accessible. – bluethundr Jan 16 '17 at 16:18

1 Answers1

2

It looks like you want to read through the two files in parallel. Since your loops are nested, your code is taking one machine name, then reading through the whole IP file, then taking the second machine name and reading through the whole IP file, etc. You can read both files simultaneously using one while loop.

while read machineHostName <&3 && read machineIPAddr <&4; do     
    echo "checking RDP port on host: $machineHostName"
    /usr/bin/telnet $machineIPAddr 3389
    echo
    sleep 2
done 3<$file1 4<$file2

You might find this related question helpful.

Community
  • 1
  • 1
A. Sokol
  • 336
  • 2
  • 13