Skip to content
Advertisement

Serial Numbers from a Storage Controller over SSH

Background
I’m working on a bash script to pull serial numbers and part numbers from all the devices in a server rack, my goal is to be able to run a single script (inventory.sh) and walk away while it generates text files containing the information I need. I’m using bash for maximum compatibility, the RHEL 6.7 systems do have Perl and Python installed, however they have minimal libraries. So far I haven’t had to use anything other than bash, but I’m not against calling a Perl or Python script from my bash script.

My Problem
I need to retrieve the Serial Numbers and Part numbers from the drives in a Dot Hill Systems AssuredSAN 3824, as well as the Serial numbers from the equipment inside. The only way I have found to get all the information I need is to connect over SSH and run the following three commands dumping the output to a local file:

  • show controllers
  • show frus
  • show disks

Limitations:

  • I don’t have “sshpass” installed, and would prefer not to install it.
  • The Controller is not capable of storing SSH keys ( no option in custom shell).
  • The Controller also cannot write or transfer local files.
  • The Rack does NOT have access to the Internet.
  • I looked at paramiko, but while Python is installed I do not have pip.
  • I also cannot use CPAN.
  • For what its worth, the output comes back in XML format. (I’ve already written the code to parse it in bash)

Right now I think my best option would be to have a library for Python or Perl in the folder with my other scripts, and write a script to dump the commands’ output to files that I can parse with my bash script. Which language is easier to just provide a library in a file? I’m looking for a library that is as small and simple as possible to use. I just need a way to get the output of those commands to XML files. Right now I am just using ssh 3 times in my script and having to enter the password each time.

Advertisement

Answer

I ended up contacting the Manufacturer and asking my question. They said that the system isn’t setup for connecting without a password, and their SNMP is very basic and won’t provide the information I need. They said to connect to the system with FTP and use “get logs ” to download an archive of the configuration and logs. Not exactly ideal as it takes 4 minutes just to run that one command but it seems to be my only option. Below is the script I wrote to retrieve the file automatically by adding the login credentials to the .netrc file. This works on RHEL 6.7:

#!/bin/bash
#Retrieve the logs and configuration from a Dot Hill Systems AssuredSAN 3824 automatically.
#Modify "LINE" and "HOST" to fit your configuration.
LINE='machine <IP> login manage password <password>'
HOST='<IP>'
AUTOLOGIN="/root/.netrc"
FILE='logfiles.zip'
#Check for and verify the autologin file
if [ -f $AUTOLOGIN ]; then
    printf "Found auto-login file, checking for proper entry... r"
    READLINE=`cat $AUTOLOGIN | grep "$LINE"`
    #Append the line to the end of .netrc if file exists but not the line.
    if [ "$LINE" != "$READLINE" ]; then
        printf "Proper entry not found, creating it...                       r"
        echo "$LINE" >> "$AUTOLOGIN"
    else
        printf "Proper entry found...                                        r"
    fi
#Create the Autologin file if it doesn't exist
else
    printf "Auto-Login file does not exist, creating it and setting permissions...r"
    echo "$LINE" > "$AUTOLOGIN"
    chmod 600 "$AUTOLOGIN"
fi
#Start getting the information from the controller. (This takes a VERY long time)
printf "Retrieving Storage Controller data, this will take awhile...              r"
ftp $HOST << SCRIPT
get logs $FILE
SCRIPT
exit 0

This gave me a bunch of files in the zip, but all I needed was the “store_….logs” file. It was about 500,000 lines long, the first portion is the entire configuration in XML format, then the configuration in text format, followed by the logs from the system. I parsed the file and stripped off the logs at the end which cut the file down to 15,000 lines. From there I divided it into two files (config.xml and config.txt). I then pulled the XML output of the 3 commands that I needed and it to the 3 files my previously written script searches for. Now my inventory script pulls in everything it needs, albeit pretty slow due to waiting 4 minutes for the system to generate the zip file. I hope this helps someone in the future.

Edit:
Waiting 4 minutes for the system to compile was taking too long. So I ended up using paramiko and python scripts to dump output from the commands to files that my other code can parse. It accepts the IP of the Controller as a parameter. Here is the script for those interested. Thank you again for all the help.

#!/usr/bin/env python
#Saves output of "show disks" from the storage Controller to an XML file.

import paramiko
import sys
import re
import xmltodict

IP = sys.argv[1]
USERNAME = "manage"
PASSWORD = "password"
FILENAME = "./logfiles/disks.xml"

cmd = "show disks"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
        client.connect(IP,username=USERNAME,password=PASSWORD)
        stdin, stdout, stderr = client.exec_command(cmd)
except Exception as e:
        sys.exit(1)

data = ""
for line in stdout:
    if re.search('#', line):
        pass
    else:
        data += line
client.close()

f = open(FILENAME, 'w+')
f.write(data)
f.close()

sys.exit(0)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement