Example #1
0
File: pifi.py Project: ihelal/pifi
def set_hostname(new_hostname):
    old_hostname = etc_io.get_hostname()
    print("Changing hostname from %s to %s" % (old_hostname, new_hostname))

    try:
        etc_io.set_hostname(old_hostname, new_hostname)
        socket.sethostname(new_hostname)
    except PermissionError:
        print(
            "Error writing to /etc/hosts or /etc/hostname, make sure you are running with sudo"
        )
    except OSError:
        print(
            "Error writing to /etc/hosts or /etc/hostname, make sure you are running with sudo"
        )
Example #2
0
def set_hostname(argv):
    parser = argparse.ArgumentParser(description="Set a new hostname")
    parser.add_argument("hostname")
    args = parser.parse_args(argv)
    new_hostname = args.hostname

    old_hostname = etc_io.get_hostname()
    print("Changing hostname from %s to %s" % (old_hostname, new_hostname))

    try:
        etc_io.set_hostname(old_hostname, new_hostname)
        socket.sethostname(new_hostname)
    except PermissionError:
        print(
            "Error writing to /etc/hosts or /etc/hostname, make sure you are running with sudo"
        )
    except OSError:
        print(
            "Error writing to /etc/hosts or /etc/hostname, make sure you are running with sudo"
        )