Hi, everybody,
Did work hard on this one, as I`m a newbies...
I did write the entire below script...
This script is suppose to ping:
a primary IP (only one), (entered manually w raw-input)
and than
secondaries IP ( less than 10), (entered manually w raw-input)
after entering the IPsssss....
the ping command does ping the primary, if the primary loss packets average is more than 2% than it goes to a files called 'myprimarylogs. xls'
and start to ping the secondaries IP,
and if the secondaries if more than 2 % than goes to the same above file called: 'myprimarylogs. xls'
once counter finish (after 2 count) than print e-mail.
Is someone can provide my suggestion,
I always get this error msg :
Here is my code:
Did work hard on this one, as I`m a newbies...
I did write the entire below script...
This script is suppose to ping:
a primary IP (only one), (entered manually w raw-input)
and than
secondaries IP ( less than 10), (entered manually w raw-input)
after entering the IPsssss....
the ping command does ping the primary, if the primary loss packets average is more than 2% than it goes to a files called 'myprimarylogs. xls'
and start to ping the secondaries IP,
and if the secondaries if more than 2 % than goes to the same above file called: 'myprimarylogs. xls'
once counter finish (after 2 count) than print e-mail.
Is someone can provide my suggestion,
I always get this error msg :
Code:
Traceback (most recent call last):
File "E:\Carl Python\21 Octo 2007\PRIMARYandSECONDARY.txt", line 70, in <module>
ipAddress(0)
File "E:\Carl Python\21 Octo 2007\PRIMARYandSECONDARY.txt", line 54, in ipAddress
ipAddress(i+1)
TypeError: 'str' object is not callable
Here is my code:
Code:
import re
import time
import thread
import os
import sys
f=open('c:/tmp/primaryip.xls','w')
f.close()
g=open('c:/tmp/workfile.txt','w')
g.close()
h=open('c:/tmp/myprimarylogs.xls','w')
h.close()
j=open('c:/tmp/secip.xls','w')
j.close()
def validIP(ipAddress):
ipRegex = r"^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$"
re_ip = re.compile(ipRegex)
return re_ip.match(ipAddress)
def validIP1(ipAddress1):
ipRegex = r"^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$"
re_ip = re.compile(ipRegex)
return re_ip.match(ipAddress1)
print
print time.asctime()
print
print (' Ctrl+C to EXIT')
print
def ipAddress1(i):
ipAddress1 = raw_input("# Enter SECONDARY IP: ")
if i<=10:
#if validIP1(ipAddress1 and i<=10): ##########################
f=open('c:/tmp/secip.xls', 'a+')
f.write(ipAddress1+"\n")######################
f.close()
ipAddress1(i+1)
anotherip()
if i>10:
ping()#######################
else:
print "Wrong syntax"
ipAddress1(i)#########################
def ipAddress(i):
ipAddress = raw_input("# Enter Primary IP: ")
if i<1 :
ipAddress(i+1)
print i
f=open('c:/tmp/primaryip.xls', 'a+')
f.write(ipAddress+"\n")##########################
f.close()
#print f
#time.sleep(3)
#print ('Press Enter to Start')
if i>1 :
ipAddress1(i)
else:
print "Wrong syntax"
ipAddress1()
ipAddress(0)
def anotherip():
anotherip= raw_input("# Enter another IP? Y/N: " )
if anotherip.upper() == 'Y':
ipAddress1()##################
if anotherip.upper() == 'N':
ping()##################
else:
print ('bye!')
def ping(i):
print i
if i<=2:
pingaling =os.popen("ping %s" %(ipAddress),"r")
results = pingaling.read()
print results
for result in results:
for line in results:
#os.remove("c:/tmp/workfile.txt")
g=open('c:/tmp/workfile.txt', 'a+')
g.write(line+"\n")
g.close()
ping(i+1)
ping1()
if i>2:
filter()
def ping1(i):
###results1=[]##########################
z= len(ipAddress1)
print z
while i==z :
pingaling1 =os.popen("ping %s" %(ipAddress1),"r")
#####next IP in ipAddress1 or secip.xls
results1 = pingaling1.read()
print results1
for result in results1:
for line in results1:
#os.remove("c:/tmp/workfile.txt")
g=open('c:/tmp/workfile.txt', 'a+')
g.write(line+"\n")
g.close()
ping1(i+1)
ping1()
else:
ping()
def filter():
for line in open("c:/tmp/workfile.txt"):
s = 0
e = 0
loss_num = 0 # Initialize it
if line.startswith("Ping statistics for"):
ip=line.split()[-1][:-1]
if line.find("(")!=-1:
s=line.index("(")
e=line.index(")")
loss=line[s+1:e].replace("loss","")
print 'loss = ' + loss + ip
loss_num = loss.strip() # remove spaces if any
loss_num = loss_num.strip('%') # remove the % sign from loss
print 'number = ' + loss_num
try:
loss_num = float(loss_num) # change it into a number
if loss_num >= 2 :
output_line = loss + ',' + "'" + ip + "'" + "\n"
s=open('myprimarylogs.xls', 'a+')
#s=open('c:/tmp/myprimarylogs.cvs', 'a+')
s.write(output_line+"\n")####################
#s.write(line+"\n")
s.close()
#######################################
except:
loss_num = ''
mail()
def mail():
#need to carry on, here, this is the end of PING.
print('Do you want to Email?')
time.sleep(2)
sys.exit()
Comment