ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.54k stars 1.3k forks source link

scp_handler.py process_md5() failed with linux ubuntu 22.04 lts #3209

Open zcy382725 opened 1 year ago

zcy382725 commented 1 year ago

log:

{'device_type': 'linux', 'host': 'some_ip', 'username': 'root', 'use_keys': True, 'key_file': 'KEY', 'ssh_config_file': './ssh_config'} Traceback (most recent call last): File "/builds/infra/sw_dump/dump.py", line 35, in output = file_transfer(net_connect, ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/netmiko/scp_functions.py", line 122, in file_transfer with TransferClass(*scp_args) as scp_transfer: ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/netmiko/ssh_dispatcher.py", line 503, in FileTransfer return FileTransferClass(args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/netmiko/linux/linux_ssh.py", line 179, in init super().init( File "/usr/local/lib/python3.11/site-packages/netmiko/scp_handler.py", line 112, in init self.remote_md5(remote_file=source_file) if hash_supported else None ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/netmiko/linux/linux_ssh.py", line 214, in remote_md5 dest_md5 = self.process_md5(dest_md5).strip() ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/netmiko/linux/linux_ssh.py", line 219, in process_md5 return super(LinuxFileTransfer, LinuxFileTransfer).process_md5( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/netmiko/scp_handler.py", line 359, in process_md5 raise ValueError(f"Invalid output from MD5 command: {md5_output}") ValueError: Invalid output from MD5 command: 4977085b132f104643c75695b8ba1af2 /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400030.unf

i've checked the scp_handler.py file and modified process_md5() def :

def process_md5(md5_output: str, pattern: str = r"=\s+(\S+)") -> str:
    """
    Process the string to retrieve the MD5 hash

    Output from Cisco IOS (ASA is similar)
    .MD5 of flash:file_name Done!
    verify /md5 (flash:file_name) = 410db2a7015eaa42b1fe71f1bf3d59a2
    """
    print(md5_output.split('\n'))
    md5_output=md5_output.split('\n')[-1]
    match = re.search(pattern, md5_output)
    if match:
        return match.group(1)
    else:
        raise ValueError(f"Invalid output from MD5 command: {md5_output}")

it seems some md5_output has a '\n' in begining(ubuntu22.04)

{'device_type': 'linux', 'host': 'some_ip', 'username': 'root', 'use_keys': True, 'key_file': 'KEY', 'ssh_config_file': './ssh_config'} /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400056.unf autobackup_7.2.94_20230514_2000_1684094400056.unf ['2674b3d0e1353125e6a0298024dddc37 /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400056.unf'] {'device_type': 'linux', 'host': 'some_ip', 'username': 'root', 'use_keys': True, 'key_file': 'KEY', 'ssh_config_file': './ssh_config'} /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400016.unf autobackup_7.2.94_20230514_2000_1684094400016.unf ['1b738b8a69ab968caecbdad112f92058 /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400016.unf'] {'device_type': 'linux', 'host': 'some_ip', 'username': 'root', 'use_keys': True, 'key_file': 'KEY', 'ssh_config_file': './ssh_config'} /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400030.unf autobackup_7.2.94_20230514_2000_1684094400030.unf ['122132d4a4b1b1f1f4ae727e7aea4ff0 /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400030.unf'] {'device_type': 'linux', 'host': 'some_ip', 'username': 'root', 'use_keys': True, 'key_file': 'KEY', 'ssh_config_file': './ssh_config'}

/root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400030.unf autobackup_7.2.94_20230514_2000_1684094400030.unf ['', '4977085b132f104643c75695b8ba1af2 /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400030.unf'] {'device_type': 'linux', 'host': 'some_ip', 'username': 'root', 'use_keys': True, 'key_file': 'KEY', 'ssh_config_file': './ssh_config'} /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400026.unf autobackup_7.2.94_20230514_2000_1684094400026.unf ['58fd1ca3ba475da91584efeaac636d3e /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400026.unf'] {'device_type': 'linux', 'host': 'some_ip', 'username': 'root', 'use_keys': True, 'key_file': 'KEY', 'ssh_config_file': './ssh_config'} /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400020.unf autobackup_7.2.94_20230514_2000_1684094400020.unf ['269953a481129a39ec5488ea2f7cfa32 /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400020.unf'] {'device_type': 'linux', 'host': 'some_ip', 'username': 'root', 'use_keys': True, 'key_file': 'KEY', 'ssh_config_file': './ssh_config'} /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400012.unf autobackup_7.2.94_20230514_2000_1684094400012.unf ['b54d1d571d60b700a8c8218fd886ee29 /root/services/unifi/config/data/backup/autobackup/autobackup_7.2.94_20230514_2000_1684094400012.unf']

zcy382725 commented 1 year ago

it seems that send_command() result also start with a "\n" in output in ubuntu2204

jaghent commented 1 month ago

it seems that send_command() result also start with a "\n" in output in ubuntu2204

Same for me on 22.04

I don't have enough hosts & distros to test on but stripping the raw output of md5sum fixed it for me.

From: https://github.com/ktbyers/netmiko/blob/ace6062c61376f609cb84a962d12459b7ce26ecd/netmiko/linux/linux_ssh.py#L214 To:

dest_md5 = self.process_md5(dest_md5.strip()).strip()