[ previous ] [ next ] [ threads ]
 
 From:  Christoph Gysin <cgysin at gmx dot ch>
 To:  John <strgout at unixjunkie dot com>
 Cc:  m0n0wall at lists dot m0n0 dot ch
 Subject:  Re: [m0n0wall] m0n0cli.pl
 Date:  Tue, 28 Dec 2004 22:35:27 +0100
John wrote:
> I made a little perl script to pull things down from a m0n0wall via /exe.php.

Funny, I did the same thing a few weeks ago. I wrote a shell script 
using wget and awk.

You could have made your live easier, by using exec_raw.php instead of 
exec.php ;-)

Christoph
-- 
echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3
#!/bin/bash
#
# a simple remote shell for m0n0wall
#
# run a script:
# 	m0n0sh <filename>
#
# run a single command:
# 	m0n0sh -c <command>
#
# run interactive shell:
# 	m0n0sh

USER="admin"		# username
PASS="mono"			# password
IP="192.168.0.1"	# m0n0wall ip address
PROTO="http"		# webgui port (http/https)

PROMPT="m0n0sh # "

URL="$PROTO://$IP/exec_raw.php?cmd="
WGET="wget -q -O- -t1 -T3 --http-user=$USER --http-passwd=$PASS $URL"

die(){ 
	echo "connection failed. ($PROTO://$USER:$PASS@$IP)" >&2
	exit 1
}

urlencode(){
	echo "$@" | awk '
	BEGIN {
		split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ")
		hextab [0] = 0
		for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
	}
	{
		encoded = ""
		for ( i=1; i<=length ($0); ++i ) {
			c = substr ($0, i, 1)
			if ( c ~ /[a-zA-Z0-9.-]/ ) {
				encoded = encoded c
			} else if ( c == " " ) {
				encoded = encoded "+"
			} else {
				lo = ord [c] % 16
				hi = int (ord [c] / 16);
				encoded = encoded "%" hextab [hi] hextab [lo]
			}
		}
		print encoded
	}'
}

if [ "$1" = "-c" ]; then
	shift
	${WGET}"$(urlencode "$@")" || die

elif [ ! -z "$*" -a -f "$1" ]; then
	while read LINE; do
		[ -z "$LINE" ] ||
			${WGET}"$(urlencode "$LINE")"  || die
	done < $1

else
	echo -n "$PROMPT"
	while read LINE; do
		[ -z "$LINE" ] ||
			${WGET}"$(urlencode "$LINE")"  || die
		echo -n "$PROMPT"
	done
fi