0

This code writes google.de has address 216.239.34.117 to the shell

import os

os.system("host google.de")

Now i want to save the IP-Address into a string variable, but i can`t figure out how i read from the command line. Or is there even an easier way to get the IP-Adress to a variable?

Thanks and Greetings, Alex

lxg95
  • 553
  • 2
  • 8
  • 28
  • 1
    Maybe you are looking for argparse – Carsten Mar 06 '20 at 10:09
  • 1
    This is not reading from command line! You want to redirect subprocess' stdout to your own program. I'd suggest using e.g. `subprocess.Popen` as it allows you to control stdin, stdout and stderr – h4z3 Mar 06 '20 at 10:09
  • 1
    I have been there before : https://stackoverflow.com/questions/54055963/catch-print-output – Ozgur Oz Mar 06 '20 at 10:09
  • 4
    Does this answer your question? [Assign output of os.system to a variable and prevent it from being displayed on the screen](https://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on) – Ocaso Protal Mar 06 '20 at 10:10
  • @OzgurOz OcasoProtal I think that is what i'm searching for! Thank you :) – lxg95 Mar 06 '20 at 10:13

1 Answers1

0

Why won't you use socket library instead of os?

import socket

host_ip = socket.gethostbyname('google.de')
print(host_ip)
Kajsa Gauza
  • 1,348
  • 1
  • 12
  • 22