1

I am creating an nslookup utility tool in python using socket.

Code:

import socket


while True:
    try:
        server = input()
        ip_addr = socket.gethostbyname(server)
        print(server, ip_addr, '\n')
    except socket.gaierror:
        print(server, 'unable to find ip')
        break

returns:

google.com google.com 172.217.8.14

yahoo.com yahoo.com 98.138.219.231

domain.domain.domain domain.domain.domain unable to find ip

stackoverflow.com


So I want to hide the input so that only the following is displayed:

google.com 172.217.8.14

yahoo.com 98.138.219.231

domain.domain.domain unable to find ip


I also noticed that the last entry never gets ran through the script and I have to manually hit enter for "stackoverflow.com" to lookup the IP. Is there anyway around that?

I appreciate any help in advance! Thanks!

avgnetmin
  • 31
  • 3
  • I should note that it looks like the input and output are on one line but I messed up on the formatting. the first address is above the output and ip. – avgnetmin Jun 04 '20 at 14:33
  • From the answer to [this question](https://stackoverflow.com/questions/18248142/what-can-i-use-to-go-one-line-break-back-in-a-terminal-in-python): Use `\033[1A` . I was not able to get rid of a space character for some reason I don't understand yet. – David Wierichs Jun 04 '20 at 14:46

2 Answers2

2

Just don’t print the server name, this will work:

import socket


while True:
    try:
        server = input()
        ip_addr = socket.gethostbyname(server)
        print(ip_addr, '\n')
    except socket.gaierror:
        print('unable to find ip')
        break

Also you have to hit enter, the interpreter can’t understand when you are done writing

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
ArcticFox
  • 50
  • 8
1

You could try to use getpass instead of input if this is want you want.

e.g

import getpass.getpass

server = getpass()
JuicyMilk
  • 116
  • 5
  • but I would prefer to use @ArcticFox's answer edit: but only if you not want to hide the output itself but don't want to see the domain 2 times – JuicyMilk Jun 04 '20 at 14:41