添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
  • Elevate
  • Become A Member
  • bluedove84

    bluedove84 06-18-2020 03:00

    I am using Windows and  trying to run this python code as mentioned below but unable to get past this error:

    from jnpr.junos import Device
    ModuleNotFoundError: No module named 'jnpr'

    The script is pretty straightforward as outlined in document below:

    https://kb.juniper.net/InfoCenter/index?page=content&id=KB34524&cat=MX_SERIES&actp=LIST&showDraft=false

    In the following example, we are connecting to the address: 10.1.1.1, with the user root and Password: test123​ using Python Script:

    from pprint import pprint
    from jnpr.junos import Device
    from jnpr.junos.utils.start_shell import StartShell
    hostname= Device(host=' 10.1.1.1 ', user=' root ', password=' test123
    ' )
    ss = StartShell(hostname)
    ss.open()
    hostname.open()
    pprint(ss.run('cli -c "show version | no-more"'))       # <-- For regular CLI commands
    pprint(ss.run('cprod -A fpc0 -c "show route summary"')) # <-- For VTY commands
    hostname.close()
    ss.close()

    Save the above contents as ".py" file and run it using the following command:

    python3 filename.py

    You can also save the outputs to a file instead of printing them on the console:

    python3 filename.py> save logfile.txt

    Greetings,

    This error is because of reason that the module not installed and you are trying to use it.

    Use this link : https://www.juniper.net/documentation/en_US/junos-pyez/topics/task/troubleshooting/junos-pyez-import-errors-troubleshooting.html

    Hope this helps.

    Please mark " Accept as solution " if this answers your query.

    Kudos are appreciated too!

    Regards,

    Sharat Ainapur

    That is the first thing that I have tried but it didnt work.

    I have tried this on python3 and I get the following error:

    python3
    Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import jnpr.junos
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'jnpr'
    >>> import jnpr.junos
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'jnpr'

    I also used Pycharm but that fails on error too:

    line 2, in <module>
    from jnpr.junos import Device
    ModuleNotFoundError: No module named 'jnpr'

    Any help would be much appreciated.

    pip install junos-eznc
    Requirement already satisfied: junos-eznc in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (2.4.1)
    Requirement already satisfied: PyYAML>=5.1 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (5.3.1)
    Requirement already satisfied: transitions in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (0.8.1)
    Requirement already satisfied: netaddr in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (0.7.19)
    Requirement already satisfied: yamlordereddictloader in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (0.4.0)
    Requirement already satisfied: jinja2>=2.7.1 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (2.11.2)
    Requirement already satisfied: pyserial in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (3.4)
    Requirement already satisfied: lxml>=3.2.4 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (4.5.1)
    Requirement already satisfied: ntc-templates in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (1.5.0)
    Requirement already satisfied: pyparsing in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (2.4.7)
    Requirement already satisfied: ncclient>=0.6.3 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (0.6.7)
    Requirement already satisfied: scp>=0.7.0 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (0.13.2)
    Requirement already satisfied: paramiko>=1.15.2 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (2.7.1)
    Requirement already satisfied: six in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from junos-eznc) (1.15.0)
    Requirement already satisfied: MarkupSafe>=0.23 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from jinja2>=2.7.1->junos-eznc) (1.1.1)
    Requirement already satisfied: textfsm>=1.1.0 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from ntc-templates->junos-eznc) (1.1.0)
    Requirement already satisfied: setuptools>0.6 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from ncclient>=0.6.3->junos-eznc) (41.2.0)
    Requirement already satisfied: cryptography>=2.5 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from paramiko>=1.15.2->junos-eznc) (2.9.2)
    Requirement already satisfied: bcrypt>=3.1.3 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from paramiko>=1.15.2->junos-eznc) (3.1.7)
    Requirement already satisfied: pynacl>=1.0.1 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from paramiko>=1.15.2->junos-eznc) (1.4.0)
    Requirement already satisfied: future in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from textfsm>=1.1.0->ntc-templates->junos-eznc) (0.18.2)
    Requirement already satisfied: cffi!=1.11.3,>=1.8 in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from cryptography>=2.5->paramiko>=1.15.2->junos-eznc) (1.14.0)
    Requirement already satisfied: pycparser in c:\users\Jazz\appdata\local\programs\python\python38-32\lib\site-packages (from cffi!=1.11.3,>=1.8->cryptography>=2.5->paramiko>=1.15.2->junos-eznc) (2.20)

    There seems to be indeed any issue with the junos-eznc package. It worked for me with Python 3.7.3, then I upgraded to 3.8.3 and loaded the junos-eznc again, now I get the error message:

    >>> import jnpr.junos
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>

    [...]

    ModuleNotFoundError: No module named 'fcntl'

    I have managed to successfully connect to my router but using Linux not  Windows. think there is an issue with windows and jnpr.junos.

    My next query is how to I make my output look user friendly as its all fudged up.from pprint import pprint

    I also need to run command show chassis hardware to get s/no of unit but that is not showing either.

    Please anyone help.

    from pprint import pprint
    from jnpr.junos import Device
    from jnpr.junos.utils.start_shell import StartShell
    hostname= Device(host=1.1.1.1, user='root', password='eb08fb5d')
    ss = StartShell(hostname)
    ss.open()
    hostname.open()
    pprint(ss.run('cli -c "show version | no-more"')) # <-- For regular CLI commands
    pprint(ss.run('cprod -A fpc0 -c "show route summary"')) # <-- For VTY commands
    hostname.close()
    ss.close()

    Please execute this on your Windows environment:

    pip install git+ https://github.com/Juniper/py-junos-eznc.git


    After performing this it should work. At least it worked for me.

    It appears my code only runs on old python 2.7.8 if anything above 3 is used it fails.

    I have tried to run pip install git+ https://github.com/Juniper/py-junos-eznc.git but getting below error:

    ERROR: Error [WinError 2] The system cannot find the file specified while executing command git clone -q https://github.com/Juniper/py-junos-eznc.git 'C:\Users\Jazz\AppData\Local\Temp\pip-req-build-7sktmot3'
    ERROR: Cannot find command 'git' - do you have 'git' installed and in your PATH?

    Make sure that you've just installed Python 3 (doesn't matter if 3.7 or 3.8)

    At first, if not already present, install GIT for Windows:
    https://git-scm.com/download/win

    After that, reboot your device.

    Then try to upgrade pip:
    python -m pip install --upgrade pip


    After that execute the download of junos-eznc again:
    pip install git+ https://github.com/Juniper/py-junos-eznc.git

    After that it should work:

    > python
    Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import jnpr.junos
    >>>

    Many thanks Sir your solution with git has saved me.

    I have run this code now and it works however indentation is not great  with lots of \r\n etc

    (True,
    'cli -c "show version | no-more"\r'
    '\r\n'
    'Hostname: Test001-001\r\n'
    'Model: srx300\r\n'
    'Junos: 15.1X49-D150.2\r\n'
    'JUNOS Software Release [15.1X49-D150.2]\r\n'
    'root@Test001-001% ')
    (False,
    'cprod -A fpc0 -c "show route summary"\r'
    '\r\n'
    '================ scb ================\r\n'
    'Broken pipe\r\n'

    Any ideas how to get rid of all these \r\n to make out look bit pretty?

    please check if this helps you to get rid of these characters:

    https://stackoverflow.com/questions/13188244/python-get-rid-of-unicode-and-r-n-characters

    https://stackoverflow.com/questions/45383938/cant-delete-r-n-from-a-string/45383951

    Please be so kind and click to "Accept Solution" to my former post so that others can find it very quickly. Thank you very much.

    Hi Simon

    The startshell function actually returns a tuple with the first element as True/False and second element as the output of the command.

    You can try to print the second element of the tuple to print only the output.

    Also you can use the links suggested by F1ght3r to remove /r/n from the output.

    I have a requirement to logon to juniper devices using a port other than ssh 22 - This mean to ssh but using port say 8022 instead.

    How do I edit this code or has any one got some examples:

    from pprint import pprint
    from jnpr.junos import Device
    from jnpr.junos.utils.start_shell import StartShell
    hostname= Device(host=' 10.1.1.1 ', user=' root ', password=' test123
    ' )  ** port required is 8022 instead of 22**
    ss = StartShell(hostname)
    ss.open()
    hostname.open()
    pprint(ss.run('cli -c "show version | no-more"'))       # <-- For regular CLI commands
    pprint(ss.run('cprod -A fpc0 -c "show route summary"')) # <-- For VTY commands
    hostname.close()
    ss.close()

    Save the above contents as ".py" file and run it using the following command:

    python3 filename.py

    You can use the port parameter from the Pyez and that should work.

    hostname= Device(host=' 10.1.1.1 ', user=' root ', password=' test123 , port = 8022 ' )

    By default the port will be 22.

    Make sure that device also has the same port configuration for ssh otherwise the connection will be refused.

    Hope this helps.

    I get an error

    retry_on_signal(lambda: sock.connect(addr))
    TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

    This works fine if you make ssh connection using port 8022 though.

    Just checked. StartShell code is using port 22 only. This was done because, PyEZ use 830 port for netconf.

    Looks like startShell left it hard coded for 22. We might need to look into this.

    I think start shell uses the paramiko to access the shell and in the startshell code i do not see port number so you are right the port 22 is hardcoded. However if you directly use paramiko you may get the option for port. However i am not sure if the junos will allow the ssh from a different port number for an inbound connection.

    Yes, you are right, seems to be a known issue on the Web.

    As per my understanding something is blocking this on your device. This might as well be due to antivirus/firewall. Verify if the project settings has the module(jnpr) installed in PyCharm also .

    Refer this python forum thread for a few steps to resolve this. Bypassing the Firewall or running as admin might be of some help: https://python-forum.io/Thread-Import-Error-jnpr-junos

    Let me know if this works. Hope this helps.

    Please mark " Accept as solution " if this solves your query.

    Kudos are appreciated too!

    Regards,

    Sharat Ainapur