pyats icon indicating copy to clipboard operation
pyats copied to clipboard

Regex used to validate device hostname is not strict enough

Open sogloarcadius opened this issue 5 years ago • 11 comments

Hello,

I am using pyats and genie connection client in a python script to parse some commands output of network devices.

I realize that the regex used to validate the device hostname is not strict enough. In fact when the actual device hostname contains a substring of the hostname parameter the connection succeed but i am expecting it to fail.

For example when the actual device hostname is "demo-vrouter1" and i use con = Device("vrouter1").connect(), I am expecting the connection to fail because vrouter1 does not strictly match demo-vrouter1.

Do i miss a configuraton option to make hostname validation more strict ?

Thanks,

Best regards,

sogloarcadius avatar Dec 26 '19 10:12 sogloarcadius

Hi

Cisco is currently on Christmas shutdown - expect most team members to return by Jan 6. Will make sure our connection SME is on this issue when they return!

@difuhu fyi

simingy avatar Dec 27 '19 05:12 simingy

Hi @sogloarcadius ,

Could you share your testbed.yaml and/or detail attributes of Device("vrouter1")? Thanks.

difuhu avatar Jan 06 '20 21:01 difuhu

Hello @difuhu

Below the pyats version installed :

pyats : 19.12
genie : 19.12

Below the Python code i use to run the test

testbed = Testbed(hostname)
testbed.credentials=Credentials(dict(default=dict(
        username=username,
        password=password)))

dev = Device(hostname,
                 os=os,
                 custom={'abstraction': {'order': ['os']}},
                 connections={'default': dict(host=management_ip,
                                              arguments=dict(init_config_commands=[],
                                                             init_exec_commands=[]),
                                              protocol='ssh')})
testbed.add_device(dev)
dev.connect(log_stdout=False, connection_timeout=connection_timeout, learn_hostname=False)
dev.parse(command)

Thanks. Regards.

sogloarcadius avatar Jan 10 '20 10:01 sogloarcadius

Hi @sogloarcadius ,

Thanks for the detail info and code snippet. Yes, enhancement can be made to validate hostname stricter. Could you provide the os (plugin) you are using? We will enhance and test it.

difuhu avatar Jan 10 '20 15:01 difuhu

Hello @difuhu

I am using a cisco CSR1000v for testing (iosxe) but i think your fix should target all supported devices by the library.

Regards

Arcadius

sogloarcadius avatar Jan 14 '20 14:01 sogloarcadius

Hi @sogloarcadius , Thanks for the CSR1000V info. Yes, sure, we will try our best to fix all supported platforms.

difuhu avatar Jan 14 '20 14:01 difuhu

@sogloarcadius could you please validate if this was fixed in v20.1 release?

simingy avatar Feb 11 '20 14:02 simingy

@sogloarcadius could you please validate if this was fixed in v20.1 release?

Update: This request is tracked on our backlog but has not yet been released.

mdear avatar Feb 11 '20 21:02 mdear

Hi Sogloarcadius,

We currently run on v20.9, could you please upgrade your env to the latest version (better to install a fresh one) and retry?

Installation steps; https://developer.cisco.com/docs/pyats/api/

If the issue persists, please provide the reproduce steps and we will get back to you with a solution.

I have tried with the most recent version and the issue is not reproducible;

Welcome to pyATS Interactive Shell
==================================
Python 3.8.1 (default, Feb  6 2020, 10:50:57)
[Clang 11.0.0 (clang-1100.0.33.16)]

>>> from genie.testbed import load
>>> testbed = load('testbed.yaml')
-------------------------------------------------------------------------------
>>> uut = testbed.devices['demo-asr-MIB-2']
>>> uut.connect()

2020-10-13 19:23:59,568: %UNICON-INFO: +++ demo-asr-MIB-2 logfile /tmp/demo-asr-MIB-2-cli-20201013T192359567.logtestbed:
 +++

2020-10-13 19:23:59,568: %UNICON-INFO: +++ Unicon plugin iosxe +++
Trying 1.1.1.1...


2020-10-13 19:23:59,602: %UNICON-INFO: +++ connection to spawn: telnet 1.1.1.1 2043, id: 1212121212 +++

2020-10-13 19:23:59,603: %UNICON-INFO: connection to demo-asr-MIB-2
Connected to arf-tsisg-asr-ssr.cisco.com.
Escape character is '^]'.

Location: 163_88_9

asr-MIB-2>^CTraceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/karmoham/pyats_3.8.1/pypi/genie/src/genie/conf/base/device.py", line 632, in connect
    self.connectionmgr.connect(*args, **kwargs)
  File "/Users/karmoham/pyats_3.8.1/pypi/pyats/pkgs/connections-pkg/src/pyats/connections/manager.py", line 334, in connect
    output = connection.connect()
  File "/Users/karmoham/pyats_3.8.1/pypi/unicon/src/unicon/bases/connection.py", line 694, in connect
    output = self.connection_provider.connect()
  File "/Users/karmoham/pyats_3.8.1/pypi/unicon/src/unicon/bases/routers/connection_provider.py", line 189, in connect
    output = self.establish_connection()
  File "/Users/karmoham/pyats_3.8.1/pypi/unicon/src/unicon/bases/routers/connection_provider.py", line 211, in establish_connection
    output = con.state_machine.go_to('any',
  File "/Users/karmoham/pyats_3.8.1/pypi/unicon/src/unicon/statemachine/statemachine.py", line 719, in go_to
    output = transition.do_transitions()
  File "/Users/karmoham/pyats_3.8.1/pypi/unicon/src/unicon/statemachine/statetransition.py", line 487, in do_transitions
    m = dialog.process(self.spawn,
  File "/Users/karmoham/pyats_3.8.1/pypi/unicon/src/unicon/eal/dialogs.py", line 460, in process
    return dp.process()
  File "/Users/karmoham/pyats_3.8.1/pypi/unicon/src/unicon/eal/dialog_processor.py", line 299, in process
    if self.expect_eval_statements(pat) is True:
  File "/Users/karmoham/pyats_3.8.1/pypi/unicon/src/unicon/eal/dialog_processor.py", line 218, in expect_eval_statements
    if self.spawn.read_update_buffer():
  File "/Users/karmoham/pyats_3.8.1/pypi/unicon/src/unicon/eal/backend/pty_backend.py", line 139, in read_update_buffer
    time.sleep(0.01)
KeyboardInterrupt
>>> exit()

Thanks, Karim

karmoham avatar Oct 13 '20 20:10 karmoham

Hello,

The issue is not fixed in the latest release. As described in the issue when the configured hostname contains the string subset, the validation process with learn_hostname = False does not work.

Example: router is configured with hostname demo-vrouter1 and connecting to the device with hostname vrouter1 works which is not the expected behavior with learn_hostname=False.

Thanks,

Regards,

Arcadius.

sogloarcadius avatar Oct 14 '20 08:10 sogloarcadius

Hi Arcadius,

Thanks for reporting the issue, item has been added to our backlog and we will address it in the coming releases.

Thanks, Karim

karmoham avatar Oct 14 '20 19:10 karmoham