Search code examples
macosbashterminalcommandmac-address

How to run a command to use data from a txt file to execute them one by one smartly


I will explain it precisely!

  1. So, i have 8 friends using same internet connection which i do and mostly we play games online.
  2. Connection is such that when you switch on your connection it displays a login page of ISP which allow you go online only after you login.
  3. We have each others mac address.Now to check either any of my friend is online or not i execute the following command:

    sudo ifconfig en0 ether xx:xx:xx:xx:xx:xx; sudo ifconfig en0 down; sudo ifconfig en0 ether up; sleep 6; ping -c 3 www.google.co.in;

  4. Where xx:xx:xx:xx:xx:xx=MAC ADDRESS, en0=ethernet

  5. I use sleep as it takes about 6 seconds to connect router

  6. So ping shows me result whether they are online or not

  7. Now i have 8 such mac addresses in a txt file organised as each in one line.

Q1) Can i use only one command such that it takes each mac address from text file one by one and uses it in above command and ends as my txt ends?

Q2) Is there any Alternative command for above purpose as the above command is organised by myself to work properly as per my use?

I work with Mac OS


Solution

  • Here's the final quote

    #!/bin/bash
    while read m; do
       echo Checking MAC address: $m
       sudo ifconfig en0 ether $m
       sudo ifconfig en0 down
       sudo ifconfig en0 up
       sleep 5
       i=1
      while [ $i -eq 1 ]
        do
          ping -c 3 www.google.co.in > /dev/null
          e=$?
             if [ $e -eq 0 ]
              then
                echo ONLINE
                i=0
                echo $m >> ONLINE.txt
             elif [ $e -eq 2 ]
              then
               echo OFFLINE
               i=0
               echo $m >> OFFLINE.txt
             else
               echo Waiting To be Connected
               i=1
               sleep 4
             fi
        done
    done < macs.txt
    echo HI, These macs are ONLINE
    cat ONLINE.txt
    echo Sorry these are OFFLINE
    cat OFFLINE.txt
    rm ONLINE.txt OFFLINE.txt
    

    Thanks for helping guys!