I have created the following script ( Python version 2.x ) in order to verify the IP address
in case the IP validation is passed the script will print OK
else the script will print Fail
the validation is very well ,
but what I don't want to print is the errors that comes from the exception ( see the Exe of the script ) ,
I just want to print OK or Fail , and redirect the standard error to null
please advice what need to update in my script to do that?
#!/usr/bin/python
import commands
import subprocess
import os
import re
import socket
def validIP(address):
parts = address.split(".")
if len(parts) != 4:
return False
for item in parts:
if not 0 <= int(item) <= 255:
return False
try:
socket.inet_aton(address)
return True
except socket.error:
return False
f = open('/dev/null', 'w')
sys.stdout = f
f.close()
address = raw_input("Please enter IP address : ")
res = validIP(address)
if res == True:
print"OK"
else:
print "Fail"
Exe the script: ( from Linux red-hat machine version 6 )
./Check_IP_Address.py
Please enter IP address : 192.9.200.WRONG
Traceback (most recent call last):
File "./Check_IP_Address.py", line 37, in <module>
res = validIP(address)
File "./Check_IP_Address.py", line 23, in validIP
if not 0 <= int(item) <= 255:
ValueError: invalid literal for int() with base 10: 'WRONG'