Skip to content

Commit

Permalink
Added acn-setup-hostname: check if HOSTNAME entry exists otherwise in…
Browse files Browse the repository at this point in the history
…sert HOSTNAME=hostname
  • Loading branch information
phumpal committed Oct 6, 2012
1 parent e632905 commit f60d948
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions bin/acn-setup-hostname
@@ -1,6 +1,6 @@
#!/usr/bin/python3
# vim:encoding=utf-8:ts=2:sw=2:expandtab

import mmap
from acnpython32 import *

try:
Expand All @@ -19,15 +19,21 @@ try:

hostname = GetInput_Regex("Enter the hostname: ", Regex='^[a-zA-Z0-9-.]+$')
check_call(('/bin/hostname', hostname))

sysconfigfile = open('/etc/sysconfig/network')
hosts = mmap.mmap(sysconfigfile.fileno(), 0, access=mmap.ACCESS_READ)

check_call(('/bin/sed', '--in-place', 's/^HOSTNAME=.*/HOSTNAME=%s/' % hostname, '/etc/sysconfig/network'))
print()
print("New contents of /etc/syconfig/network:")
print("---")
call(("cat", "/etc/sysconfig/network"))
print("---")
print()

if 'HOSTNAME' in hosts:
check_call(('/bin/sed', '--in-place', 's/^HOSTNAME=.*/HOSTNAME=%s/' % hostname, '/etc/sysconfig/network'))
print()
print("New contents of /etc/syconfig/network:")
print("---")
call(("cat", "/etc/sysconfig/network"))
print("---")
print()
else:
doc = open('/etc/sysconfig/network', 'a')
doc.write('HOSTNAME=%s'%hostname)

except Exception as e:
print()
Expand Down

0 comments on commit f60d948

Please sign in to comment.