New to pyez need info on scrpiting

236 views
Skip to first unread message

Budoor Nagaraju

unread,
Feb 23, 2018, 4:09:31 AM2/23/18
to Junos Python EZ
Hi

New to Junos pyez , my requirement is to move the interface to the vlans , have studied couple of document but unable to succeed here is the below script can someone help me ?

 import sys
from sys import argv
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from jnpr.junos.exception import *
from jnpr.junos.op.ethport import EthPortTable
from pprint import pprint
import getpass
import os

dict = {}
dict['ip1'] = "host1"

target_host = raw_input('device name: ')
target_interface = raw_input('Interface name: ')
vlan=raw_input('vlan id: ')
dev = Device(host=target_host, user='user1', password='password', interface=target_interface)
dev.open()
change=Config(dev)
#vlan=argv[1]
cmd=("delete interface $target_interface")
cmd=("set interfaces 'target_interface' unit 0 family ethernet-switching vlan members 'vlan'")
change.load(cmd, format ='set')
print 'diff from junos device:'
print change.pdiff()
change.commit()
dev.close()
exit()


Nitin Kr (India)

unread,
Feb 23, 2018, 4:15:14 AM2/23/18
to Budoor Nagaraju, Junos Python EZ

Change below line

 

cmd=("set interfaces 'target_interface' unit 0 family ethernet-switching vlan members 'vlan'")

 

 

to

 

cmd="set interfaces {0} unit 0 family ethernet-switching vlan members {1}".format(target_interface, vlan)

 

 

Thanks

Nitin Kr

--
You received this message because you are subscribed to the Google Groups "Junos Python EZ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
junos-python-...@googlegroups.com.
Visit this group at
https://groups.google.com/group/junos-python-ez.
To view this discussion on the web visit
https://groups.google.com/d/msgid/junos-python-ez/d3b7aa01-fcef-4820-89fd-2b8149c3bf3a%40googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.

Nagaraju Budur

unread,
Feb 23, 2018, 4:36:52 AM2/23/18
to Nitin Kr (India), Junos Python EZ
Modified  getting the below error.

 python vlan.py

Traceback (most recent call last):
  File "vlan.py", line 3, in <module>
    from jnpr.junos import Device
  File "/usr/lib/python2.7/site-packages/jnpr/junos/__init__.py", line 1, in <module>
    from jnpr.junos.device import Device
  File "/usr/lib/python2.7/site-packages/jnpr/junos/device.py", line 18, in <module>
    from ncclient import manager as netconf_ssh
  File "/usr/lib/python2.7/site-packages/ncclient/manager.py", line 22, in <module>
    from ncclient import operations
  File "/usr/lib/python2.7/site-packages/ncclient/operations/__init__.py", line 16, in <module>
    from ncclient.operations.rpc import RPC, RPCReply, RPCError, RaiseMode
  File "/usr/lib/python2.7/site-packages/ncclient/operations/rpc.py", line 20, in <module>
    from ncclient.transport import SessionListener
  File "/usr/lib/python2.7/site-packages/ncclient/transport/__init__.py", line 18, in <module>
    from ncclient.transport.ssh import SSHSession
  File "/usr/lib/python2.7/site-packages/ncclient/transport/ssh.py", line 26, in <module>
    import paramiko
  File "/usr/lib/python2.7/site-packages/paramiko/__init__.py", line 22, in <module>
    from paramiko.transport import SecurityOptions, Transport
  File "/usr/lib/python2.7/site-packages/paramiko/transport.py", line 38, in <module>
    from paramiko.auth_handler import AuthHandler
  File "/usr/lib/python2.7/site-packages/paramiko/auth_handler.py", line 48, in <module>
    from paramiko.ssh_gss import GSSAuth, GSS_EXCEPTIONS
  File "/usr/lib/python2.7/site-packages/paramiko/ssh_gss.py", line 54, in <module>
    GSS_EXCEPTIONS = (gssapi.GSSException,)
AttributeError: 'module' object has no attribute 'GSSException'
[root@cephc network-python-scripts]#


On Fri, Feb 23, 2018 at 2:45 PM, Nitin Kr (India) <nit...@juniper.net> wrote:

Change below line

 

cmd=("set interfaces 'target_interface' unit 0 family ethernet-switching vlan members 'vlan'")

 

 

to

 

cmd="set interfaces {0} unit 0 family ethernet-switching vlan members {1}".format(target_interface, vlan)

 

 

Thanks

Nitin Kr

To unsubscribe from this group and stop receiving emails from it, send an email to junos-python-ez+unsubscribe@googlegroups.com.
Visit this group at
https://groups.google.com/group/junos-python-ez.


To view this discussion on the web visit

Nitin Kr (India)

unread,
Feb 23, 2018, 4:42:43 AM2/23/18
to Nagaraju Budur, Junos Python EZ
Check this out


Try to fix by some options given here. Else I can help

Thanks
Nitin Kr

Sent from mobile, please excuse my brevity

Nagaraju Budur

unread,
Feb 23, 2018, 4:47:56 AM2/23/18
to Nitin Kr (India), Junos Python EZ
Tried all the options ,still facing issues.

Thanks,
Nagaraju

Nagaraju Budur

unread,
Feb 25, 2018, 11:14:20 PM2/25/18
to Nitin Kr (India), Junos Python EZ
Uninstalled below package and now able to execute but getting error,

yum remove python-gssapi.x86_64


Error :

device name: sw1
Interface name: ge-0/0/46
vlan id: 888

Traceback (most recent call last):
  File "vlan.py", line 26, in <module>

    change.load(cmd, format ='set')
  File "/usr/lib/python2.7/site-packages/jnpr/junos/utils/config.py", line 533, in load
    ignore_warning=ignore_warning)
  File "/usr/lib/python2.7/site-packages/jnpr/junos/utils/config.py", line 454, in try_load
    raise ConfigLoadError(cmd=err.cmd, rsp=err.rsp, errs=err.errs)
jnpr.junos.exception.ConfigLoadError: ConfigLoadError(severity: error, bad_element: interfacesge-0/0/46unit, message: error: syntax error)
[root@cephc network-python-scripts]#


Script:

[root@cephc network-python-scripts]# cat vlan.py
import sys
from sys import argv
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
from jnpr.junos.exception import *
from jnpr.junos.op.ethport import EthPortTable
from pprint import pprint
from jnpr.junos.resources.interface import InterfaceTable

import getpass
import os

dict = {}
dict['ip1'] = "host1"

target_host = raw_input('device name: ')
target_interface = raw_input('Interface name: ')
vlan=raw_input('vlan id: ')
dev = Device(host=target_host, user='test', password='password', interface=target_interface)
dev.open()
change=Config(dev)

cmd=("delete interface $target_interface")
cmd="set interfaces{0}unit 0 family ethernet-switching vlan members{1}".format(target_interface,vlan)
change.load(cmd, format ='set')
print 'diff from junos device:'
print change.pdiff()
change.commit()
dev.close()
exit()

Thanks,
Nagaraju


 

Stacy W. Smith

unread,
Feb 26, 2018, 10:40:39 AM2/26/18
to Nagaraju Budur, Nitin Kr (India), Junos Python EZ

On Feb 25, 2018, at 9:14 PM, Nagaraju Budur <rajub...@gmail.com> wrote:
jnpr.junos.exception.ConfigLoadError: ConfigLoadError(severity: error, bad_element: interfacesge-0/0/46unit, message: error: syntax error)

There are now spaces between interfaces, ge-0/0/46,and unit in the configuration you generated.

cmd="set interfaces{0}unit 0 family ethernet-switching vlan members{1}".format(target_interface,vlan)

This explains why the generated configuration had no spaces. There are no spaces in your format string. Change the above line to:

cmd="set interfaces {0} unit 0 family ethernet-switching vlan members {1}".format(target_interface,vlan)

—Stacy

Reply all
Reply to author
Forward
0 new messages