cancel
Showing results for 
Search instead for 
Did you mean: 

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

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

Marvell_Kay
New Contributor II
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 8

Marvell_Kay
New Contributor II
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") 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()

OscarK
Extreme Employee
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()

Marvell_Kay
New Contributor II
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('#')

davidj_cogliane
Contributor
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.
GTM-P2G8KFN