Search code examples
pythonjunos-automationpyez

Executing vty commands in Juniper routers using PyEZ


I have a requirement where, a python script running in a Juniper router shell needs to execute some commands in vty console of the FPC. I cannot use vty ­c because it may not work properly in all platforms. However, I can use vty fpc0 and then execute the command and exit from there.

Is there a way to execute vty command using PyEZ? If yes, please provide the syntax.


Solution

  • Using PyEZ StartShell utility we can do something like

    from jnpr.junos.utils.start_shell import StartShell
    from jnpr.junos import Device
    
    dev = Device(host='xxxx', user='xxxx', password='xxxx')
    dev.open()
    
    with StartShell(dev) as ss:
        op = ss.run('vty fpc0', 'vty\)#')
        print op[1]
        op = ss.run('show version', 'vty\)#')
        print op[1]
    
    dev.close()
    

    or even

    dev = Device(host='xxxx', user='xxxx', password='xxxx')
    dev.open()
    
    with StartShell(dev) as ss:
        op = sh.run('cprod -A fpc0 -c "show version"')
        print op[1]
    
    dev.close()