Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
passing ver to os.popen
#1
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 :(
Reply
#2
f"ping {x}" - make it f-string. Also better use subprocess.run()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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() ?
Reply
#4
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing string args to Popen CardBoy 3 4,241 Jan-16-2018, 09:22 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020