Reputation: 13
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()
Upvotes: 0
Views: 572
Reputation: 6745
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)
Upvotes: 3
Reputation: 1739
@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.
Upvotes: 1