#!/bin/sh
set -x
#########################################################################
# This script will export a listing of all devices and where each device
# lives (GlobalCollection speaking).
# This list can then be used as a reference.
#########################################################################
# USE AT YOUR OWN RISK
#########################################################################
#
# ALTHOUGH THIS SCRIPT WORKED IN THE ORIGINAL ENVIRONMENT
# WHERE IT WAS WRITTEN IT IS NOT GUARANTEED TO WORK IN YOUR
# ENVIRONMENT. THEREFORE, USE THIS SCRIPT AT YOUR OWN RISK.
# CA TECHNOLOGIES ASSUMES NO LIABILITY AS A RESULT OF RUNNING
# THIS SCRIPT. THIS SCRIPT IS RUN AT THE SOLE DISGRESSION
# AND RISK OF THE USER OR COMPANY RUNNING THIS SCRIPT.
# HCL (c) 2023
#
# WE SUGGEST THAT YOU BACKUP YOUR DATABASE BEFORE RUNNING ANY SCRIPT
# WHICH ACCESSES THE SSDB DATABASE. IT WOULDN'T BE A BAD IDEA TO RUN
# A DDMDB BACKUP AS WELL. YOU CAN'T HAVE TOO MANY BACKUPS :-)
#
##########################################################################
#
# You will need to verify the SPECROOT location and modify if necessary.
# You will also need to create a dir under /vnmsh called "MyScripts"
#
###################### MAIN ##########################
host=$1
SPECROOT=/es/Spectrum/DX-Spectrum
DATE=`date +%Y%m%d`
CLISESSID=$$
CLIMNAMEWIDTH=48
CLIPATH=${SPECROOT}/vnmsh
WORKPATH=${SPECROOT}/vnmsh/MyScripts
DEVICEGCPAIR=${WORKPATH}/Dev_and_GC_Pair.legacy ## Old Server Location
GC_NEWLIST=${WORKPATH}/GC_NewList.list ## This file in on New SS
NEWDEVICES=${WORKPATH}/NewEnvDevices.txt ## This file is on New SS
GCIMPORT=${WORKPATH}/Mh_IP_Mn_GCTopo.OUT
NEWDEVWIP=${WORKPATH}/NewDevicesWithIPAdd.list
export SPECROOT DATE CLISESSID CLIMNAMEWIDTH CLIPATH WORKPATH
export DEVICEGCPAIR GC_NEWLIST NEWDEVICES GCIMPORT NEWDEVWIP DEVEXPORT
######### move old file to BAK ###########
if [ -a "${DEVICEGCPAIR}" ]
then
mv ${DEVICEGCPAIR} ${DEVICEGCPAIR}.BAK
fi
if [ -a "${GC_NEWLIST}" ]
then
mv ${GC_NEWLIST} ${GC_NEWLIST}.BAK
fi
if [ -a "${NEWDEVICES}" ]
then
mv ${NEWDEVICES} ${NEWDEVICES}.BAK
fi
${CLIPATH}/connect $host
read -p "Press Enter to continue...."
######### GETTING A LISTING OF ALL GLOBAL COLLECTIONS #################
${CLIPATH}/show models mth=0x10474 |awk ' NR==1 {next;}; { print $1, $2} ' > ${GC_NEWLIST}
## GC_NEWLIST Format: $1= 0x123456 (mh of GC) $2 = CollectionModelNameString, separated by ":"
## A sample line might look like this: 0x2003b6c Default Domain:GC_snmp:Jump Servers:VM Hosts
## ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
## MHandle |> starting here is the Collection list
## NOTICE that the above list has only TWO columns. The first Collection "Default Domain" just
## has a space in it, as well as other Collections in the list.
########## GETTING A LIST OF ALL DEVICES FROM THE SS ################
${CLIPATH}/show devices |awk ' NR==1 {next;}; { print $1, $2} ' > ${NEWDEVICES}
## NEWDEVICES Format: $1=<ModelHandle> (MH) $2=<Model Name> (MN)
#### A sample line output is: 0x10027ef London_Core_Router_18
## I also need the IP address of the device, which is not provided in the previous step.
## I therefore am taking the file I just created and using it as an INPUT file to
## using CLI, then creating a file with all three attributes.
while read MH MN;
do
IPADD=`${CLIPATH}/show attributes attr=0x12d7f mh=${MH} | awk 'NR==1 {next;}; {print $3}'`
echo ${MH}"|"${IPADD}"|"${MN} >> ${NEWDEVWIP}
done < ${NEWDEVICES}
## The output of this will produce a file, each line containing the
## Model Handle (MH), the IP ADDRESS and the Model Name (MN) of each
## device in the database.
## The MH is the unique identifier for that device. All CLI activities
## require the MH of the model (whether it's a device or not) to do some
## type of "thing" on it, whether it is to change an attribute, like the
## Name of the device, or other.
while read NEWMH NEWIPADD NEWMN;
do
cat ${DEVICEGCPAIR} |awk '/${NEWIPADD}/ && /${NEWMN}/'
GCLEG=`echo $LINE | awk '{print $3}' FS='|'`
while read GCLINE;
do
GC01=`echo ${GCLINE} |awk '{print $1}' FS=':'`
echo ${GC01} # echo used in troubleshooting code.
awk -v GC01= "$GC01"
awk 'BEGIN{if(GC01=="");break 1;}'
cat ${GC_NewList} |awk '/${GC01}/'|while read GCMH GCMN
do
${CLIPATH}/create association rel=staticGlobalCollects lmh=${GCMH} rmh=${NEWIPADD};
done
done < ${GCLEG}
done < ${NEWDEVWIP}
${CLIPATH}/disconnect
################## DETAILS BELOW ###################
Hopefully I am reading the $NEWDEVWIP file, and populating the values of "NEWMH" "NEWIPADD" and "NEWMN" using the while read statement.
Now that I have the NEWMH, NEWIPADD, and NEWMN in tact I am catting the $DEVICEGCPAIR file, which is a file copied from another server with the following format: "MHandle"|"IP Add"|"ModelName"|"Collection String"
A sample line would look like:
0x10027ef|268.900.420.150|London_Core_Router_18|Default Domain:GC_snmp:Jump Servers:VM Hosts
I am finding the line in the $DEVICEGCPAIR file that has the same IP and Name as my "filter" and then pulling the Collection Name String.
Since the Collection Name String could have 0, 1, or even 40 Collections in that string (separated by a ":") I need to pull each Collection out and assign the string text to a variable, GC##, where ## could be 01 through 99 (although I've never seen devices being members of more than 20 or 30 Collections.
I then take each GC Name String and create an "association" using CLI. Once I do that the device model I just did the "create association" command with now lives in that Collection.
I check to see if the GC## has a value (because many devices do not live in any Collection) by using the awk 'BEGIN...' line. IF the GC## is blank or null, I need to exit out of that one loop and read in the next $DEVICEGCPAIR line.
In the above script I am only showing GC01, but I plan on copying/pasting this same code up to 30 times in case there are 30 Collections that a device is a member of. IF ANYONE KNOWS HOW BEST TO DO THAT I WOULD REALLY APPRECIATE IT.
I know there's a way to create a count++, but I have no idea how I would do that. So, until then :-) I am creating 30 individual GC## loops. That's why I wanted to know how to break out of the loop, because I don't want to go through all 30 iterations if I really only needed to go through 3.