-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_hosts.py
executable file
·69 lines (58 loc) · 2.01 KB
/
rename_hosts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python2.7
import subprocess
import json
import sys, os
# TODO ADD EXCEPTIONS
try:
if not os.path.isfile(sys.argv[1]):
print "ERROR: File provided as argument does not exist"
exit(1)
except IndexError:
print "ERROR: json with hosts to rename was not provided.\n" \
"Pass it as the first argument"
exit(1)
# GETTING CURRENT HOSTNAME
get_hostname_cmd = "hostname"
process = subprocess.Popen(
get_hostname_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
old_hostname, error = process.communicate()
if error:
print "ERROR: Can not execute '" + get_hostname_cmd + "' command. Details:"
print error
exit(1)
print "old_hostname is " + old_hostname
# GETTING CURRENT IP
get_ip_cmd = "hostname -I"
process = subprocess.Popen(get_ip_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
host_ip, error = process.communicate()
if error:
print "ERROR: Can not execute '" + get_ip_cmd + "' command. Details:"
print error
exit(1)
host_ip=host_ip.split()
print "host_ip is " + host_ip
# PARSING DATA FROM JSON
try:
with open(sys.argv[1], 'r') as outfile:
# json.dump(hostnames_list, outfile)
hostnames_list = json.load(outfile)
except ValueError:
print "ERROR: Provided json file is not valid"
exit(1)
found = False
for ip, new_hostname in hostnames_list.items():
if ip in host_ip:
rename_host_cmd = "hostnamectl set-hostname " + new_hostname
process = subprocess.Popen(
rename_host_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
output, error = process.communicate()
if error:
print "ERROR: Can not execute '" + rename_host_cmd + "' command. Details:"
print error
exit(1)
print "HOST RENAMED SUCCESSFULLY"
found = True
break
if not found:
print "Hostname of current host was not found in a list.\n" \
"So it was not renamed"