Tuesday, July 30, 2013

Python remote class for TAHI's v6eval engine.

The TAHI v6eval Perl module for IPv6 compliance testing at one point included a bunch of 'remote' scripts to automate tasks on the target device under test (the NUT, or node under test).

These scripts were always more like examples than set in stone automation and always required adjustment for the target device.   The scripts used a assumed serial port connection via null modem to the target and Perl expect to check for the prompt, issue commands, etc..

At work to automate the compliance testing, I wanted to use the out-of-band management port via ssh to issue commands, and to avoid long waits on reboot with just quick restarts of  services etc, so I made my own remote interface that used this:

It uses some other utils we've coded for keyswapping with the management host,  a ssh remote wrapper, etc, that I won't provide here, but you can see how it works below.

It uses v6eval's own configuration of tn.def and nut.def to provide all the connectivity,
In tn.def:

  • the 'RemoteDevice' option is re-used to provide an ssh ip address. 
  • 'RemoteMethod' is also defined to 'ssh' and although not parsed here could be easily expanded to switch different access methods based on that variable.

In nut.def:

  • the 'User' option is used to provide username for ssh access.
  • the 'Password' option is used to provide the password for ssh access.
Of course 'System' is used by the engine to determine which scripts to execute in /usr/local/v6eval/bin/[system]

All options passed to each of the '.rmt' scripts are available when using the remote class ropt dictionary for easy consumption.

#!/usr/bin/env python
reboots the NUT (via a quick reload)

example invocation from the TAHI v6eval remote suite

./reboot.rmt -u root -p default -d

import remote
import sys
import time

def reboot(argv):
    nut = remote.remote(argv)

    cmd = 'reload'

    rc = nut.runcmd(cmd)
    # maximum time needed for config reload

    if rc != 0:
        print "cmd remote command %s returned %d" % (cmd, rc)
        return nut.exitFail

    return nut.exitPass

if __name__ == '__main__':

# remote.py
# python v6eval remote interface class

import os
import logging
import tcutils.commandutils as cu

from tcutils import logger
from paramikoutils import keyswap

DEBUG = bool(os.environ.get('DEBUG'))
log = logger.stdout_logger('remote',
    level=logging.DEBUG if DEBUG else logging.INFO)

class remote:
    """v6eval remote interface class"""
    def __init__(self, argv):
        """init remote class"""

        # options passed by v6eval
        #my $cmd  = searchPath($rpath, $fname);
        #   $cmd .= " -t $NutDef{System}"       if $NutDef{System};
        #   $cmd .= " -u $NutDef{User}"         if $NutDef{User};
        #   $cmd .= " -p $NutDef{Password}"     if $NutDef{Password};
        #   $cmd .= " -T $TnDef{RemoteCuPath}"  if $TnDef{RemoteCuPath};
        #   $cmd .= " -d $TnDef{RemoteDevice}"  if $TnDef{RemoteDevice};
        #   $cmd .= " -v $TnDef{RemoteDebug}"   if $TnDef{RemoteDebug};
        #   $cmd .= " -i $TnDef{RemoteIntDebug}"    if $TnDef{RemoteIntDebug};
        #   $cmd .= " -o $TnDef{RemoteLog}"     if $TnDef{RemoteLog};
        #   $cmd .= " -s $TnDef{RemoteSpeed}"   if $TnDef{RemoteSpeed};
        #   $cmd .= " -l $TnDef{RemoteLogout}"  if $TnDef{RemoteLogout};
        #   $cmd .= " $RemoteOption $opts @args";

        self.exitPass=0            # PASS
        self.exitIgnore=1          # Ignore (ex. initializeation script)
        self.exitNS=2              # Not yet supported
        self.exitWarn=3            # WARN
        self.exitHostOnly=4        # Host Only
        self.exitRouterOnly=5      # Router Only
        self.exitSpecialOnly=6     # Special Only
        self.exitExceptHost=7      # Except Host
        self.exitExceptRouter=8    # Except Router
        self.exitExceptSpecial=9   # Except Special
        self.exitSkip=10           # Skip
        self.exitTypeMismatch=11   # Type Mismatch
        self.exitFail=32           # FAIL
        self.exitInitFail=33       # Initialization Fail
        self.exitCleanupFail=34    # Cleanup Fail
        #                          # 35 - 63: reserved for future use
        self.exitFatal=64          # FATAL (terminate series of related tests)

        self.lastrc = 0
        self.laststdout = ""
        self.laststderr = ""

        self.ropt = { }
        self.ropt = self.parseargs(argv)

        # root user
        if '-u' in argv:
            self.user = argv[argv.index('-u') + 1]
        # root password
        if '-p' in argv:
            self.passwd = argv[argv.index('-p') + 1]
        # management ip
        if '-d' in argv:
            self.mgmt_ip = argv[argv.index('-d') + 1]

        # save the original options
        self.argv = argv
        keyswap.keyswap(self.mgmt_ip, self.user, self.passwd)

    def parseargs(self, args):
        """find the foo=bar variables"""
        arg_dict = { }
        for arg in args:
                nom, val = arg.split('=')
                arg_dict[nom] = val
            except ValueError:

        return arg_dict

    def runcmd(self, cmd):
        """run the command on the NUT"""
        (self.lastrc, self.laststdout, self.laststderr) = cu.runcmd_ssh(self.mgmt_ip, cmd)
        if self.lastrc > 0:
           log.error('command: %s FAILED %s' % (cmd, self.laststderr))
           return 1
        return 0

Thursday, January 24, 2013

The sick and twisted world of KDE/konsole 4.8.4 from dbus


For some reason, you have to find yourself when running from a konsole window now.

So the code for opening tabs now has to:

  • Grab a list of all the fricking windows (getWindowId()/getWindows() below) to find my own id.
  • Grab a list of all the fricking sessions (getSessions() below)
  • Open a new tab
  • Grab the session list AGAIN and compare it to find the one we just opened.
  • Only then are you able to send a command to that tab.
Hope you can read some shell script, because this is almost too painful to explain.

#!/usr/bin/env bash

# konsole dbus functions

getWindows() {
    windows=`qdbus org.kde.konsole /konsole org.freedesktop.DBus.Introspectable.Introspect |grep MainWindow | cut -d \" -f 2`
    echo ${windows}

getSessions() {
    sessions=`qdbus org.kde.konsole /Sessions org.freedesktop.DBus.Introspectable.Introspect |grep "node name" | cut -d \" -f 2`
    echo ${sessions}

getWindowId() {
    for i in $windows
        id=`qdbus org.kde.konsole /konsole/$i org.kde.KMainWindow.winId`
        if [ "${WINDOWID}" -eq "$id" ]; then
            echo $i
findNewSession() {
    for t in $new; do 
        grep -q $t <(echo $old) 
        if [ "$?" -eq "1" ]; then 
            echo $t
sshopentabs () {
    comm='ssh -o StrictHostKeyChecking=no -o CheckHostIP=no -o UserKnownHostsFile=/dev/null'
    for i in $boxlist
        echo ${KONSOLE_DBUS_SERVICE}
        j=`findNewSession $sessions`
        if [ "${box_name}" -eq "" ]; then
            renametab ${j} $i
            renametab ${j} "${box_name} slot ${slot}"
        sendtab ${j} "${comm} root@${i}"
        let slot=$slot+1

    return ${j}
newtab () {
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply /konsole/$id org.kde.KMainWindow.activateAction string:"new-tab"

renametab () {
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.setTabTitleFormat int32:0 string:"$tabname"
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.setTabTitleFormat int32:1 string:"$tabname"
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.setTitle int32:1 string:"$tabname"

sendtab () {
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${session} org.kde.konsole.Session.sendText string:"$cmd^M"

closetab () {
    dbus-send --session --dest=${KONSOLE_DBUS_SERVICE} --type=method_call --print-reply ${KONSOLE_DBUS_SESSION} org.kde.konsole.Session.sendText string:"exit^M"