Skip to content

Instantly share code, notes, and snippets.

@anmolj7
Created December 7, 2019 18:34
Show Gist options
  • Save anmolj7/bc6926520b7095ab24f8535999411b92 to your computer and use it in GitHub Desktop.
Save anmolj7/bc6926520b7095ab24f8535999411b92 to your computer and use it in GitHub Desktop.
import subprocess as sp
import socket
def clrscr():
sp.call("clear", shell=True)
def get_ip():
output = sp.check_output(["ip route | grep default"], shell=True) # Gets the gateway.
output = output.decode("utf8")
gateway_ip = None
for i in output.split(' '):
if len(i.split('.')) == 4:
gateway_ip = i
return gateway_ip
return None
def breakline():
print("-"*60)
def main():
ip = get_ip()
temp = '.'.join(ip.split('.')[:-1])
Add = []
clrscr()
x = ip.split('.')[-1]
x = int(x)
for ping in range(0, 256):
address = f'{temp}.{ping}'
print(f'Scanning on: {address}')
res = sp.call(["ping", address, '-w', '1'])
if res == 0:
Add.append(address)
clrscr()
clrscr()
breakline()
print("Scan Complete.")
breakline()
print(f'The number of devices found on the network are: {len(Add)}')
breakline()
for i in range(len(Add)):
print(f"{i+1} device's ip: {Add[i]}'")
breakline()
print("Also, one of the devices is the current and one is the host.")
breakline()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment