I'm starting on devops and Python coding (I'm a network engineer, not a developer) so I apologize in advance if my question is too basic.
I'm writing a code that translates a fully qualified domain name into a list of ip addresses, then the program should write those ips into a file, inserting each one of them in a different line, on top of that, each new line should also contain some pre-defined strings "network device commands".
Than, my code will take this file, connect (using NETCONF) to some network devices and execute the commands that are in the file.
Right now, my code is:
import os
import socket
from netconf.os import Device
from netconf.utils.config import Config
# Variables Declaration Section
domain = raw_input('Enter the domain name you want to resolve: ')
device_management = raw_input('Enter the device Management IP address: ')
device_user = raw_input('Enter the device admin account: ')
device_pass = raw_input('Enter the device admin account password: ')
# DNS Resolution Section
ip_list = list()
try:
ip_list = socket.gethostbyname_ex(domain)
print "Resolving addresess"
except socket.gaierror, err:
print "Domain resolution error, please check network connectivity"
ip_list = ()
if ip_list != ():
print "Domain name resolved"
else:
print "Error: List of IP address is empty"
exit()
# Initial list clean up section (ip_list contains ips and words, need to filter)
cleaned_ip_list = ip_list[2]
# Creating the Device Template Config file
file = open("device_config.txt", "w")
for i in range(len(cleaned_ip_list)):
a=None
file.write( "'set address '+'a'+(a+1)+' '+cleaned_ip_list[i]\n")
file.close()
The issue I have right now is in the file.write line, it's simple writing the line as is in the code and not inserting the variables and concatenating them with the string.
I tried several different combinations with no success.