Created
June 27, 2022 16:34
-
-
Save atucom/a84c5bc4f1a2f02dc92274a674f89db8 to your computer and use it in GitHub Desktop.
only return IP if either IP or hostname is supplied
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# @atucom | |
''' parse simple command line arguments ''' | |
import argparse | |
parser = argparse.ArgumentParser(description='only return IP if either IP or hostname is supplied') | |
parser.add_argument('target', help='IP or hostname') | |
args = parser.parse_args() | |
def ip_filter(input): | |
''' return ip if already an ip, otherwise resolve hostname to ip ''' | |
import socket | |
try: | |
ip = socket.gethostbyname(input) | |
return ip | |
except socket.gaierror as e: | |
print("BAD INPUT: {}".format(input)) | |
except: | |
return input | |
ip = ip_filter(args.target) | |
print(ip) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment