Nov-27-2019, 09:16 AM
Dear All,
I have tried to write a code which fetches the Host name and the IP Address for a user. The program is working nice as you can see below in the code,
However, when I try to use FOR loop to pass the parameter for more than one user through a text file which name is computer.txt is not showing as expected and drop the exception message instead Please see the code below,
Suppose the computer.txt is containing two names which are ABC & XYZ
Many thanks,
I have tried to write a code which fetches the Host name and the IP Address for a user. The program is working nice as you can see below in the code,
import socket def get_Host_name_IP(): remote_host =input("Please put the pc name: ") try: host_ip = socket.gethostbyname(remote_host) print ("The host name is: %s" %remote_host) print("IP address is %s : " %host_ip) except: print("Unable to get hostname or IP") get_Host_name_IP()
However, when I try to use FOR loop to pass the parameter for more than one user through a text file which name is computer.txt is not showing as expected and drop the exception message instead Please see the code below,
Suppose the computer.txt is containing two names which are ABC & XYZ
import socket def get_Host_name_IP(): OsPcs = open("computers.txt", "r") Osdata = OsPcs.readlines() for i in Osdata: ##Here is printing the hostname without error print(i) try: ### the following line is not execute print("And IP address is %s : " %socket.gethostbyname(i)) except: print("Not done") get_Host_name_IP()The output is:
ABC Not done XYZ Not done Process finished with exit code 0Any ideas?
Many thanks,