Executing a batch of commands on multiple IBM SVC/Storwize storage systems

Sometimes you have to change several configuration parameters or run multiple commands over a list of IBM SVC storage systems. Here is my small Python 3 script to do the stuff quickly. It requires Paramiko module to handle SSH interactions.

Edit svcrun_conf.py to set targets, logins, passwords and all other parameters.

Usage: ./svcrun.py

#!/usr/bin/python3

# svcrun.py - using SSH, execute a batch of commands on multiple IBM SVC/Storwize storage systems
#
# Python 3 and paramiko module are required
# Edit svcrun_conf.py to set targets, logins, passwords and all other parameters
#
# Usage:
#  ./svcrun.py
#
# 2017.09.14  v 1.0   Mikhail Zakharov 

import sys
import datetime
import paramiko

# Configurable values are in svcrun_conf.py
import svcrun_conf

def ssh_exec(command, target, port=22):
    """Execute a command via SSH and read results"""

    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    try:
        client.connect(target[0], username=target[1], password=target[2], port=port)
    except:
        return 'Error: {user}@{target}: is inaccessible: {error}'.format(user=target[1], target=target[0],
                                                                         error=sys.exc_info()[1])

    stdin, stdout, stderr = client.exec_command(command)

    error = stderr.read()
    if error:
        error = error.decode('US-ASCII')
        client.close()
        return 'Error: {user}@{target}: "{command}" has failed: {error}'.format(target=target[0], user=target[1],
                                                                                command=command, error=error)

    data = stdout.read()
    client.close()

    try:
        data = data.decode('UTF-8')
    except UnicodeDecodeError:
        data = data.decode('US-ASCII')

    return data

def runcommand(target, command):
    dt = '{0:%Y-%m-%d-%H-%M-%S}'.format(datetime.datetime.now())

    print(dt, 'Run command: "{cmd}"'.format(cmd=command), file=sys.stderr)
    print(ssh_exec(command, target), file=open(dt + '_' + target[0] + '_' + command.split()[0] + '.out', 'a'))

for target in svcrun_conf.svc_targets:
    for command in svcrun_conf.svc_commands:
        runcommand(target, command)

print('Done', file=sys.stderr)

Configuration file is required:

# Configuration file for svcsrun.py

# Default SVC/Storwize credentials
svc_username = 'username'
svc_password = 'password'

# SVC/Storwize systems to run commands on
svc_targets = {
    # 'Hostname/IP', 'Login', 'Password'),
    ('192.168.1.11', svc_username, svc_password),
    ('192.168.1.12', 'anotheruser', 'secret')
}

# Commands to run one by one on each SVC/Storwize system
svc_commands = [
    'lssystem -delim \,',
    'lsvdisk -nohdr -bytes -delim \,'
]

Download it from GitHub GitLab.

Advertisements

About mezzantrop

12+ years of experience in large SAN and storage environments: mainly Hitachi, HP and Brocade. Now I am a proud SAN/storage IBMer. Redbooks author. The BeaST – storage-system concept inventor. Empty – expect-like tool developer. FreeBSD enthusiast.
This entry was posted in Storage Automation, Tips & tricks and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.