summaryrefslogtreecommitdiff
path: root/bin
diff options
authorrod.smith <rod.smith@canonical.com>2014-07-30 17:28:45 -0400
committerrod.smith <rod.smith@canonical.com>2014-07-30 17:28:45 -0400
commit5169ebff02db6b797bb80839d09e30e28ea566ff (patch)
tree0debdbb26593e1fc940c83d343688225683cee14 /bin
parente56b860f05b4fd2d10f5ea33654d129759b2a764 (diff)
Implemented (most of) the suggested changes
Diffstat (limited to 'bin')
-rwxr-xr-xbin/network43
1 files changed, 21 insertions, 22 deletions
diff --git a/bin/network b/bin/network
index ce0dbb5..61f0964 100755
--- a/bin/network
+++ b/bin/network
@@ -34,6 +34,7 @@ import shlex
import socket
import struct
import subprocess
+import tempfile
from subprocess import (
CalledProcessError,
check_call,
@@ -453,23 +454,21 @@ def get_test_parameters(args, environ):
def can_ping(the_interface, test_target):
working_interface = False
num_loops = 0
- devnull = open('/dev/null', 'w')
-
- while (not working_interface) and (num_loops < 48):
- working_interface = True
+ with open('/dev/null', 'w') as devnull:
+ while (not working_interface) and (num_loops < 48):
+ working_interface = True
- try:
- ping_cmd = 'ping -I {} {} -c 1'.format(the_interface, test_target)
- check_call(shlex.split(ping_cmd), stdout=devnull, stderr=devnull)
- except CalledProcessError:
- working_interface = False
- logging.warning("Ping failure on %s", the_interface)
+ try:
+ ping_cmd = 'ping -I {} {} -c 1'.format(the_interface, test_target)
+ check_call(shlex.split(ping_cmd), stdout=devnull, stderr=devnull)
+ except CalledProcessError as excp:
+ working_interface = False
+ logging.warning("Ping failure on %s (%s)", the_interface, excp)
- if not working_interface:
- time.sleep(5)
- num_loops += 1
+ if not working_interface:
+ time.sleep(5)
+ num_loops += 1
- devnull.close()
return working_interface
@@ -522,13 +521,13 @@ def interface_test(args):
# Back up routing table, since network down/up process
# tends to trash it....
- tempfile = open('/tmp/plainbox-network.route', 'wb')
+ temp = tempfile.TemporaryFile()
+
try:
cmd = "ip route save table all"
- check_call(shlex.split(cmd), stdout=tempfile)
- except CalledProcessError:
- logging.warning("Unable to save routing table!")
- tempfile.close()
+ check_call(shlex.split(cmd), stdout=temp)
+ except CalledProcessError as route_error:
+ logging.warning("Unable to save routing table: %s", route_error)
result = 0
# Stop all other interfaces
@@ -581,16 +580,16 @@ def interface_test(args):
result = 3
# Restore routing table to original state
- tempfile = open('/tmp/plainbox-network.route', 'rb')
+ temp.seek(0)
devnull = open('/dev/null', 'w')
try:
cmd = "ip route restore"
# Harmless "RTNETLINK answers: File exists" messages on stderr
- check_call(shlex.split(cmd), stdin=tempfile, stderr=devnull)
+ check_call(shlex.split(cmd), stdin=temp, stderr=devnull)
except CalledProcessError:
logging.warning("Unable to restore routing table!")
- tempfile.close()
devnull.close()
+ temp.close()
return result