Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Python - Paramiko - Line has Invalid autocommand

I am trying to pull the running configuration of a device using paramiko. While I get the output of all the commands like sh cdp neigh, sh int status quite easily, whenever I try to pull the running configuration from the Cisco device - it gives me 'Line has an invalid command'. This is true in the case of direct command - 'sh run' and also 'sh run interface ' command. Here is the code which I have used. It would be great if someone can help. I have searched everywhere, but I am no closure to resolving this.

import atexit
import paramiko
import os
import re


global sshdata
global hostname
global ips

ip = input("Enter the IP of the device you want to start from ")
username = input("Enter your username ")
password = input("Enter your password ")

class myssh:

    def __init__(self, host, user, password, port = 22):
        client = paramiko.SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(host, port=port, username=user, password=password)
        atexit.register(client.close)
        self.client = client

    def __call__(self, command):
        global sshdata,hostname,privilege,ips,interfaces,protocols

        stdin,stdout,stderr = self.client.exec_command(command)
        sshdata = stdout.readlines()

        if command == 'enable':
                stdin.write(password)
        elif command == 'sh priv':
                privilege = sshdata[1]
        elif command == 'sh run | in hostname':
                temp = sshdata
                temp = (''.join(temp))
                temp = (str(temp)).split()
                hostname = temp[1]

        else:
            #print (''.join(sshdata))
            output_file=open(hostname+'.txt',"a")
            output_file.write(str(command))
            output_file.write('\n\n')
            output_file.write(str(''.join(sshdata)))
            output_file.write('\n\n\n\n')

remote = myssh(ip,username,password)
remote('sh priv')

if privilege == 'Current privilege level is 1' :
  remote = myssh(ip,username,password)
  remote('enable')
  #print("*****************************I come here***********************")




remote = myssh(ip,username,password)
remote('ter len 0')

remote = myssh(ip,username,password)
remote('sh run')

Comments