-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinuxDNSChange
70 lines (64 loc) · 2.36 KB
/
LinuxDNSChange
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
69
70
import re
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException
from paramiko.ssh_exception import SSHException
from netmiko.ssh_exception import AuthenticationException
def main_def():
username = input("Enter your username: ")
password = input("Enter you password: ")
dns = str(input("Enter DNS server: "))
with open('devices') as f:
devices_list = f.read().splitlines()
for d in devices_list:
linux = {
'device_type': 'linux',
'ip': str(d),
'username': str(username),
'password': str(password),
'port': 22,
'verbose': True
}
print("*" * 80, "\n")
try:
connection = ConnectHandler(**linux)
except NetMikoTimeoutException:
print("*" * 100)
print(f'Timeout to device {d}:')
continue
except AuthenticationException:
print("*" * 100)
print(f'Authentication failure device {d}:')
continue
except SSHException:
print("*" * 100)
print(f'SSH Issue. Are you sure SSH is enabled device {d}')
continue
except Exception as unknown_error:
print("*" * 100)
print(f'Other error device {d} : ' + str(unknown_error))
continue
pass
output = connection.send_command('cat /etc/resolv.conf')
ip = re.search(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", output)
parsedint = ip.group(0)
print("*" * 80, "\n")
print("Contains of etc/resolf.conf: \n ")
output = connection.send_command('cat /etc/resolv.conf')
print(output)
if parsedint != dns:
print("incorect DNS entry")
print(parsedint)
print("*" * 80, "\n")
change = (input("Do you wish to change the entry? : Y/N "))
if change == "Y" or change == "y":
change_dns = connection.send_command(f"sed -i 's/{parsedint}/{dns}/g' /etc/resolv.conf")
print("DNS Entry changed")
output = connection.send_command('cat /etc/resolv.conf')
print(output, "\n")
else:
print("System exit")
else:
print("*" * 80, "\n")
print("DNS entry is correct")
if __name__ == "__main__":
main_def()