To complete my script, I would like to put a timer,
let say, I want the primary IP to be ping for 24 hrs, what
do you suggest me and than I stop anoying you : )
Brgds
5 8967
To complete my script, I would like to put a timer,
let say, I want the primary IP to be ping for 24 hrs, what
do you suggest me and than I stop annoying you : )
Brgds
I've also gotten pretty good at the whole 'site moderator' thing (ie: this new thread in your name).
It's really no annoyance. If we didn't enjoy this forum and the work entailed in keeping it alive and growing, we wouldn't be posting, etc. here.
Probably the best (safest) way to run a script periodically would be to schedule a task in the OS. Windows (in your case, I believe) uses the "Scheduled Tasks" applet in the Control Panel. On *nix, it's cron.
The reason for using the OS instead of a Python timer or scheduler is that the interface is guaranteed to be running even when your computer is first booted. If you would rather require that a Python script be running (it would be a great feature if you could shut down the task with out using the Task Manager (or ps)), then I could probably whip us some running examples which you could then fine tune. I strongly suggest the former solution, though.
Hi Bartonc,
I did the find the below script, to implement
a timer to ping each minute and stop in 24 hrs.
according to your script (below) where should I place
the following properly ? (did try several combinaisons but unsuc).
Or do you have a better idea ? (answer is probably yes : ) ! -
-
insert time
-
the_time = time.time()
-
start_time = the_time
-
end_time = the_time + (60*60*24) # 24 hrs
-
while the_time < end_time:
-
# insert my code here ????????????????what I put here and How??
-
time.sleep(60) # wait 60 seconds
-
the_time = time.time() # get the new time
-
-
-
-
def fil(data):
-
patt = re.compile(r'\((\d+)% loss\)')
-
patt1 = re.compile(r'(\d+.\d+.\d+.\d)')
-
-
for line in data.split('\n'):
-
-
if line.startswith("Ping statistics for"):
-
ip = patt1.search(line).group(1)
-
-
if patt.search(line):
-
loss_num = int(patt.search(line).group(1))
-
-
if loss_num >= 2 :
-
s = open('c:/tmp/myprimarylogs.xls', 'a+')
-
s.write("%s '%s'\n" % (loss_num, ip))
-
s.close()
-
# if loss >= 2, return False - then secondary IPs are pinged
-
print 'loss_num is >= 2 ', loss_num
-
return False
-
else:
-
print 'loss_num is < 2,', loss_num
-
return True
-
-
def ping(*fnames):
-
g = open(fnames[0], 'w')
-
for fn in fnames[1:]:
-
f=open(fn, 'r')
-
ipList = [line.strip() for line in f.readlines()]
-
f.close()
-
resList = []
-
for ipAdd in ipList:
-
pingaling =os.popen("ping %s" %(ipAdd),"r")
-
data = pingaling.read()
-
resList.append(data)
-
# if loss > 2, ping secondary IPs
-
proceed = fil(data)
-
if proceed:
-
break
-
g.write('/n'.join(resList))
-
if proceed:
-
break
-
g.close()
-
-
def get_IPs(fnP, fnS):
-
while True:
-
ipAddP = raw_input("# Enter Primary IP: ")
-
if validIP(ipAddP):
-
f = open(fnP, 'w')
-
f.write(ipAddP.strip() + "\n")
-
f.close()
-
break
-
else:
-
print "Invalid IP."
-
ipList = []
-
while True:
-
while True:
-
ipAddS = raw_input("# Enter Secondary IP: ")
-
if validIP(ipAddS):
-
ipList.append(ipAddS.strip())
-
break
-
else:
-
print "Invalid IP"
-
s = raw_input("# Enter another IP? Y/N: " )
-
if s.upper() == "N":
-
f = open(fnS, 'w')
-
f.write('\n'.join(ipList))
-
f.close()
-
break
-
-
fnP = 'c:/tmp/primaryip.xls'
-
fnS = 'c:/tmp/secip.xls'
-
fnW = 'c:/tmp/workfile.txt'
-
-
get_IPs(fnP, fnS)
-
ping(fnW, fnP, fnS)
-
-
-
Loads of code
something like: -
#!/usr/bin/env python
-
# coding: UTF-8
-
-
import time
-
import re
-
import whateverelse
-
-
fnP = 'c:/tmp/primaryip.xls'
-
fnS = 'c:/tmp/secip.xls'
-
fnW = 'c:/tmp/workfile.txt'
-
-
def fil(data):
-
......
-
-
def ping(*fnames):
-
......
-
-
def get_IPs(fnP, fnS):
-
......
-
-
def main():
-
the_time = time.time()
-
start_time = the_time # Youre not using start_time at all btw.
-
end_time = the_time + (60*60*24) # 24 hrs
-
while the_time < end_time:
-
get_IPs(fnP, fnS)
-
ping(fnW, fnP, fnS)
-
time.sleep(60) # wait 60 seconds
-
the_time = time.time() # get the new time
-
-
-
if __name__ == "__main__":
-
main()
-
perhaps? Not many other ways of doing it.
Hi,
tks for your reply,
I did try your script but it
stop to :
# Enter Primary IP:
Hi,
tks for your reply,
I did try your script but it
stop to :
# Enter Primary IP:
Sorry to say it but your code makes no sense at all.
You use several functions that are never defined somwhere.
But i do know that this part: -
-
end_time = the_time + (60*60*24) # 24 hrs
-
while the_time < end_time:
-
get_IPs(fnP, fnS)
-
ping(fnW, fnP, fnS)
-
is 'wrong' and shuld be this: -
-
end_time = the_time + (60*60*24) # 24 hrs
-
get_IPs(fnP, fnS)
-
while the_time < end_time:
-
ping(fnW, fnP, fnS)
-
<rant>
WO! THERE IS A SLIMY LITTLE BUG IN THE CODE-TAGGS!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have...
|
by: Brandon J. Van Every |
last post by:
I'm realizing I didn't frame my question well.
What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump
up in your chair and scream "Wow! Ruby has *that*? That is SO...
|
by: Derek Fountain |
last post by:
Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run
this bit of code" sort of thing?
|
by: Jonas Kölker |
last post by:
Hello.
I'm doing a Rubik's Cube timer, so I need a function to measure 100ths of
a second. I've browsed through the library reference of python.org, but I
didn't find anything that struck me as...
|
by: Reed L. O'Brien |
last post by:
I see rotor was removed for 2.4 and the docs say use an AES module
provided separately... Is there a standard module that works alike or
an AES module that works alike but with better encryption?...
|
by: Harro de Jong |
last post by:
(absolute beginner here, sorry if this seems basic)
Section 7.10 of 'How to Think Like a Computer Scientist' contains this
discussion of string.find and other string functions:
(quote)
We can...
|
by: John Salerno |
last post by:
My code is below. The main focus would be on the OnStart method. I want
to make sure that a positive integer is entered in the input box. At
first I tried an if/else clause, then switched to...
|
by: Thomas Ploch |
last post by:
Hello folks,
I am having troubles with implementing a timed queue. I am using the
'Queue' module to manage several queues. But I want a timed access, i.e.
only 2 fetches per second max. I am...
|
by: DaTurk |
last post by:
Hi,
I'm creating an application that will need to use a timer to do a
static method call every second. My question, is what would be the
best timer to use? I know there are several, but I'd...
|
by: Robin Becker |
last post by:
I've just been testing out Jakob Sievers' speedup of Python 2.5.2 by
compiling on freebsd with gcc-4.3.3 (the standard freebsd 6.1 gcc is 3.4.4).
I'm glad to say that his modification did improve...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |