title: Bash while loop script that runs Nmap with iterating IP and port number
body: <p>Summary: I'm trying to create a Bash script that selects an IP address and it's associated open ports, then run each IP through Nmap and display/store the results.</p>
  <p>The format of the ports file (portsList):</p>
  <pre><code>192.168.1.1   53   udp
  192.168.1.1   80   tcp
  192.168.1.1   1900 tcp
  192.168.1.110 135  tcp
  192.168.1.115 1080 tcp
  </code></pre>
  <p>The format of the IP file (ipList):</p>
  <pre><code>192.168.1.1
  192.168.1.110
  192.168.1.115
  </code></pre>
  <p>My Bash script:</p>
  <pre><code>#!/bin/bash
  portsvar=&quot;$(cat formattedPorts)&quot;
  hostvar=&quot;$(cat oneHost)&quot;
  
  while read -r line;
  do
  echo &quot;$line&quot; &gt; oneHost
  grep -wf oneHost portsList | awk '{print $2}' | tr '\n' ',' &gt; formattedPorts
  nmap -n -sV -p&quot;$portsvar&quot; &quot;$hostvar&quot; &gt;&gt; scanResults
  cat scanResults
  done &lt; ipList
  </code></pre>
  <p>I spent several hours, trying multiple variations of this code and tried to find a solution online. But to no avail. The most this script can do is scan and output for the first selected IP and ports.</p>
  <p>Additional considerations would be; if a port is UDP, to append 'U:' on the ports option. Also, if there is a way to display the output of Nmap scanning, while it is being redirected '&gt;&gt;' to a file.</p>
  <p>Bash is what I'm most familiar with, but I'm open to learning a solution in a different language.</p>
  <p><em>Edit</em>
  I tried jhnc's solution below by replacing:</p>
  <pre><code>echo &quot;$line&quot; &gt; oneHost
  grep -wf oneHost portsList | awk '{print $2}' | tr '\n' ',' &gt; formattedPorts
  nmap -n -sV -p&quot;$portsvar&quot; &quot;$hostvar&quot; &gt;&gt; scanResults
  cat scanResults
  </code></pre>
  <p>with jhnc's code in the comment below. the resulting output was:</p>
  <pre><code>nmap -n -sV -p T:135 192.168.1.110
  nmap -n -sV -p T:5985 192.168.1.112
  nmap -n -sV -p T:54112,T:60000,T:8009,T:8888 192.168.1.131
  nmap -n -sV -p T:5040 192.168.1.132
  nmap -n -sV -p T:1041,T:1900,T:20005,T:33344,T:49152,T:49153,T:80 192.168.1.1
  </code></pre>
  <p>This output was echo'ed onto the terminal 5 times. From this I'll try to find a solution to have the input of this group of IP's ran once.</p>