#!/bin/sh # HostMonster V1.0 by Todd Hammer # This software is licensed under the GPL (GNU Public License) # I wanted to be able to use a treeview to select hosts to connect to # categorized by ISP or Organiziation or Group (Routers, Servers, etc..) MENU="/tmp/menu.$$" TITLE="Host Monster v1.0" CHECKER="Use telnet instead of SSH" CONFDIR="$HOME/.hostmonster" HMRC="$HOME/.hostmonsterrc" # Not currently used..but, who knows? SCRATCH="/tmp/hostmonster-scratch.$$" DIA="Xdialog" MAXHT="40" if [ ! -n "$HOME" ]; then echo "\$HOME is not set! Quitting." exit -1 fi if ! type -path "Xdialog" >/dev/null; then echo "You need to install Xdialog first!" echo "Get it at http://www.chez.com/godefroy" exit; fi # Create the config directory if needed. This will hold all the configs # and the hostmonsterrc file. if [ ! -d "$CONFDIR" ]; then mkdir -m 0700 "$CONFDIR" touch "$HMRC" fi ######################################################################## # This is add/select group mode # We would arrive here once we click the 'Add New Host' entry if [ "$1" = "-g" ]; then # Look for the category name here if [ "x$2" != "x" ]; then # We have a category specified...create it if # necessary and then run hostmonster -a "category" if [ ! -d "$CONFDIR/$CAT" ]; then # Create if the directory doesn't exist mkdir -m 0700 "$CONFDIR/$CAT" fi hostmonster -a "$2" else # Okay. No category has been specified. # Have the user choose a category. # First, see what categories we can find. CATS=`ls $CONFDIR` PROMPT="Select a Group\nto add the entry to:" cat << EOF > $MENU CHOICE=\$($DIA --title "$TITLE" \\ --stdout --no-tags \\ EOF echo -n "--menubox \"$PROMPT\" 0 0 7 " >> $MENU # Build a list of catgeories to choose from # from the user's confdir echo "\"addgroup\" \"Add New Group\" \\">> $MENU IFS=" " for i in $CATS; do if [ -d "$CONFDIR/$i" ]; then echo "\"$i\" \"$i\" \\" >> $MENU fi done # Finish the script and run it cat << EOF >> $MENU ) ret=\$? echo \$CHOICE exit \$ret EOF chmod +x $MENU CHOICE=`$MENU` ret=$? rm -f $MENU if [ "$ret" != "0" ]; then exit 0 fi if [ "$CHOICE" = "addgroup" ]; then $DIA --title "Add New Group" \ --inputbox "Please enter the group name" \ 15 45 "" 2> $SCRATCH ret=$? GRPNM=`cat $SCRATCH` rm -f $SCRATCH if [ "$ret" != "0" ]; then exit 0 fi hostmonster -a "$GRPNM" else # We have a valid group name...ask for host info hostmonster -a "$CHOICE" fi fi fi if [ "$1" = "-a" ]; then if [ ! -n "$2" ]; then echo "Error: No category specified!" exit 1; fi if [ ! -d "$CONFDIR/$2" ]; then mkdir -m 0700 "$CONFDIR/$2" fi # We should have the category by this point # in the form of $2 PROMPT="Adding New Host Entry for '$2'" IPPROMPT="Enter the IP or hostname:" DESC="Enter a Short Desc:" USERP="Enter Username for this session:" # This sets up the dialog window but doesn't finish it yet... $DIA \ --title "$TITLE" \ --wizard \ --check "$CHECKER" \ --separator "," \ --3inputsbox "$PROMPT" 0 0 \ "$IPPROMPT" "" \ "$DESC" "" \ "$USERP" "$USER" 2> $SCRATCH ret=$? CHOICE=`cat $SCRATCH` rm -f $SCRATCH if [ "$ret" != 0 ]; then exit 0 fi OPTS=`echo "$CHOICE" | head -1` if echo $CHOICE | egrep "unchecked" >/dev/null; then CMD='ssh' else CMD='telnet' fi HOST=`echo $CHOICE | cut -d, -f1` DESC=`echo $CHOICE | cut -d, -f2` USR=`echo $CHOICE | cut -d, -f3 | sed 's/\(un\)\?checked//' | sed 's/\n//' | sed 's/[ ]\+//'` FNAME="$CONFDIR/$2/$DESC" # Prompt for colors and custom port number PROMPT="Enter optional colors and port number:" BGPROMPT="Background color (Hex, RGB or simple name):" FGPROMPT="Font color (Hex, RGB or simple name):" PORTPROMPT="Optional custom port number:" $DIA \ --title "$TITLE" \ --wizard \ --separator "," \ --3inputsbox "$PROMPT" 0 0 \ "$BGPROMPT" "" \ "$FGPROMPT" "" \ "$PORTPROMPT" "" 2> $SCRATCH ret=$? OPTIONS=`cat $SCRATCH` rm -f $SCRATCH # Now, get our chosen options BG=`echo $OPTIONS | cut -d, -f 1` FG=`echo $OPTIONS | cut -d, -f 2` PORT=`echo $OPTIONS | cut -d, -f 3` if [ "x$BG" = "x" ]; then BG="black" fi if [ "x$FG" = "x" ]; then FG="white" fi if [ "x$PORT" = "x" ]; then if [ "$CMD" = "telnet" ]; then PORT="23" else PORT="22" fi fi # Now write the new host file FNAME=`echo $FNAME | sed 's/\n//'` # Remove the newline (chomp) if [ ! -f "$FNAME" ]; then echo "#!/bin/sh" >> "$FNAME" echo "xterm \\" >> "$FNAME" echo "-bg \"$BG\" -fg \"$FG\" -sb -T \"$DESC\" \\" >> "$FNAME" echo "-name \"$DESC\" -sl 1500 -fn fixed \\" >> "$FNAME" if [ $CMD = ssh ]; then echo "-e $CMD -p $PORT $USR@$HOST" >>$FNAME else echo "-e $CMD $HOST $PORT" >>$FNAME fi chmod +x "$FNAME" else Xdialog --yesno "Error: \"$DESC\" exists!.\nWould you like to overwrite?" 0 0 over=$? if [ "$over" = "0" ]; then echo "#!/bin/sh" > "$FNAME" echo "xterm -bg \"$BG\" -fg \"$FG\" -sb -T \"$DESC\" \\" >> "$FNAME" echo "-name \"$DESC\" -sl 1500 -fn fixed \\" >> "$FNAME" if [ $CMD = ssh ]; then echo "-e $CMD -p $PORT $USR@$HOST" >>$FNAME else echo "-e $CMD $HOST $PORT" >>$FNAME fi chmod +x "$FNAME" else exit; fi fi fi # Here's the beef of this script...it presents a treeview based # selector based on what it finds in the config dir. PROMPT="Please choose the host to connect to:" IFS=" " DIRLIST=`ls -C1F "$CONFDIR" | grep "/" | wc -l | awk '{print $1}'` if [ "$DIRLIST" -lt "$MAXHT" ]; then LIMIT=$((DIRLIST+10)) else LIMIT=$MAXHT fi #echo "\$LIMIT is '$LIMIT'"; exit; # This sets up the dialog window but doesn't finish it yet... cat << EOF >$MENU #!/bin/sh CHOICE=\$($DIA --title "$TITLE" --stdout --no-tags --treeview "$PROMPT" $LIMIT 40 0 \\ EOF echo "'additem' \"Add Host\" off 0 \\" >> $MENU # This produces a list of hosts to choose from and adds it to $MENU for i in `ls -C1F "$CONFDIR" | grep "/"`; do DIR=`echo "$i" | sed 's/\///'` DIRCNT=`ls "$CONFDIR/$DIR" | wc -l` # see if there are files there if [ $DIRCNT = 0 ]; then echo "\"$DIR\" \"$DIR\" off 0 \\" >> $MENU else echo "\"$DIR\" \"$DIR\" on 0 \\" >> $MENU fi for j in `ls "$CONFDIR/$DIR" | egrep -v "~$"`; do EXEC="$CONFDIR/$DIR/$j" if [ -f "$EXEC" ]; then echo "\"$EXEC\" \"$j\" off 1 \\" >>$MENU fi done done # this finishes the command cat << EOF2 >>$MENU ) ret=\$? echo \$CHOICE exit \$ret EOF2 chmod +x $MENU CHOICE=`$MENU` ret=$? rm -f $MENU if (( $ret != 0 )) ; then exit 0 fi #xlock $NOLOCK -mode $MODE CH1=`echo $CHOICE | awk '{print $1}'` if [ "$CH1" = "additem" ]; then hostmonster -g else sh "$CHOICE" fi