HI All,

My name is s0ur and I am not a programmer. Isn't that the first step to recovery?

I am hoping that the experts can help a noob. I am trying to write a python script that will run on windows login. it should do the following:

1) ping IP address
2) analyze response
3a) if true (there is a response) run another command and wait 300 sec.
3b) if no response wait 300 sec.
4) repeat until log out.

It sounds simple when I write it like that but when I try to write it in python - I become an idiot. If someone can help me out on this I would appreciate it. If you have recommendations on an online python course for non-programmers I would also appreciate it. Teach a man to fish, right?

Cheers

s0ur

import os
import time
import sys
import subprocess

ip = " my secret ip"

def pingping (
    subprocess.Popen("ping -w 200 -n 1 " + ip, stdout=subprocess.PIPE)):
    result_str = pingping.stdout.read():
    "0% loss" in result_str:
	return;
# Then I become an idiot...

pingping(true);
if pingping == true
    subprocess.Popen("secret windows command here")
    waitasec

waitasec

# As you can see there is little hope for me at this point.

Thanks

Like this mate.

import time
import subprocess

ip = '127.0.0.1'

def ping(ip):
    pinging = subprocess.Popen('ping -w 200 -n 1 ' + ip, stdout=subprocess.PIPE)
    if "0% loss" in pinging.stdout.read():
        return True

while 1:
    if ping(ip):
        subprocess.Popen('cmd')
        time.sleep(0.3)
    else:
        time.sleep(0.3)

Be aware, if you run it like this it will open a shell every 300 milisecs.

Cheers and Happy coding

commented: Really nice guy! :-) +0

Thanks Beat_Slayer,

You have given me exactly what I asked for. :-) You bring up a good point about the new shell every 300 millisecs. In a perfect would I have a 300 second delay between attempts and it wouldn't open a shell at all it would run silently in the background unless there is an error. You have given me logic that I was missing so I should be able to figure that out with some trial and error.

Thanks again. (although if you insist on helping me some more - I would even MORE be grateful :-))

Cheers

s0ur

import time
import subprocess

ip = '127.0.0.1'

def ping(ip):
    pinging = subprocess.Popen('ping -w 200 -n 1 ' + ip, stdout=subprocess.PIPE,
                                creationflags=0x08000000)
    time.sleep(1) # wait 1 sec before reading ping results
    if "0% loss" in pinging.stdout.read():
        return True
    
while 1:
    if ping(ip):
        subprocess.Popen('cmd', creationflags=0x08000000)
        time.sleep(300)
    else:
        time.sleep(300)

Cheers and Happy coding

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.