-2

I am writing a script to config a router basically it will be webpage and it will pass arguments to python. At the moment it is just writing to a file and playing with exscript to get it writing via ssh

For example this is toned version of the script after it receives it arguments from the webpage the following is run

python myscript.py 4 newfile.txt 

(4 and newfile are variables received from webpage)

then the script is like

from sys import argv

script, vlanno, filename = argv
target = open(Filename, 'w')
vlanapply = (int vlan %r) %svino
target.write(vlanapply)

ok so this works but the problem is with the output the number 4 repersents a vlan no

it comes out as int vlan '4'

then when I try apply this using exscript using conn.execute to the cisco device it throws up an invalid command error other non variable commands are working fine.

The problem is the command is int vlan 4 not int vlan '4'

how can i get rid of the ' '?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

3 Answers3

2

The %r string format uses repr. If the argument is a string, the result will be enclosed in quotes and escaped as it would appear in an interactive session.

You probably want %s instead, which uses str to format the argument. That will eliminate the quotes.

By the way, if you're using Python 2.7+ (or Python 3+), you may use a somewhat nicer syntax for writing to a file:

script, vlanno, filename = argv
with open(filename, 'w') as target:
    print >> target, 'int vlan', vlanno # or print('int vlan', vlanno, file=target) in Python 3
nneonneo
  • 171,345
  • 36
  • 312
  • 383
1

The problem is within the statement :

vlanapply = (int vlan %r) %svino

The %r prints the actual object that vino represents - in this case a string.

Replace the above line with something like :

vlanapply = 'int vlan %s'  % vino

This should work correctly.

Refer this question that provides a good description of the %r and %s directives in python

Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
0
int('4') should give you the integer 4 instead of the string '4'.
Hrishi
  • 7,110
  • 5
  • 27
  • 26