Get IP address using domain in text file in python
Question: I have a text file contains domains and IPs, I want to get the IP using the domain name but it returns null. E.g.: Prerequisite: Python GUI – tkinter In this article, we are going to see how to find IP from Domain Names and bind it with GUI Application using Python.
Get IP address using domain in text file in python
I have a text file contains domains and IPs, I want to get the IP using the domain name but it returns null.
import json import os import sys import pathlib import platform HERE = pathlib.Path(__file__).parent def clean_server_names(name): server_names_list = [x.lower().strip() for x in open(HERE / "servers" / "myservers.txt", 'r').readlines()] for server in server_names_list: domain_name_and_code = server.split(' ') domain = domain_name_and_code[0].strip() ip = domain_name_and_code[-1].strip() if name == domain: print(ip) clean_server_names("domain1.nordvpn.com ")
Here’s what inside my text file.
domain1.nordvpn.com 192.168.1.1
You have a lot of code in there that’s not needed and is cluttering up what your intent is.
$ cat ./x.py #! /usr/bin/env python3 # -*- coding: UTF8 -*- def clean_server_names(name): with open("servers/myservers.txt", "r") as f: for line in f: domain_name, ip = line.lower().split() if name == domain_name: print(ip) clean_server_names("domain1.nordvpn.com") $ cat ./servers/myservers.txt domain1.nordvpn.com 192.168.1.1 $ ./x.py 192.168.1.1
$ awk '$1 == "domain1.nordvpn.com"' servers/myservers.txt 192.168.1.1
Python — IP address by Domain Name, Works fast, easy and also in windows, anaconda directly. If you need to use the ip, it would be domainName = ‘anydomain.here’ ip= socket.gethostbyname (domainName) print («IP:», ip) # Python3 code to display hostname and # IP address # Importing socket library import socket # Function … Code sampleimport socketdomainName = input(‘Enter the domain name: ‘)print(socket.gethostbyname(domainName))Feedback
Running list of domain name to get IP address
I want to run «host domain.com» by getting in URL
for url in list: Host = "host %s" % url IPADDRESS = subprocess.check_output(HOST, shell=True) print IPADDRESS
enter code here #!/usr/bin/python import subprocess list = ("/root/cisco/indexlist.txt","r") for hostname in list: p1 = subprocess.Popen(['host', hostname],stdout=subprocess.PIPE) p2 = subprocess.Popen(['grep', 'IP ADDRESS'], stdin=p1.stdout, stdout=subprocess.PIPE) p3 = subprocess.Popen(['sort','-u'],stdin=p2.stdout, stdout=subprocess.PIPE) print (p3.communicate()[0])
os.system("host %s | grep 'has address' | sort -u" % (url))
Python program to find IP Address, This article focus on How to get IP address of your computer in python. You have to first import socket library and then use IP = socket.gethostbyname (hostname) and then print the value of the ip into the print () function your IP address as output as shown in the program given below.
Determine if Host is Domain Name or IP in Python
Is there a function in python that determines if a hostname is a domain name or an IP(v4) address?
Note, the domain name may look like: alex.foo.bar.com or even (I think this is valid): 1.2.3.com .
One of the visual differences between IP and domain address is when you delete dots in the IP address the result would be a number. So based on this difference we can check if the input string is an IP or a domain address. We remove dots in the input string and after that check if we can convert the result to an integer or we get an exception.
def is_ip(address): return address.replace('.', '').isnumeric()
Although in some IP-Representations like dotted hexadecimal (e.g. 0xC0.0x00.0x02.0xEB ) there can be both letters and numbers in the IP-Address. However the top-level-domain (.org, .com, . ) never includes numbers. Using the function below will work in even more cases than the function above.
def is_ip(address): return not address.split('.')[-1].isalpha()
I’d use IPy to test if the string is an IP address, and if it isn’t — assume it’s a domain name. E.g.:
from IPy import IP def isIP(str): try: IP(str) except ValueError: return False return True
Get IP address using domain in text file in python, I have a text file contains domains and IPs, I want to get the IP using the domain name but it returns null. Here is my code: import json import os import sys import pathlib import platform HERE =
Create a GUI to find the IP for Domain names using Python
Prerequisite: Python GUI – tkinter
In this article, we are going to see how to find IP from Domain Names and bind it with GUI Application using Python. We will use iplookup module for looking up IP from Domain Names. It is a small module which accepts a single domain as a string, or multiple domains as a list, and returns a list of associated IP.
Run this code into your terminal for installation.
- Import module
- Create objects of iplookup
- Pass the domain into iplookup obj
- Now traverse the IP
Implementation: