183k views
0 votes
Write an If/else statement to check whether host is online. If it is online, then print system is online otherwise print system is unreachable.

User Gnomet
by
6.6k points

1 Answer

3 votes

Answer:

from socket import *

hostname = input('Enter the host to be scanned: ')

ip_add = gethostbyname(hostname)

connections = [ ]

for i in range(133, 136):

s = socket(AF_INET, SOCK_STREAM)

conn = s.connect_ex((ip_add, i))

print(conn)

connections.append(conn)

if 0 in connections:

print ('Host is online')

s.close()

else:

print ('system is unreachable')

Step-by-step explanation:

The python source code above scans for all the available range of ports in the provided hostname, if any port is available, the host is online else the program print the error message "system is unreachable.

User Karel Lenc
by
5.6k points