Home > Software design >  'ping' is not recognized as an internal command or external, an operable program or a batc
'ping' is not recognized as an internal command or external, an operable program or a batc

Time:08-17

I have a simple script in Python using VS Code to make a ping verification, I don't know why it isn't recognize 'ping' as a command.

import os

ip_list = ["8.8.8.8", "8.8.4.4"]

for ip in ip_list:
    response = os.popen(f"ping {ip}").read()
    if "Received = 4" in response:
        print(f"UP {ip} Ping Successful")
    else:
        print(f"DOWN {ip} Ping unsuccessful")

CodePudding user response:

Assuming you're on windows

import os

ip_list = ["127.0.0.1"]
for ip in ip_list:
    response = os.popen(f"C:\Windows\System32\ping {ip}").read()
    if "Received = 4" in response:
        print(f"UP {ip} Ping Successful")
    else:
        print(f"DOWN {ip} Ping unsuccessful")

CodePudding user response:

This error occurs because the computer cannot find an executable program for ping, so it cannot execute the ping command.

You can fix the problem by adding the ping executable directory to your computer's environment variables.

  1. WIN R and type sysdm.cpl and press enter

    enter image description here

  2. Select Environment Variables at the bottom right

    enter image description here

  3. Double-click path (you can choose to add variables for the user or for the entire system)

    enter image description here

  4. Choose New and enter C:\Windows\system32

    enter image description here

  5. OK, then reopen vscode

Sorry my system language is Chinese. For more information on creating environment variables you can check this enter image description here

  • Related