diff --git a/Python/IP-Hostname_Validator/README.md b/Python/IP-Hostname_Validator/README.md new file mode 100644 index 000000000..a87d0cf9f --- /dev/null +++ b/Python/IP-Hostname_Validator/README.md @@ -0,0 +1,39 @@ +# IP/Hostname Validator +### Modules used: +- ipaddress - Provides functions to create, manipulate and operate in IPv4 and IPv6 addresses and networks. +- sys - Used to provide access to variables maintained by the interpreter. +- socket - Provides functions and objects to build network applications for clients and servers. It helps access the BSD Socket interface. + +### Description +The script can be used to find the: +- IP Address from hostname +- Hostname from an IP Address + +It mainly validates a given IP Address and Hostname. The IP Addresses are accepted in IPv4 and IPv6 formats. + +### Setup instructions +Navigate to Rotten-Scripts/Python/IP-Hostname_Validator. + +To validate a given IP Address and get the hostname. + +` +python main.py ip 8.8.8.8 +` + +Output: + +![image](https://user-images.githubusercontent.com/76458668/162009872-de378e0d-dc39-4894-bc6d-d9c98e538964.png) + + +To find the IP Address of a given hostname. + +` +python main.py hostname facebook.com +` + +Output: + +![image](https://user-images.githubusercontent.com/76458668/162010700-d61a24b9-46bb-44c8-8e24-77f0e2c2baa9.png) + +### Author +[Yashesvinee](https://github.com/Yashesvinee) diff --git a/Python/IP-Hostname_Validator/main.py b/Python/IP-Hostname_Validator/main.py new file mode 100644 index 000000000..5e7b49e7c --- /dev/null +++ b/Python/IP-Hostname_Validator/main.py @@ -0,0 +1,27 @@ +import ipaddress +import socket +import sys + +if sys.argv[1]=="ip": + ip = sys.argv[2] + try: + print(ipaddress.ip_address(ip)) + print('IP Valid') + host = socket.gethostbyaddr(ip) + print("Host: " + str(host[0])) + except ValueError: + print('-' *25) + print('IP is not valid') + +elif sys.argv[1]=="hostname": + host = sys.argv[2] + try: + print(host) + # gethostbyname() returns IPv4 address + ip = socket.gethostbyname(host) + print("IP Address: ", ip) + except socket.gaierror: + print('-' *25) + print("Hostname does not exist") + + diff --git a/Python/README.md b/Python/README.md index 27f88eb5b..af3a1c906 100644 --- a/Python/README.md +++ b/Python/README.md @@ -205,3 +205,4 @@ - [Zoom Automation](./Zipper) - [Linkedin Wish Sender](./Linkedin_Wishes_Sender) - [Facebook Friend Request Accepter](./Fb_Request_Accepter) +- [IP Address/Hostname Validator](./IP-Hostname_Validator)