Python Forum

Full Version: passing ver to os.popen
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
im tying to get my local ip then generate a range list then ping each one of them to check who is live and who is dead
this is my code :

import socket
import ipaddress
import os
import nmap

getmy_ip = socket.gethostname()
getmy_ip = socket.gethostbyname(getmy_ip)

net4 = ipaddress.ip_network("192.168.1.0/24")
for x in net4.hosts():
    ping = os.popen("ping {x}").read()
    


    #scan1 = nmap.PortScanner()
    #scan1 = scan1.scan(str(x))
    #print(scan1)
    
Output:
ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known ping: {x}: Name or service not known
why and how i can solve it in simple why please :(
f"ping {x}" - make it f-string. Also better use subprocess.run()
(Jan-26-2021, 12:25 PM)buran Wrote: [ -> ]f"ping {x}" - make it f-string. Also better use subprocess.run()
its work with f"ping {x}"
how i can do it with subprocess.run() ?
(Jan-26-2021, 12:27 PM)evilcode1 Wrote: [ -> ]how i can do it with subprocess.run() ?
never mind, I thought of you using os.system(), os.popen() is implemented with subprocess.
But you can look at the docs