Forum Archive

Pythonista / os.system Ping

DavinE

Hello Guys,

I was planning to create a ping query via a python "script" with:

print(os.system('ping -c 5 localhost'))

but here I always get the message:

32512 

which according to the net should mean that this command was not found... so I wanted to use:

which ping

to specify the path which didn't help either...

if I try the whole thing now in StaSh:

~/Documents/site-packages/stash/bin/ping -c 1 localhost
ping -c 1 localhost

I get an output in each case:
ping localhost... got ping in 0.2782ms

could someone tell me what I am doing wrong or suggest another solution how I could reach my goal.

With kind regards
Kalysto

p.s.
Happy New Year to all!

JonB

You can't use os.system in pythonista.
The stash ping command is based on a python implementation of ping.c.

https://github.com/ywangd/stash/blob/master/bin/ping.py

(You have a copy of it inside your stash/bin folder)

You can copy that file to your project or site-packages, and use it directly by importing it. Or you might be able to use

from stash.bin import ping

But I have not tried that. You could also add that path to sys.path then import ping.

DavinE

@JonB
from stash.bin import ping this looks like it works... when i try that:

from stash.bin import ping

print(ping)

i get:
<module 'stash.bin.ping' from '/private/var/mobile/Containers/Shared/AppGroup/xxx/Pythonista3/Documents/site-packages/stash/bin/ping.py'>

but i don't get it to work....

print(ping -c 1 localhost)

here i get the error invalid Syntax

I don't know how to use it correctly :(

DavinE

Or asked another way:
I have a NAS running a MySQL server I wanted to check with the ping if the server would be on or off.

or is there a better solution how to check this ?

and could one achieve with a ping that the NAS would not go into sleep mode ?

cvp

@DavinE try

from stash.bin.ping import verbose_ping
verbose_ping('localhost', timeout=2, count=4, interval=1.0)
DavinE

@cvp said:

@DavinE try
from stash.bin.ping import verbose_ping verbose_ping('localhost', timeout=2, count=4, interval=1.0)

Yes this works ^^.
But i don't want the print thus i use it like this:

if not do_one('192.168.178.251', 1) is None:

thanks @cvp

cvp

@DavinE 👍