ksator / junos_automation_with_netmiko

Examples of Netmiko content to automate Junos
MIT License
6 stars 11 forks source link

Configuring juniper devices (default factory) through a console server #1

Open AJNOURI opened 4 years ago

AJNOURI commented 4 years ago

Hi @ksator ,

Great stuff, keep it up.

I have hard time configuring juniper devices (default factory) through a console server (MRV).

Context:

I need to configure new factory default juniper EX/QFX switches.

The new devices connected with console port to Terminal server (MRV LX) asynchronous ports. The TS (MRV) is reachable only by SSH and inside, asynchronous ports are associated to SSH and telnet sockets (ports) as shown below:

InReach:0 >show port async summ
Time:                                       Sat, 09 Nov 2019 15:18:58 Euro

Port       Port Name        Access    Speed  TCP Port  SSH port     Device
  1  device01        Remote     9600     2100      2122      /dev/ttyGN0 
  2  device02        Remote     9600     2200      2222      /dev/ttyGN1 
  3  device03        Remote     9600     2300      2322      /dev/ttyGN2 
  4  device04        Remote     9600     2400      2422      /dev/ttyGN3 
...

 11  device11        Remote     9600     3100      3122     /dev/ttyGN10 
 12  device21        Remote     9600     3200      3222     /dev/ttyGN11 
...
Type a key to continue, q to quit
InReach:0 >

Knowing that I can SSH connect to the device socket as follow:

$ssh user@terminal_server -p 3122    <<<<<<<<<<<<<<< login to the ts (on the proper async port)
Welcome to the MRV Communications' LX Series Server
Port 11 Speed 9600

"ATTENTION:
...mod...
QFX1 (ttyu0)

login:        <<<<<<<<<< login to the new QFX

Python Pyez: According to the Pyez docs:

https://www.juniper.net/documentation/en_US/junos-pyez/topics/task/program/junos-pyez-connection-methods.html

https://github.com/Juniper/py-junos-eznc/releases/tag/2.3.0

This can be done, by the below script :


import sys
from getpass import getpass
from jnpr.junos import Device
from jnpr.junos.exception import ConnectError

hostname = input("Console server hostname: ")
cs_username = input("Console server username: ")
cs_password = getpass("Console server or SSH key password: ")
junos_username = input("Junos OS username: ")
junos_password = getpass("Junos OS password: ")

try:
    with Device(host=hostname, user=junos_username, passwd=junos_password, 
            cs_user=cs_username, cs_passwd=cs_password, timeout=6) as dev:   
        print (dev.facts)
except ConnectError as err:
    print ("Cannot connect to device: {0}".format(err))
    sys.exit(1)
except Exception as err:
    print (err)
    sys.exit(1)

Result:

ConnectRefusedError: ConnectRefusedError(terminal_server)

Any ideas?

vnitinv commented 4 years ago

@AJNOURI can you add these 2 lines in your script and share the logs

import logging
logging.basicConfig(level=logging.DEBUG)