4

The user will input either hostname or the IP address. If the user enters the IP address, I want to leave as it is but if the user enters the hostname I want to convert it into IP address using the following method:

def convert(hostname):
    command = subprocess.Popen(['host', hostname],
                           stdout=subprocess.PIPE).communicate()[0]

    progress1 = re.findall(r'\d+.', command)
    progress1 = ''.join(progress1)
    return progress1 

How do I do it?

user1881957
  • 3,258
  • 6
  • 34
  • 42
  • Look in this [thread](http://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python). – maverik Dec 20 '12 at 10:27
  • I already have a method to validate if the user input IP address is valid or not. – user1881957 Dec 20 '12 at 10:32
  • so, you have the validation method and the conversion method, you just need to combine them, what's the real problem then? – Samuele Mattiuzzo Dec 20 '12 at 10:47
  • Before posting this question I didn't have the conversion method. Now, I do have this function socket.gethostbyname(ip4_or_hostname) but this is not working in my code. – user1881957 Dec 20 '12 at 11:05

4 Answers4

7

To get ip whether input is ip or hostname:

ip4 = socket.gethostbyname(ip4_or_hostname)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
1

you can use a regex to match your input and test if it is a ip address or not

test = re.compile('\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b')
result = test.match(hostname)
if not result:
    # no match -> must be an hostname #
    convert(hostname)

that regex allows invalid ip addresses (like 999.999.999.999) so you may want to tweak it a bit, it's just a quick example

Samuele Mattiuzzo
  • 10,760
  • 5
  • 39
  • 63
  • Note that it doesn't cover IPv6 addresses. On Python3, you can use https://docs.python.org/3/library/ipaddress.html – Jared Feb 05 '16 at 20:33
0

There are a number of questions on stackoverflow already about validating an IP address.

  1. IP Address validation in python
  2. Validating IP Addresses in python

I would like to ask why you are communicating with a subprocess when you can do this within the standard python library.

I would recommend resolving a host name into a IP address by using some of pythons built in functionality.

You can do this by importing and using the python sockets library

For example using the code found in link 1:

import socket
import re
regex = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
result = regex.match(address)
if not result:
    address = socket.gethostbyname(address)
Community
  • 1
  • 1
Matt Seymour
  • 8,880
  • 7
  • 60
  • 101
0

In my case, host name can only contain - as a separator. So you can uncomment and use it according to your requirement.

import re

regex = "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$"
# string_check= re.compile('[@_!#$%^&*()<>?/\|}{~:.]')
string_check= re.compile('[-]')

ip_host_detail = {}
    
def is_valid_hostname_ip(IpHost):
    # pass regular expression and ip string into search() method
    
    if (re.search(regex, IpHost)):
        print("Valid Ip address")
        ip_host_detail['is_ip'] = 'True'
        ip_host_detail['is_hostname'] = 'False'
        return True
    elif(string_check.search(IpHost)):
        print("Contain hostname")
        ip_host_detail['is_hostname'] = 'True'
        ip_host_detail['is_ip'] = 'False'
            return True
    else:
        print("Invalid Ip address or hostname:- " + str(IpHost))
        ip_host_detail['is_hostname'] = 'False'
        ip_host_detail['is_ip'] = 'False'
        return False
    
    
IpHost = sys.argv[1]
# IpHost = 'RACDC1-VM123'

is_valid_hostname_ip(IpHost)
print(ip_host_detail)
Flair
  • 2,609
  • 1
  • 29
  • 41
ankuj
  • 21
  • 5