ktbyers / netmiko

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

SCP issues #265

Closed ktbyers closed 8 years ago

ktbyers commented 8 years ago

All issues besides these should be fixed: Will SCP operations both get and put work on windows? scp_conn for the SCP operation itself blindly trusts the SSH host key. need to add unit tests for SCP operation with SSH host keys. Get operation wouldn't overwrite existing file.

SideHustleDev commented 8 years ago

As mentioned in the scp example, disabling file copy confirmation is needed for it to work properly. I am running a similar script, but copying a file to an hp device with the Comware cli which doesn't have the ability to disable file copy confirmation. Is there anyway I can get my scp script to work?

from netmiko import ConnectHandler, SCPConn
from DEVICE_CREDS import hp_7500

def main():
    """ SCP transfer file to network device """
    ssh_conn = ConnectHandler(**hp_7500)
    scp_conn = SCPConn(ssh_conn)
    s_file = 'afile.txt'
    d_file = 'afile.txt'

    print "\n\n"

    scp_conn.scp_transfer_file(s_file, d_file)
    scp_conn.close()

    output = ssh_conn.send_command("Dir | inc afile")
    print ">> " + output + '\n'

    # Merge config
    print "Merging config...\n"
    output = ssh_conn.send_command("copy afile.txt current-configuration")
    print output

    #Verify change
    print "Verifying change...\n"
    output = ssh_conn.send_command("display current-configuration | inc vlan")
    print ">> " + output + '\n'

if __name__ == "__main__":
    main()

The script completes as normal without confirming. When I print the output of Merge config it displays - Copy flash:/afile.txt to flash:/current-configuration?[Y/N]: Without disabling copy confirmation, how can I pass the Y to the prompt?

ktbyers commented 8 years ago

@netmanchris Can you disable this prompt confirmation on Comware7 CLI? ....never mind Chris, I didn't read the original issue closely enough.

We would need to make a vendor specific driver for HP Comware for SCP. @ISuckAtPython I assume you are using comware7?

SideHustleDev commented 8 years ago

Yes Comware7 is what I am using

ktbyers commented 8 years ago

@ISuckAtPython I assume you would be willing to test this (if I build an SCP driver here)?

SideHustleDev commented 8 years ago

Sure

ktbyers commented 8 years ago

@ISuckAtPython Hmmmm, I didn't pay enough attention to what you posted above (my bad).

You should be able to do the following:

    # Merge config
    print "Merging config...\n"
    output = ssh_conn.send_command("copy afile.txt current-configuration")
    if 'Y/N' in output:
        output += ssh_conn.send_command("Y")
    print output

Note, when Netmiko 1_0 is released probably next week (and when you upgrade), you will need to change send_command to send_command_timing() for this operation.

ktbyers commented 8 years ago

Let me know if this works.

SideHustleDev commented 8 years ago

okay good to know for 1_0.

The change did not work, nothing new happened and I also tried this as well to no avail

if 'Y/N' in output:
        output += ssh_conn.send_command("Y")
ktbyers commented 8 years ago

@ISuckAtPython What does output show you? With the .send_command("Y").

SideHustleDev commented 8 years ago

I get "Copy flash:/afile.txt to flash:/current-configuration?[Y/N]:" still for both

ktbyers commented 8 years ago

@ISuckAtPython Can you show me what the config merge looks like when you do it manually?

Kirk

SideHustleDev commented 8 years ago

@ktbyers Thanks for all your help Kirk. I have since discovered it is actually not possible to copy a couple lines of config to a file to append the running config of a Comware switch like you can in Cisco IOS. HP doesn't quite support that functionality.

I stopped adjusting the script after I discovered that, but I think I started to get it to work by using: if '*Y/N*' in output:

SideHustleDev commented 8 years ago

@ktbyers

Having another issue. I want to use the FileTransfer method within scp_handler.py. However, currently it only supports Cisco devices. I am trying to use the methods for HP devices. I have had success using the SCPConn method with HP, but I would like to use FileTransfer to utilize the more advanced functions like checking if file exists, etc. It appears that using FileTransfer could work, but my script gets hung up when comparing the md5 because the command to do so is different on hp. Instead of verify /md5 it is md5sum <arg>. I was wondering if I could just adjust the def compare_md5(self, base_cmd='verify /md5'): to the HP command and have it work. Is this viable, thoughts?

ktbyers commented 8 years ago

Yes, it is probably viable. Obviously, you will be hard-coding that code to work with HP and not with Cisco.

The way it should be done ultimately is to make an HP specific class that inherits from FileTransfer and then overrides anything that you need to override. If you send me your working code (after you do this), I can probably do this.

Kirk