HTTP API Scripting Examples

Two command line scripts are available; these scripts provide good examples of the use of the InterMapper HTTP API:

Both scripts make a copy of the InterMapper Settings directory. Both scripts assume they are invoked from the destination directory rather than trying to look it up - this is intended to make testing easier. Dartware recommends that you create a dummy InterMapper Settings folder someplace and try the synchronization to that (the destination directory doesn't even require that InterMapper is installed, only the script). The remote server does not need to be the same platform as the destination machine.

The destination directory should not have an active InterMapper running in it, as the script assumes that it can replace files at will.

Unix shell script

The Unix shell script assumes that the current working directory is the "Temporary" folder inside the destination InterMapper Settings (This is likely to change.).

The script requires bash, curl, tr, grep, sed and awk. to be installed.

Unix script options

clone_im.sh [options]
-r [remote_host_name ]
-t [remote_port ]
-u [remote_user ] 
-p [remote_password]

Defaults:
remote_host_name =  "localhost"
remote_port = 8080 (this is the InterMapper web access  port)
remote_user = "admin"
remote_password =  "admin"

Example:
clone_im.sh -r nitro.dartware.com -t 8080 -u IMuser -p UsErpaSS


Windows vbscript

The Windows vbscript assumes that the current working directory is the destination InterMapper Settings.

Windows script options

clone_im.vbs 
/host:[remote_hostname] 
/port:[remote_port] 
/user:[remote_user]
/password:[remote_password]

Defaults:
remote_host_name (none, must be specified)
remote_port = 80
user  (none, uses auto-login unless specified)
password (none, uses auto-login  unless specified)
secure = false

Example:
clone_im.vbs /host:nitro.dartware.com /port:8080 /user:IMuser /password:UsErpaSS /secure:true


Known bugs

clone_im.sh

InterMapper Settings folder Corresponding URL
Custom Icons /~files/icons
Extensions /~files/extensions
Fonts /~files/fonts
Maps /~files/maps
MIB Files /~files/mibs
Probes /~files/probes
Sounds /~files/sounds
Tools /~files/tools
Web Pages /~files/webpages

 

#! /bin/bash

# Synchronize InterMapper Settings folder from a remote host with InterMapper SDK
# 
# Requires curl and gnu awk

remote=localhost
port=8080
user=admin
password=admin
auth=
while getopts 'r:t:u:p:' OPTION ; do
   case $OPTION in 
      r)   remote="$OPTARG"
         ;;
      t)   port="$OPTARG"
         ;;
      u)   user="$OPTARG"
         ;;
      p)   password="$OPTARG"
         ;;
      ?)   printf "Usage: %s -r remotehost\n" $(basename $0) >&2
         exit 2;;
   esac
done

if [ "$user" ] ; then
   auth="--user $user:$password"
fi

# This script requires InterMapper to be stopped before running.
#/etc/init.d/intermapperd stop

# Get list of top-level file directories from InterMapper
topdirs=$(curl $auth -s http://$remote:$port/~files | tr '\r' '\n')

for dir in $topdirs ; do 
   # Get list of files in this directory
   filelist=$(curl $auth -s $dir | tr '\r' '\n')

   webdir=$(basename $dir)
   echo "Processing $webdir..."
   localdir=""

   echo $filelist | grep "does not exist" >& /dev/null
   if [ $? != 0 ] ; then

   # Convert the web path into the corresponding Setting folder path
      case $webdir in 
         icons) localdir="Custom Icons" ;;
         sounds) localdir="Sounds" ;;
         mibs) localdir="MIB Files" ;;
         probes) localdir="Probes" ;;
         tools) localdir="Tools" ;;
         webpages) localdir="Web Pages";;
         fonts) localdir="Fonts" ;;
         extensions) localdir="Extensions" ;;
         maps) localdir="Maps" ;;
      esac

      for file in $filelist ; do
      # Get this file and move it into the proper location
         curl $auth -s -O $file 
         filename=$(basename $file)

      # Decode the URL to find the real filename
      # Modified from http://do.homeunix.org/UrlDecoding.html to work with gnu awk
         local_filename=$(echo $filename | \
            sed 's/+/ /g'| \
            sed 's/\%0[dD]//g' | \
            awk '/%/{while(match($0,/\%[0-9a-fA-F][0-9a-fA-F]/)){$0=substr($0,1,RSTART-1)sprintf("%c",strtonum("0x"substr($0,RSTART+1,2)))substr($0,RSTART+3);}}{print}')

      # This version works with BSD awk
      # local_filename=$(echo $filename | \
      #    sed 's/+/ /g'| \
      #    sed 's/\%0[dD]//g' | \
      #    awk '/%/{while(match($0,/\%[0-9a-fA-F][0-9a-fA-F]/)){$0=substr($0,1,RSTART-1)sprintf("%c",0+("0x"substr($0,RSTART+1,2)))substr($0,RSTART+3);}}{print}')

      # Make sure the destination directory exists
         local_dirname=$(dirname "$local_filename")
         mkdir -p "../$localdir/$local_dirname"

         echo "  " $(basename "$local_filename")
         mv $(basename $file) "../$localdir/$local_filename"
      done
   fi
done

# Preferences file is separate, since it's stored in the top level of the InterMapper Settings directory
curl $auth -s -O http://$remote:$port/~files/Preferences
mv Preferences ../

# restart InterMapper 
#/etc/init.d/intermapperd start