0

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.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
ripclawbr
  • 13
  • 3
  • You have the entire write content as a string literal... – TigerhawkT3 Aug 27 '15 at 03:40
  • possible duplicate of [how to use concatenate a fixed string and a variable in Python](http://stackoverflow.com/questions/18348717/how-to-use-concatenate-a-fixed-string-and-a-variable-in-python) – Moishe Lipsker Aug 27 '15 at 03:45
  • If a particular answer works,mark it as 'Answer'(the green tick mark) so that someone else experiencing similar problem could get to the solution directly. – Tony Roczz Aug 27 '15 at 04:33

3 Answers3

1

Let's take a look at the line with the problem:

file.write( "'set address '+'a'+(a+1)+' '+cleaned_ip_list[i]\n")

You are trying to write parts of the String interspersed with values that change (variables).

file.write( "set address " + a + (a+1) + " " + cleaned_ip_list[i] + "\n")
Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29
0

You are currently writing a single string literal rather than a concatenated string. I recommend using the format() method.

file.write('set address {}{} {}\n'.format(a, a+1, cleaned_ip_list[i]))

The curly brackets {} will be replaced with the corresponding arguments to format().

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0
file.write('set address ' + a +' '+ (a + 1) +' '+cleaned_ip_list[i] +'\n' )

I think it should look like that.