diff options
author | Todd Gaunt <singularik@iol.unh.edu> | 2016-10-03 16:02:12 -0400 |
---|---|---|
committer | Todd Gaunt <singularik@iol.unh.edu> | 2016-10-03 16:02:12 -0400 |
commit | 4f0ecb702a601d122f261a134007377435e4aca1 (patch) | |
tree | 40acae3074ab4a5ec1287e282b31646d22ed55be /pharos-validator/src/pxe_initrd/src/bin/initial_network.py | |
parent | 6ecb8d290c106e41d0f5a446e7aa878a219224ff (diff) |
Add pharos-validator tool
Change-Id: I38e077c2c90059e39ee9871abf5d867a875827a3
Signed-off-by: Todd Gaunt <singularik@iol.unh.edu>
Diffstat (limited to 'pharos-validator/src/pxe_initrd/src/bin/initial_network.py')
-rwxr-xr-x | pharos-validator/src/pxe_initrd/src/bin/initial_network.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/pharos-validator/src/pxe_initrd/src/bin/initial_network.py b/pharos-validator/src/pxe_initrd/src/bin/initial_network.py new file mode 100755 index 0000000..6c98f6f --- /dev/null +++ b/pharos-validator/src/pxe_initrd/src/bin/initial_network.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +# bin/setup_interface + +# ----------------------------------------------------------------------- + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# ------------------------------------------------------------------------ + +# Author: Todd Gaunt, toddgaunt@iol.unh.edu or toddgaunt@gmail.com +# License: Apache v2.0 +# Description: Script for setting up initial network interfaces +# it activates dhcp on all interfaces in order to at least get the admin +# network up + +import os +import subprocess +import netifaces + +def generate_interfaces_file(ifaces, os_network_file): + """Takes a list of interfaces and a location to save a network + interfaces file""" + interfaces = "" + for i in ifaces: + n = "auto " + str(i) + "\n" \ + + "iface " + str(i) + " inet dhcp\n" + interfaces += n + return interfaces + +def set_interfaces_up(ifaces): + """Uses ifup command to put network devices up according to + interfaces file""" + for iface in ifaces: + ifupcmd = [ \ + "ifup", + iface] + ifdowncmd = [ \ + "ifdown", + iface] + with open(os.devnull, 'w') as fn: + status = subprocess.Popen(ifdowncmd, stdout=fn, stderr=fn).wait() + status = subprocess.Popen(ifupcmd, stdout=fn, stderr=fn).wait() + print(str(iface) + " " + str(status)) + +def main(): + os_network_file="/etc/network/interfaces" + ifaces = netifaces.interfaces() + interfaces = generate_interfaces_file(ifaces, os_network_file) + with open(os_network_file, 'w') as fd: + fd.write(interfaces) + set_interfaces_up(ifaces) + +if __name__ == "__main__": + main() |