0

I have file mikrotik.py

'ip route add dst-address={}/{} gateway={}'.format(destination,prefix,gateway)

and this some code

import os
    localfilepath = os.getcwd()
    staticDir = localfilepath+"/website/plugin/config/routing/static/"
    vendors = staticDir+"MIKROTIK.py"
    destination = 192.168.2.0
    prefix = 24
    gateway = 192.168.1.1
    x = execfile(vendors)
    print x

the result is

None

I want the result is

ip route add dst-address=192.168.2.0/24 gateway=192.168.1.1

When I use

x = open(vendors)
print x

the result is

'ip route add dst-address={}/{} gateway={}'.format(destination,prefix,gateway)

Thanks in advance

so finally i use eval(x)

and the result is ip route add dst-address=192.168.2.0/24 gateway=192.168.1.1, i dont know its the best way or no

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Adhy
  • 177
  • 1
  • 1
  • 10

1 Answers1

0

If I understand correctly, you only need simple string manipulation:

import os

destination = 192.168.2.0
prefix = 24
gateway = 192.168.1.1
vendors = 'ip route add dst-address={}/{} gateway={}'.format(destination,prefix,gateway)
print x

If you really want to put the string/format in a separate file, use text file, json file etc, but no .py file.

BTW, identification is important in python.

B.Gu
  • 1
  • so if i use text file or json file, what must i do – Adhy Mar 03 '19 at 03:22
  • Assuming a text file has content: `ip route add dst-address={}/{} gateway={}`, in the python snippet, read the text file content into `vendor_str`, then `vendors=vendor_str.format(destination, prefix, gateway)`. I don't why I do that :-) – B.Gu Mar 04 '19 at 21:13