Python SSH script to log into Extreme switch, run command and save output

  • 13 December 2017
  • 8 replies
  • 4898 views

I have been trying to write a python script that logins to Extreme switch via SSH2, runs commands and shows output. I looked at Extreme's github page but can't seem to find it. Is there a example script that someone can share that login and runs commands. I also heard there is a better way to screen scrap Extreme.

Thanks
Damon

8 replies

Userlevel 1
I think using netmiko is easy for python SSH scripting.
Very simple example:
from netmiko import ConnectHandler extrm_X460 = { 'device_type': 'extreme', 'ip': '172.16.10.2', 'username': 'admin', 'password': 'verysecret', 'port' : 12322, # optional, defaults to 22 } net_connect = ConnectHandler(**extrm_X460) output = net_connect.send_command('show switch') print(output)
[/code]
or something like:
#with SSH login without password and authentication keys enabled
import subprocess
try:
out = subprocess.check_output(['printf "show config\n" | ssh -o ForwardX11=no -T admin@10.0.0.1'], shell=True)
except subprocess.CalledProcessError as e:
out = e.output
print out
exit(1)
print "Switch output: %s\n" % out
Userlevel 3
Also add a last line with:
[/code]main()[/code]But argparse needs to be imported from somewhere..
Userlevel 6
Yes, the script I provided only connects to a switch and exits, to print something you need to add more.
Just before xos.exit() enter lines for commands like:
print xos.cmd("show vlan")
Thanks!!! I will look into JSONRPC but since we have mixture of codes in out env I don't think it is going to work.

I tried the second code but when i run it on by Linux machine, it shows nothing. It does run without giving me any errors but no output. I have a test device with user "user12" and password "1password2" with ip "10.10.10.1". Here is how I edited the code. Can you please check and see if I missed something. I am not familiar with classes.

I also added a print in main and I don't see that either.

class SSH2EXOS:
def __init__(self, switch, user='user12',password='1password2'):
self.connected = True
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(switch,username=user,password=password)
stdin, stdout, stderr = self.client.exec_command("disable clipaging")
stdin.close()
def cmdFast(self,cmd,timeout=30):
stdin, stdout, stderr = self.client.exec_command(cmd)
stdin.close()
return cmd+"\n"+stdout.read()
def exit(self):
self.client.close()
self.connected = False
def exitwithsave(self,save):
if save == "Y":
print "Saving config"
try:
stdin, stdout, stderr = self.client.exec_command('save')
stdin.flush()
print stdout.read()
self.client.close()
self.connected = False
except:
print "except"
self.client.close()
self.connected = False

def isConnected(self):
if self.connected:
return True
else:
return False
def main(): print "Main"
parser = argparse.ArgumentParser(description='Connect to switch(es) and delete xsf files')
parser.add_argument("-u", dest="user", default="user12", help="Username")
parser.add_argument("-p", dest="password", default="1password2", help="Password, leave out for none")[/code] parser.add_argument("-s", dest="switch", default=None, help="10.10.10.1")
args = parser.parse_args()
mlagfilters = None
if args.switch:
print "Connecting to :",args.switch
xos = SSH2EXOS(checkIP(args.switch), user=args.user, password=args.password)
if xos.isConnected():
print "Connected."
info = xos.cmdFast("show switch")
for line in info.splitlines():
m = re.search(r'SysName:\s+(\S+)',line)
if m:
switchname = m.group(1)
xos.exit()[/code]
Userlevel 6
class SSH2EXOS:
def __init__(self, switch, user='admin',password=''):
self.connected = True
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(switch,username=user,password=password)
stdin, stdout, stderr = self.client.exec_command("disable clipaging")
stdin.close()

def cmdFast(self,cmd,timeout=30):
stdin, stdout, stderr = self.client.exec_command(cmd)
stdin.close()
return cmd+"\n"+stdout.read()

def exit(self):
self.client.close()
self.connected = False

def exitwithsave(self,save):
if save == "Y":
print "Saving config"
try:
stdin, stdout, stderr = self.client.exec_command('save')
stdin.flush()
print stdout.read()
self.client.close()
self.connected = False
except:
print "except"
self.client.close()
self.connected = False

def isConnected(self):
if self.connected:
return True
else:
return False

def main():
parser = argparse.ArgumentParser(description='Connect to switch(es) and delete xsf files')
parser.add_argument("-u", dest="user", default="admin", help="Username")
parser.add_argument("-p", dest="password", default="", help="Password, leave out for none")
parser.add_argument("-s", dest="switch", default=None, help="Switch IP address")
args = parser.parse_args()

mlagfilters = None
if args.switch:
print "Connecting to :",args.switch
xos = SSH2EXOS(checkIP(args.switch), user=args.user, password=args.password)
if xos.isConnected():
print "Connected."
info = xos.cmdFast("show switch")
for line in info.splitlines():
m = re.search(r'SysName:\s+(\S+)',line)
if m:
switchname = m.group(1)
xos.exit()
I can't use SecureCRT as it will be a web service. I was looking for just a script to SSH and run commands .. maybe paramiko/pextect? I could not find any example.

I tried something simple like this but it didn't work:

import pexpect
import sys

switch_ip = "10.10.10.1"
switch_un = "user"
switch_pw = "pass"

child = pexpect.spawn('ssh %s@%s' % (switch_un, switch_ip))
child.logfile = sys.stdout
child.timeout = 4
child.expect('Password:')
child.sendline(switch_pw)
child.expect('#')
Userlevel 6
If you're just trying to run a command or two, what about using NetSight. Or if you don't have that you might consider secure crt instead of putty. CRT would allow you to run commands on groups of switches.

Reply