Automate It – Gigamon Backups

In this post we’ll look at automation and management of Gigamon backup files using Python.  We’re going to whip up  two separate scripts using Python to pass a couple commands to each of the H-series and 2404 models of Gigamon appliances in your network.  This script will log into each of the appliances and tell them to upload the config to an SCP server.

What will you need?

First, you’re going to need a Linux box running Python 2.7.  This particular script makes use of pexpect which will need to be run from within a Bash prompt.  You will also need an SCP server to collect the configs.  Finally, you will need to provide two files as lists of hostnames of devices you would like to backup.  These files should be named gmnlist_h_series.txt and gmnlist_2404.txt.

H-Series Script

#!/usr/bin/env python

import pexpect
import fileinput
import time
import argparse

#assign arguments
parser = argparse.ArgumentParser(description='This script runs the backup operation for Gigamons.  It requires a file called gmnlist_h_series.txtnto be in the directory from which it's run.')
parser.add_argument('-u', help='SCP Username')
parser.add_argument('-s', help='SCP Server')
parser.add_argument('-p', help='SCP Password')
args = parser.parse_args()
arg_username = args.u
arg_password = args.p

#assign variables to aruguments
scp_server = args.s
scp_username = args.u
scp_password = args.p

#main function to backup config file for H series
def run_h():
	try:
		child = pexpect.spawn ('ssh %s' % line) 
		child.expect ('Password:')
		child.sendline ('%srn' % scp_password)
		child.expect ('w+ >')
		child.sendline ('enrn')
		child.expect ('w+ #')
		child.sendline ('config trn')
		child.expect ('w+ #')
		child.sendline ('config upload active scp://%s:%s@%s/home/%s/%s.cfgrn' % (scp_username, scp_password, scp_server, scp_username, line) ) 
		child.expect ('w+ #')
		child.sendline ('quit')
		print line, 'successful'
	except:
		print line, 'failed'

#read the file gmnlist_h_series.txt		
for line in fileinput.input(['gmnlist_h_series.txt']):
	line = line.rstrip('n')
	run_h()

2404 Script

#!/usr/bin/env python
 
import pexpect
import fileinput
import time
import argparse
 
#assign arguments
parser = argparse.ArgumentParser(description='This script runs the backup operation for Gigamons.  It requires a file called gmnlist_2404.txtnto be in the directory from which it's run.')
parser.add_argument('-u', help='SCP Username')
parser.add_argument('-s', help='SCP Server')
parser.add_argument('-p', help='SCP Password')
args = parser.parse_args()
arg_username = args.u
arg_password = args.p
 
#assign variables to aruguments
scp_server = args.s
scp_username = args.u
scp_password = args.p
 
#main function to backup config file of 2404s
def run_2404():
  try:
		child = pexpect.spawn ('ssh %s' % line) 
		child.expect ('password:')
		child.sendline ('%srn' % scp_password)
		child.expect ('w+>')
		child.sendline ('upload -cfg %s.cfg scp:%s@%srn' % (line, scp_username, scp_server) ) 
		child.expect ('password:')
		child.sendline ('%srn' % scp_password)
		child.expect ('w+>')
		child.sendline ('quit')
		print line, 'successful'
	except:
		print line, 'failed'
 

#read the file gmnlist_2404.txt		
for line in fileinput.input(['gmnlist.txt']):
	line = line.rstrip('n')
	run_2404()

This Post Has One Comment

  1. John

    I do the same thing, but I use clogin:

    $ clogin -c ‘upload -cmd -running -console’ mygigamon > mygigamon.running.config

    that’s for a GigaVue-420 or 2404. The -c flag just tells clogin to execute whatever string follows. Obviously, you can send a different string to an H-Series to backup that configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *