0

Results not doesn't output . I get an error please help .

Can someone please tell me what's wrong with this ? I'm just starting out in programming. I can run the script but the results don't show. I get an error please help.

enter link description here

filein = open( 'IOS.j2' )
src = string.Template( filein.read() )

hostname = input("Enter the hostname: ")
subnet = input("Enter the third octet: ")
secret_type = input("Enter the secret_type: ")


d={ 'hostname':hostname, 'secret_type':secret_type }
result = src.substitute(d)

#print result

fileout = open( hostname + '.cfg', 'w' )
fileout.write(result)

Thanks for any help .

LogCat

Traceback (most recent call last): File "rtrtemplate.py", line 33, in result = src.substitute(d) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\string.py", line 132, in substitute return self.pattern.sub(convert, self.template) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\string.py", line 129, in convert self._invalid(mo) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\string.py", line 105, in _invalid (lineno, colno))

joanis
  • 10,635
  • 14
  • 30
  • 40
steve
  • 11
  • 2
  • 2
    Can you provide some code? So that we could understand it better. – Tupio Jul 16 '19 at 16:27
  • Sure thanks for the quick response I've included a link but here is the code in question . – steve Jul 16 '19 at 17:35
  • Any help with this would be much appreciated . – steve Jul 18 '19 at 16:06
  • Does the print statement work? – Tupio Jul 18 '19 at 16:35
  • Thanks for the reply again I get prompted for the additional parameters but I get this error message at the end . – steve Jul 19 '19 at 16:11
  • raceback (most recent call last): File "rtrtemplate.py", line 33, in result = src.substitute(d) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\string.py", line 132, in substitute return self.pattern.sub(convert, self.template) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\string.py", line 129, in convert self._invalid(mo) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\string.py", line 105, in _invalid (lineno, colno)) – steve Jul 19 '19 at 16:12
  • Add your template, if you can – Tupio Jul 20 '19 at 04:33

1 Answers1

0

Follow this guide.This should work.

Make sure your template is correctly formatted, dictionary labels match the template placeholders. If it persists post a comment.:)

Tupio
  • 432
  • 7
  • 19
  • Sorry about my very limited knowledge and thanks for the response so when you mean formatted correctly you are talking about this section for the results right ?#Substitute and store in the variable "result" d={ 'hostname':hostname, 'secret_type':secret_type } result = src.substitute(d) – steve Jul 22 '19 at 17:59
  • python rtrtemplate.py Enter the hostname: Enter the third octet: Enter the secret_type: Enter the enable_secret: Enter the radius_secret: Enter the name_server: Enter the domain_name: Enter the snooping: Enter the the path: Enter the priority: Enter the archive: Enter the username_secret: Enter the management_ip: Enter the gateway: Enter the ftp_secert: Enter the location: Enter the snmp_server: Enter the password_secert: Enter the ntp_server: Traceback (most recent call last): File "rtrtemplate.py", line 33, in result = src.substitute(d) – steve Jul 22 '19 at 21:56
  • So I changed the cfg template file for the imput to match $ for each entry and that seemed to work but when I started adding addtional line items. I got error on line 19 for results again. d={ 'hostname':hostname, 'subnet':subnet, 'nameserver':nameserver, 'enable secret':enablesecret, } result = src.substitute(d) . I'm probably missing something so simple thanks for any help . – steve Jul 23 '19 at 16:10
  • Thanks for posting that guide I think it's working now seems I had a lot of formatting problems in my cfg file and not the py thanks again :) – steve Jul 23 '19 at 18:50
  • Sorry I was offline for a few days. Happy that it worked. Consider accepting the answer. If you found it useful. Cheers. – Tupio Jul 26 '19 at 16:03