October 28, 2019 · Basic Pen-Testing

1.3 : Basic Bash scripting - ping sweeper (Part IV)

  1. Basic ping scanning for port 10.0.0.0/24

    #!/bin/bashA
    scan()
    {
        output=$(nmap $1)
        if echo $output | grep -q "Host is up"; then
            echo $1 >> ping-sweep.txt
        fi
    }
    
    for ID in `seq 1 10`
    do
        scan 10.0.0.$ID &
    done
    
  2. In python

    # import os
    import subprocess
    
    def scan(ip):
        # res = os.system('ping -c 1 %s' % ip)
        # print(res)
        proc = subprocess.Popen("ping -W 500 -c 1 %s" % ip, shell=True, stdout=subprocess.PIPE)
        (out, err) = proc.communicate()
        # print(out.decode())
        if " 0.0% packet loss" in out.decode() :
            print(ip)
    
    def main():
        # scan("10.0.0.1")
        for ip in range(1,10):
            scan ("10.0.0.%s" %ip)
    
    if __name__ == "__main__":
        main()
    

> : redirect output to file
| : pass to next command as input arguments