Advertisement
Guest User

Ping IP addresses array

a guest
Oct 21st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4.  
  5. # DÉPENDANCES:
  6. # pip install termcolor
  7. # pip install nmap
  8. # pip install pyroute2
  9.  
  10. from termcolor import colored
  11. from pyroute2 import IPRoute
  12. import nmap
  13. import subprocess
  14.  
  15. nm = nmap.PortScanner()
  16.  
  17. def changeIp(interface, ipadd):
  18.     ip = IPRoute()
  19.     print colored('Configuration IP ' + ipadd + ' sur interface ' + interface, 'blue')
  20.     index = ip.link_lookup(ifname=interface)[0]
  21.     ip.addr('add', index, address=ipadd, mask=24)
  22.     ip.close()
  23.  
  24. def scanSubnet(subnetToScan):
  25.   print colored('scanning subnet' + subnetToScan, 'blue')
  26.   nm.scan(hosts=subnetToScan, arguments='-n -sP -PE -PA21,23,80,3389')
  27.   hosts_list = [(x, nm[x]['status']['state'], nm[x]['status']['state']) for x in nm.all_hosts()]
  28.   for host in hosts_list:
  29.     print colored("✔️ " + str(host[0]), 'green')
  30.  
  31. def pingIpList(iplist):
  32.   print colored('ping ips', 'blue')
  33.   for ip in iplist:
  34.     address = ip
  35.     res = subprocess.call(['ping', '-c', '3', '-W', '1', address],stdout=subprocess.PIPE)
  36.     if res == 0:
  37.         print colored("✔️ " + address, 'green')
  38.     elif res == 2:
  39.         print colored("❌  " + address + " aucune réponse", 'yellow')
  40.     else:
  41.         print colored("❌  " + address, 'red')
  42.  
  43.  
  44. changeIp('enp9s0', '172.16.30.1')
  45. pingIpList(["8.8.8.8", "172.16.30.1"])
  46. scanSubnet('172.16.0.0/24')
  47. changeIp('enp9s0', '10.51.1.250')
  48. scanSubnet('10.51.1.0/24')
  49. changeIp('enp9s0', '10.200.100.250')
  50. scanSubnet('10.200.100.0/24')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement