Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh connection #2

Open
cemaktas opened this issue Feb 7, 2014 · 1 comment
Open

ssh connection #2

cemaktas opened this issue Feb 7, 2014 · 1 comment

Comments

@cemaktas
Copy link

cemaktas commented Feb 7, 2014

I have use following code for ssh connection to HP Procurve switch

import paramiko

def readAndConnect(user, password, tftpRoot, tftp, hostFile, continueBannerSwitchListExist="no"):

#open switch list file for reading
hostFile = open(hostFile, "r")

#read each host and perform actions, sequentially
for host in hostFile:

    #remove newlines from host
    host = host.strip(' \t\n\r')
    print "\tConnecting to " + host + "..."

    #connect to host via ssh
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host,username=user ,password=password)
        chan = ssh.invoke_shell()
    except:
        print "\n\tCould not establish session with " + host + "\n"
        break
    #if continue banner on switch, wait for it, then send newline (enter)
    if continueBannerSwitchListExist == "yes":
            # any character send trough channel to bypass the banner
        chan.send('a')
    #create string with config download command
    download = "copy running-config tftp %s %s.txt \n" % (tftp, host)
    chan.send(download)
           #wait to download
    time.sleep(3)
    #print status message
    print "\n\tDownloading running-config from " + host + "..."
    print "\tDone\n"

    #disconnect from switch
    ssh.close()

hostFile.close()
@agreenbhm
Copy link
Owner

That's awesome! Thanks for your help. I'll review and merge asap.
On Feb 7, 2014 6:50 AM, "cemaktas" notifications@github.com wrote:

I have use following code for ssh connection to HP Procurve switch

import paramiko

def readAndConnect(user, password, tftpRoot, tftp, hostFile,
continueBannerSwitchListExist="no"):

#open switch list file for reading
hostFile = open(hostFile, "r")

#read each host and perform actions, sequentially
for host in hostFile:

#remove newlines from host
host = host.strip(' \t\n\r')
print "\tConnecting to " + host + "..."

#connect to host via ssh
try:
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host,username=user ,password=password)
    chan = ssh.invoke_shell()
except:
    print "\n\tCould not establish session with " + host + "\n"
    break
#if continue banner on switch, wait for it, then send newline (enter)
if continueBannerSwitchListExist == "yes":
        # any character send trough channel to bypass the banner
    chan.send('a')
#create string with config download command
download = "copy running-config tftp %s %s.txt \n" % (tftp, host)
chan.send(download)
       #wait to download
time.sleep(3)
#print status message
print "\n\tDownloading running-config from " + host + "..."
print "\tDone\n"

#disconnect from switch
ssh.close()

hostFile.close()

Reply to this email directly or view it on GitHubhttps://github.com//issues/2
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants