2

I have some troubles with the below script.

proc checkUser {userip} {
    set search "show users | include "
    set search_cmd [concat $search $userip]
    exp_send "$search_cmd\r"

    expect {
        -re "Vi.*" {

            set oui "uei.opennms.org/services/passiveServiceStatus"
            set host "localhost"
            set iface "--interface $userip"
            set service "--service Test"
            set parm1 "--parm 'passiveNodeLabel [getNodeName $userip]'"
            set parm2 "--parm 'passiveIpAddr $userip'"
            set parm3 "--parm 'passiveServiceName Test'" 
            set parm4 "--parm 'passiveStatus Up'"

            send_user [ exec perl /opt/opennms/bin/send-event.pl $oui,$host,$iface,$service,$parm1,$parm2,$parm3,$parm4 ]
            #send_user [ exec [pwd]]

        }
    }
}

The whole Expect script executes without error however the perl script called does not seem to execute however when i run the perl script in bash with the same arguments it does what it suppose to do.

Is it the correct way to call a perl script from within the Expect?

Thanks in advance.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
SneakyMummin
  • 683
  • 1
  • 11
  • 30

1 Answers1

3

The program as written will pass the parameters to the Perl script as one large argument with pieces separated by commas. While this is generally legal, it's probably not what the Perl program wants! Instead, put spaces in-between so that the values are separate arguments at the Tcl level, and they will become separate command line arguments at the Perl level too:

exec perl /opt/opennms/bin/send-event.pl $oui $host $iface $service $parm1 $parm2 $parm3 $parm4

Moreover, it appears that you've got multiple arguments per Tcl variable and you're trying to use shell quoting inside them. That's even more wrong. You'd be far better off writing the code like this:

expect {
    -re "Vi.*" {
        set oui "uei.opennms.org/services/passiveServiceStatus"
        set host "localhost"

        send_user [ exec perl /opt/opennms/bin/send-event.pl $oui $host \
                --interface $userip \
                --service "Test" \
                --parm "passiveNodeLabel [getNodeName $userip]" \
                --parm "passiveIpAddr $userip" \
                --parm "passiveServiceName Test" \
                --parm "passiveStatus Up" ]
    }
}

Each parameter word is its own word in Tcl; I've put backslash-newlines in to make things clearer. We use double-quotes so that they can have embedded spaces while still processing substitutions, but if you need something weird then put in {braces} or use backslashes.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215