Running omshell from a Shell Script

Gauthier, Eric L elg at bu.edu
Fri Apr 21 19:52:40 UTC 2006


Martin,

We chose to use an Expect script instead of bash as a wrapper around
Omshell.   Our script is a bit customized to the few commands we wanted
it to run (query, add, remove a MAC), but hopefully it will give you a
place to start.  In case you are curious, we then write some perl
routines with shell calls to this script to give us a perl API to our
DHCP system.  Its kludgy as perl calls a shell, which calls expect,
which calls a shell, but it works :)


#!/usr/bin/expect --

set type 0
# set timeout 30
set timeout 10

# Parse the command line.
for { set i 0 }  { $i < [ llength $argv ] }  { incr i } {
        switch -- [ lindex $argv $i ] { 
                "-h" { set hostname [ lindex $argv [incr i] ] }
                "-m" { set macaddr [ lindex $argv [incr i] ] }
                "-s" { set server [ lindex $argv [incr i] ] }
                "-q" { set type 1 }
                "-a" { set type 2 }
                "-d" { set type 3 }
        }
}

set cmdLineErr 0

if { $type == 0 } {
        puts "Please specify what you want"
        puts "  -a Add"
        puts "  -q Query"
        puts "  -d Delete"
        set cmdLineErr 1        
}

if { ![info exists hostname] } {
        puts "Please specify a hostname"
        set cmdLineErr 1        
}

if { ![info exists macaddr] } {
        puts "Please specify a mac address"
        set cmdLineErr 1
}

if { ![info exists server] } {
        puts "Please specify a server"
        set cmdLineErr 1
}

if { $cmdLineErr == 1 } { exit 1 }

puts "MAC addr = $macaddr"
puts "Hostname = $hostname"

spawn /usr/bin/omshell

expect "> "
send "server $server\r"
expect "> "
send "port 7911\r"
expect "> "
send "key <PUT YOUR KEY INFORMATION HERE\r"
expect "> "
send "connect\r"
expect {
   -re "obj: " { send "" }
   timeout exit
  }
expect "> "
send "new host\r"
expect "> "
send "set name = \"$hostname\"\r"
expect "> "
send "set hardware-address = $macaddr\r"
expect "> "
send "set hardware-type = 00:00:00:01\r"
expect "> "

if { $type == 1 } {
  send "open\r"
  expect "> "
}
if { $type == 2 } {
  send "create\r"
  expect "> "
}
if { $type == 3 } {
  send "open\r"
  expect "> "
  send "remove\r"
  expect "> "
}

exit 0


Eric Gauthier
  Network Engineer
  617-353-8218 ~^~ elg at nsegc.bu.edu
Boston University - Office of IT




 

-----Original Message-----
From: dhcp-users-bounce at isc.org [mailto:dhcp-users-bounce at isc.org] On
Behalf Of Martin McCormick
Sent: Friday, April 21, 2006 3:03 PM
To: dhcp-users at isc.org
Subject: Running omshell from a Shell Script

	I am testing a bash script which is similar to one which someone
posted earlier and it fails unusually under FreeBSD.  It kind of has the
earmarks of a timing issue, but I am not sure.

	Here is a shell script that shuts down a failover peer or would
if it could only connect.

#!/usr/local/bin/bash
# ARG1 Address of DHCP SERVER
#Read in these values from the named file.
. $HOME/keys/omshell.conf
#Set up the here document.
cat << EOF |omshell
server $1
port $MYPORT
key $KEY $SECRET
connect
new control
set state = 00:00:00:02
open
update
EOF 

	What the output looks like:

> > > > dhcpctl_connect: not found
> not connected.
> no open object.
> not connected.
> not connected.
> <STDIN> line 1: unknown token: EOF
EOF
^
> 

	If I do all those commands manually, it works as expected.

	This may be the wrong group for this question because it
appears to not be a problem with omapi but may be a problem with the
shell script.  It makes one wonder what would happen if one could slow
it down.  Any ideas are appreciated.

Martin McCormick WB5AGZ  Stillwater, OK 
Systems Engineer
OSU Information Technology Department Network Operations Group



More information about the dhcp-users mailing list