0

i made this script a while ago and it was working if i remember correctly but now i get a could not find host error. Any help is appreciated.

from tkinter import *
from tkinter import ttk
import socket
import sqlite3
import subprocess




BASE = Tk()
BASE.geometry("400x400")




def PING_CLIENT():
    
    HOST = PING_ENTRY

    command = "ping {} 30 -t".format(HOST)
    subprocess.run(command)
    
    


PING = ttk.Button(BASE, text="Ping IP", command=PING_CLIENT)
                  
PING.place(x=35, y=100, height=30, width=150)

PING_ENTRY = ttk.Entry(BASE)
PING_ENTRY.place(x=200, y=100, height=30, width=150)


BASE.mainloop()

2 Answers2

3

You need to get the value of your Entry widget. To do this, call the get() method on the widget. You can read more about the Tkinter Entry Widget here.

Example:

HOST = PING_ENTRY.get()

Also, I'm not exactly sure what the "30" in your command is supposed to do. If you intend for it to ping 30 times, you need to add the -n switch beforehand (on Windows) or -c switch (on most Linux distributions). For example, on Windows:

command = "ping {} -n 30 -t".format(HOST)

JoshG
  • 6,472
  • 2
  • 38
  • 61
  • Thank you, i had a few versions of this and i imagine i deleted the one that had this included when i cleared some stuff out. – iHateThisShit Jun 28 '20 at 14:34
  • Hey sorry to bother again but i was trying to add a stop ping button with def PING_STOP(): subprocess.Popen.terminate() dont know what else to add to get it to work, im not the best lol – iHateThisShit Jun 28 '20 at 18:18
  • Just saw your reply now. It looks like @DineshKumar might have already answered this in his answer. But if you're still stuck just let me know and I'll see what I can do to help! – JoshG Jun 29 '20 at 04:45
  • His method works but i would like to be able to use a button to stop the ping. :) – iHateThisShit Jun 29 '20 at 10:01
  • To do this, I would recommend using `subprocess.Popen` instead so you can get a reference to the process, then terminate it. Crude working example: https://pastebin.com/ea13QedA – JoshG Jun 29 '20 at 10:35
1

@AndroidNoobie's answer works fine. I am adding this just in case you want the execution to be async, you could use subprocess.Popen instead of subprocess.run.

The UI freezes until the run execution is complete. If you don't want that to happen, I would recommend using subprocess.Popen

def PING_CLIENT():

    HOST = PING_ENTRY.get()

    command = "ping {} -n 30  -t".format(HOST)
    #subprocess.run(command, shell=True)
    subprocess.Popen(command, shell=True)

From another SO answer: The main difference is that subprocess.run executes a command and waits for it to finish, while with subprocess.Popen you can continue doing your stuff while the process finishes and then just repeatedly call subprocess.communicate yourself to pass and receive data to your process.

EDIT: Added code to make the ping stop after 30 trials.

To make your code stop after a specific number of packets use the below code.

Windows:

    command = "ping -n 30 {}".format(HOST)
    pro = subprocess.Popen(command, shell=True,stdout=subprocess.PIPE)
    print(pro.communicate()[0]) # prints the stdout

Ubuntu:

    command = "ping -c 30 {}".format(HOST)
    pro = subprocess.Popen(command, shell=True,stdout=subprocess.PIPE)
    print(pro.communicate()[0]) # prints the stdout

-t basically pings indefinitely in windows.That's why you weren't able to stop it.

DineshKumar
  • 1,599
  • 2
  • 16
  • 30
  • Thank you that was the next thing i was working on fixing, saved me some time there. Is there a way to stop the ping ? if so please let me know, this is the first time ive used this site and the community is great, thanks to everyone that replied – iHateThisShit Jun 28 '20 at 16:30
  • Glad to be of help.Could you please let us know what is that 30 in your `command`? What is it that you are trying to implement in this app exactly? – DineshKumar Jun 28 '20 at 17:00
  • it was to make it stop the ping after 30 replies but i lost the code that was originally working so obviously that was messed up – iHateThisShit Jun 28 '20 at 17:37
  • @iHateThisShit I have edited the code with your ask. Try this and let me know if it worked :) – DineshKumar Jun 29 '20 at 04:40