API of switches, Web app...

Post Reply
sincorchetes
Member
Posts: 6
Joined: 14 Feb 2018 15:58

API of switches, Web app...

Post by sincorchetes »

Good afternoon,

I just wanna do a script that send a single line one command into switch over SSH, but I cannot perform that, because only connect into the machine to let's start work with Alcatel devices.

I wanna get more information if Alcatel does have some API for web development or for this cases. Just Does It have?

Thanks!
silvio
Alcatel Unleashed Certified Guru
Alcatel Unleashed Certified Guru
Posts: 1886
Joined: 01 Jul 2008 10:51
Location: Germany

Re: API of switches, Web app...

Post by silvio »

most informations about API you will find in the switch management guides.
regards
Silvio
sincorchetes
Member
Posts: 6
Joined: 14 Feb 2018 15:58

Re: API of switches, Web app...

Post by sincorchetes »

silvio wrote: 15 Feb 2018 03:26 most informations about API you will find in the switch management guides.
regards
Silvio
I just found information in AOS Release 7 and 8 management guides that talks about APIs, but for AOS6850 I didn't find anything contains API's related in AOS Release 6 Management Guide.
devnull
Alcatel Unleashed Certified Guru
Alcatel Unleashed Certified Guru
Posts: 976
Joined: 07 Sep 2010 10:16
Location: Germany

Re: API of switches, Web app...

Post by devnull »

I don't think that the AOS6 based switches support netconf, but with
"perl Net::SSH::Expect" you can automate it for yourself. Just follow the CLI Guide
Otherwise setting parameters by snmp should work as well, you find the needed snmp Objects in the CLI guide as well.
User avatar
David_Klancar
Member
Posts: 12
Joined: 01 Dec 2017 04:56

Re: API of switches, Web app...

Post by David_Klancar »

Good afternoon,

I made a python script for sending SSH command (multiple command separated with ;) and save all outputs in a file. I use the pexpect module; please find the script if needed:

#!/usr/bin/python
# -*- coding: utf-8 -*-

##############################################################
# Script : Send_SSH_Command.py
# Author : David KLANCAR
# Date : 29/01/2018
# Update : 14/02/2018 : Add multiple commands
##############################################################

###########################################################

from datetime import timedelta
import time
import os
import re
import sys
import copy
import subprocess
import pexpect

from argparse import ArgumentParser, ArgumentError
from functools import wraps



######################################
#### Functions ####
######################################


# Argument parser
# My own ArgumentParser with single-line stdout output and unknown state Nagios retcode
class NagiosArgumentParser(ArgumentParser):
def error(self, message):
sys.stdout.write('UNKNOWN: Bad arguments (see --help): %s\n' % message)
sys.exit(3)
# Nagios unknown exit decorator in case of TB
def tb2unknown(method):
@wraps(method)
def wrapped(*args, **kw):
try:
f_result = method(*args, **kw)
return f_result
except Exception, e:
print 'UNKNOWN: Got exception while running %s: %s' % (method.__name__, str(e))
if debug:
raise
sys.exit(3)
return wrapped

# Arguments handler
@tb2unknown
def parse_args():
argparser = NagiosArgumentParser(description='MAC Table entries')
argparser_global = argparser.add_argument_group('Global')
argparser_global.add_argument('-H', '--host', type=str, required=True,
help='Hostname or address to query (mandatory)')
argparser_global.add_argument('-N', '--name', type=str , required=True,
help='Switch hostname')
argparser_global.add_argument('--sshlogin',type=str , default='admin',
help='user used for ssh session')
argparser_global.add_argument('--sshpassword',type=str , default='switch',
help='password used for ssh session')
argparser_global.add_argument('--clicommand',type=str , default='show',
help='CLI command to execute')
argparser_global.add_argument('--clitimeout',type=int , default=3,
help='CLI timeout')
argparser_global.add_argument('--filename',type=str , default='',
help='Word to add in the file name if needed')
argparser_global.add_argument('--outputfolder',type=str , default='/Temp/',
help='Output folder - Example "/Temp/"')
args = argparser.parse_args()
return args


@tb2unknown
def assure_path_exists(path):
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)

@tb2unknown
def sshSendCommands(IP,Login,Password,CLICOMMAND):
child = pexpect.spawn('ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no %s@%s' % (Login, IP))
# output
SessionOutput = ''
child.logfile = sys.stdout
child.timeout = 10
# Enter password or accept ssh key first
i = child.expect([pexpect.TIMEOUT, 'assword', 'yes/no'])
if i == 0:
print('SSH Time OUT!')
sys.exit(3)
if i == 1:
child.sendline(Password)
if i == 2:
child.sendline('yes')
child.expect('assword')
child.sendline(Password)
# Expect end of prompt and send command
i = child.expect(['#', '>'])
child.timeout = inputs.clitimeout
if i == 0 or i == 1:
for c in range(len(CLICOMMAND)):
child.sendline(CLICOMMAND[c])
i = child.expect([pexpect.TIMEOUT])
if i == 0:
SessionOutput = child.before + '\n\n'
# Exit connection
if i == 0:
child.sendline('exit')
return SessionOutput

######################################
#### Input ARGUMENTS ####
######################################


if __name__ == '__main__':
# Input arguments
debug = False

inputs = parse_args()
File = inputs.outputfolder + '/' + inputs.name + '_' + inputs.filename + '.log'
assure_path_exists(inputs.outputfolder)
CommandsToSend = []
if ';' in inputs.clicommand:
for i in range(len(inputs.clicommand.split(';'))):
CommandsToSend.append(inputs.clicommand.split(';'))
else:
CommandsToSend.append(inputs.clicommand)
sshoutput = sshSendCommands(inputs.host, inputs.sshlogin, inputs.sshpassword, CommandsToSend)
RefFile = open(File, 'w')
RefFile.write(str(sshoutput))
RefFile.close()
sys.exit(0)
Post Reply

Return to “Misc”