Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python script to check the validity of a given IP address #1368

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Python/IP-Hostname_Validator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# IP/Hostname Validator
### Libraries 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically i think these aren't libraries, but instead modules of the standard library.


### 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)
27 changes: 27 additions & 0 deletions Python/IP-Hostname_Validator/main.py
Original file line number Diff line number Diff line change
@@ -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")


1 change: 1 addition & 0 deletions Python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)