title: How do I make these multiple Nmap commands more elegant in bash script?
body: <p>I have a small project script I have been working on that scans my network for new MAC Addresses, and notifies me if there is an unfamiliar device on my network.</p>
  <p>I've introduced the ability to automatically perform an OS scan on the unknown MAC address if there is one. However, I can only get this to work by saving the grepped portion of my ping scan into a temporary file (tmpmac.txt), and then grepping the IP address out of that file into an additional temporary file (tmpip.txt), as the -O Nmap option requires an IP to scan:</p>
  <pre><code>        nmap -sn 192.168.0.0/24 | grep -B 2 &quot;$address&quot; &gt; /home/pi/tmpmac.txt
          cat /home/pi/tmpmac.txt | grep &quot;Nmap&quot; | awk {'print $5'} &gt; /home/pi/tmpip.txt
          unknown_mac=$( cat /home/pi/tmpip.txt )
          nmap -O $unknown_mac | tee -a /home/pi/mac_scan.log | mutt -s &quot;OS Scan for $address&quot; XXXXXXXX@gmail.com
  </code></pre>
  <p>Entire Script:</p>
  <pre><code>echo &quot;&lt;&lt;---AUTOMATIC SCAN---&gt;&gt;&quot; &gt;&gt;/home/pi/mac_scan.log
  date &gt;&gt;/home/pi/mac_scan.log
  nmap -sn 192.168.0.0/24 | grep &quot;MAC&quot; | awk '{print $3}'| sort &gt; /home/pi/arp.txt
  
  readarray -t mac &lt;/home/pi/arp.txt
  
  foundall=true
  
  for address in &quot;${mac[@]}&quot;; do
      if ! grep -Fxq &quot;$address&quot; /home/pi/arp_table.txt;
      then
          echo &quot;WARNING: $address is an unknown device on the network&quot; &gt;&gt; /home/pi/mac_scan.log
          nmap -sn 192.168.0.0/24 | grep -B 2 &quot;$address&quot; | tee -a /home/pi/mac_scan.log | mutt -s &quot;Unknown Devices on Network&quot; XXXXXXX@yahoo.com
          echo &quot;&quot;
          echo &quot;MAC SEARCH RESULTS: $address&quot; &gt;&gt; /home/pi/mac_scan.log
          nmap -sn 192.168.0.0/24 | grep -B 2 &quot;$address&quot; &gt; /home/pi/tmpmac.txt
          cat /home/pi/tmpmac.txt | grep &quot;Nmap&quot; | awk {'print $5'} &gt; /home/pi/tmpip.txt
          unknown_mac=$( cat /home/pi/tmpip.txt )
          nmap -O $unknown_mac | tee -a /home/pi/mac_scan.log | mutt -s &quot;OS Scan for $address&quot; XXXXXXXX@yahoo.com
          foundall=false
      fi
  done
  
  [[ &quot;${foundall}&quot; == 'true' ]] &amp;&amp; echo &quot;All Devices are familiar on your network&quot; | tee -a /home/pi/mac_scan.log
  </code></pre>
  <p>Script in current form performs how it should, but I was curious if there was a more elegant way to accomplish this.</p>