CGI app on web interface
-
I'd like to run Fivn's Asterisk Managemnet GUI on pfSense box, next to asterisk itself. It used to run well on DD-WRT, now the question is, how could I run this .sh CGI file so that it can be displayed in a browser?
Here's the code:
#!/bin/sh ################################################################################ # Asterisk Managemnet GUI # Version 1.6 # Copyright (C) 2006 - 2009, Loc Nguyen, http://www.fivn.com/ # This software is allowed to use only for noncommercial purposes. # Ask the author for commercial use. # THERE IS NO WARRANTY FOR THE PROGRAM BECAUSE IT IS LICENSED FREE OF CHARGE. # # This prorgram was originally written for handling Asterisk maintenance. # POST method was used because DD-WRT web server does not support # GET method to submit form data. # WARNING: In the DD-WRT system, large files cannot be posted back to the server. # LIMITATION: The DD-WRT web server does not support other file formats than html, # style files and pictures. # export PATH=/opt/bin:/opt/sbin:/opt/usr/sbin:${PATH} script_name="asterisk" script_version="1.6" # Values are: 1.2|1.4, use 1.4 for Asterisk 1.6 asterisk_version="1.4" asterisk_cdr='"${CDR(accountcode)","${CDR(src)}","${CDR(dst)}","${CDR(dcontext)}","${CDR(clid)}","${CDR(channel)}","${CDR(dstchannel)}","${CDR(lastapp)}","${CDR(lastdata)}","${CDR(start)}","${CDR(answer)}","${CDR(end)}","${CDR(duration)}","${CDR(billsec)}","${CDR(disposition)}","${CDR(amaflags)}"' show_call_reverse="yes" keep_log_entry="2" keep_call_entry="30" disable_backup_button="no" table_col="5" # Values are: dd-wrt|openwrt|unslung|other operation_system_variant="dd-wrt" if [ "$disable_backup_button" = "no" ]; then table_col="6" fi if [ "$operation_system_variant" = "dd-wrt" ]; then asterisk_calls="/opt/var/log/asterisk/cdr-csv/Master.csv" asterisk_logs="/opt/var/log/asterisk/messages" asterisk="/opt/sbin/asterisk" path="/opt/etc/asterisk" backup_command="tar -czvf /tmp/www/asterisk.tar.gz $path" backup_text="``` Download Asterisk configuration files: [asterisk.tar.gz](\"../asterisk.tar.gz\") ```" elif [ "$operation_system_variant" = "openwrt" ]; then asterisk_calls="/var/log/asterisk/cdr-csv/Master.csv" asterisk_logs="/var/log/asterisk/messages" asterisk="/usr/sbin/asterisk" path="/etc/asterisk" backup_command="tar -C /etc -czvf /www/asterisk.tar.gz asterisk" backup_text="``` Download Asterisk configuration files: [asterisk.tar.gz](\"../asterisk.tar.gz\") ```" elif [ "$operation_system_variant" = "unslung" ]; then asterisk_calls="/opt/var/log/asterisk/cdr-csv/Master.csv" asterisk_logs="/opt/var/log/asterisk/messages" asterisk="/opt/sbin/asterisk" path="/opt/etc/asterisk" backup_command="tar -czvf /opt/share/www/admin/asterisk.tar.gz $path" backup_text="``` Download Asterisk configuration files: [asterisk.tar.gz](\"../admin/asterisk.tar.gz\") ```" else asterisk_calls="/var/log/asterisk/cdr-csv/Master.csv" asterisk_logs="/var/log/asterisk/messages" asterisk="/usr/sbin/asterisk" path="/etc/asterisk" backup_command="tar -czvf /www/asterisk.tar.gz $path" backup_text="``` Download Asterisk configuration files: [asterisk.tar.gz](\"../asterisk.tar.gz\") ```" fi urldecode() { awk ' { HEX_INDEX="0123456789ABCDEF" gsub(/\+/, " ") while ((pos = index($0, "%")) > 0 && length($0) >= 3) { if (pos > 1) printf substr($0, 0, pos - 1) hex1 = toupper(substr($0, pos + 1, 1)) hex2 = toupper(substr($0, pos + 2, 1)) val1 = (index(HEX_INDEX, hex1) - 1) * 16 val2 = (index(HEX_INDEX, hex2) - 1) printf "%c", val1 + val2 $0 = substr($0, pos + 3, length($0) - 3) } if (length($0) > 0) printf $0 }' } list_action () { awk ' { COMMANDS[1,1]="sip reload" COMMANDS[1,2]="Reload SIP configuration" COMMANDS[1,3]="SIP configuration reloaded" COMMANDS[2,1]="iax2 reload" COMMANDS[2,2]="Reload IAX configuration" COMMANDS[2,3]="IAX configuration reloaded" COMMANDS[3,1]="extensions reload" COMMANDS[3,2]="Reload extensions" COMMANDS[3,3]="Extensions reloaded" COMMANDS[4,1]="moh reload" COMMANDS[4,2]="Reload Music On Hold" COMMANDS[4,3]="Music On Hold reloaded" COMMANDS[5,1]="dnsmgr reload" COMMANDS[5,2]="Reload DNS configuration" COMMANDS[5,3]="DNS configuration reloaded" COMMANDS[6,1]="reload" COMMANDS[6,2]="Reload configuration" COMMANDS[6,3]="Configuration reloaded" COMMANDS[7,1]="logger reload" COMMANDS[7,2]="Reopen the log files" COMMANDS[7,3]="The log files reopened" COMMAND_COUNT=7 if (asterisk_version == "1.4") { COMMANDS[3,1]="dialplan reload" COMMANDS[6,1]="module reload" } for (i = 1; i <= COMMAND_COUNT; i++) { print "" print " <form action="\"'$script_name'\"" method="\"post\"">" print ""COMMANDS[i,2] print "" print "" print "" print " " print "" print "" print "</form> " } }' script_name="$1" asterisk_version="$2" - } parse_calls() { cdr_tag="$(echo "$2" | sed -e 's/\"//g; s/\$//g; s/{//g; s/(//g; s/)//g; s/}//g;')" awk ' function formattime(time) { TEN = 10 TIME = 60 text="" hour = 0 min = int(time / TIME) sec = time % TIME if (min > TIME) { hour = int(min / TIME) min = min % TIME } if (hour < TEN) { text = text "0" } text = text hour ":" if (min < TEN) { text = text "0" } text = text min ":" if (sec < TEN) { text = text "0" } time = text sec return time } { TIME_FIELDS="CDRduration,CDRbillsec" FIELD_COUNT=8 HEADERS="Caller,From,To,Application,Data,Time,Duration,Status" FIELDS="CDRclid,CDRsrc,CDRdst,CDRlastapp,CDRlastdata,CDRstart,CDRduration,CDRdisposition" split(TIME_FIELDS, time_tag, ",") split(HEADERS, header_tag, ",") split(FIELDS, field_tag, ",") split(cdr_tag, cdr_log, ",") reverse=0 reverse_count=1 tr_tag="" if (show_call_reverse == "yes") { reverse = 1 } printf "" for (i = 1; i <= FIELD_COUNT; i++) { printf "" header_tag[i] "" } print "" while ((getline line < asterisk_calls) > 0) { gsub(/\"/, "", line) gsub(/&/, "\\&", line) gsub(/ gsub(/>/, "\\>", line) split(line, data, ",") if (!reverse) { printf tr_tag } for (i = 1; i <= FIELD_COUNT; i++) { for (y in cdr_log) { if (field_tag[i] == cdr_log[y]) { for (k in time_tag) { if (time_tag[k] == cdr_log[y]) { data[y] = formattime(data[y]) break } } if (reverse) { buf[reverse_count, i] = data[y] } else { printf "" data[y] "" } break } } } if (reverse) { reverse_count++ } else { print "" } } if (reverse) { for (i = reverse_count - 1; 1 <= i; i--) { printf tr_tag for (j = 1; j <= FIELD_COUNT; j++) { printf "" buf[i, j] "" } print "" } } close(asterisk_calls) }' asterisk_calls="$1" cdr_tag="$cdr_tag" show_call_reverse="$show_call_reverse" - } parse_logs() { awk ' { FIELD_COUNT=3 HEADERS="Time,Level,Text" split(HEADERS, header_tag, ",") printf "" for (i = 1; i <= FIELD_COUNT; i++) { printf "" header_tag[i] "" } print "" while ((getline line < asterisk_logs) > 0) { split(line, data, "] ") data[1] = substr(data[1], 2) data[2] = data[2] "]" printf "" for (i = 1; i <= FIELD_COUNT; i++) { printf "" data[i] "" } print "" } close(asterisk_logs) }' asterisk_logs="$1" - } replace_html_chars() { sed -e 's/\&/\&/g; s//\>/g' } remove_flash_chars() { sed -e 's/\.\.\///g; s/\.\///g; s/\///g; s/\.\.\\//g; s/\.\\//g; s/\\//g' } remove_unsecure_chars() { sed -e 's/;//g' } file_missing() { echo "``` The $1 file doesn't exist. ```" } div_id() { echo "" } div_close() { echo "" } header() { echo "**$1**" } form_open() { echo " <form action="\"$script_name\"" method="\"post\"">" echo "" } form_close() { echo "" echo "</form> " } form_submit() { echo "" } form_submit_disable() { echo "" } form_action() { echo "" } form_action_submit() { form_action "$2" if ([ "$3" != "" ] && [ "$2" = "$3" ]); then echo "" else echo "" fi } form_input_field() { echo "" } form_hidden_field() { echo "" } textarea() { echo "" } table_open() { echo "" } table_close() { echo " " } #if [ "$operation_system_variant" != "dd-wrt" ]; then echo 'Content-Type: text/html' echo '' #fi echo ' <title>Asterisk Management</title> ' if [ "$REQUEST_METHOD" = "POST" ]; then read QUERY_STRING eval $(echo "$QUERY_STRING" | awk -F'&' '{for(i=1;i<=NF;i++){print $i}}') if [ "$action" != "" ]; then action="$(echo $action | urldecode)" fi if [ "$command" != "" ]; then command="$(echo $command | urldecode | remove_unsecure_chars)" fi if [ "$file" != "" ]; then file="$(echo $file | urldecode | remove_flash_chars | remove_unsecure_chars)" fi if [ "$message" != "" ]; then message="$(echo $message | urldecode | remove_unsecure_chars)" fi if [ "$content" != "" ]; then content="$(echo $content | urldecode | tr -d '\r')" fi fi div_id 'function' table_open echo '' echo 'Peers' echo 'Channels' echo 'View' echo "Administration, version: $script_version, © 2006 - 2009 [FIVN](\"http://www.fivn.com/\")" echo '' echo '' echo '' form_open form_action_submit 'SIP' 'sip_peers' "$action" form_close echo '' form_open form_action_submit 'IAX' 'iax_peers' "$action" form_close echo '' echo '' form_open form_action_submit 'SIP' 'sip_channels' "$action" form_close echo '' form_open form_action_submit 'IAX' 'iax_channels' "$action" form_close echo '' form_open form_action_submit 'All' 'show_channels' "$action" form_close echo '' echo '' form_open form_action_submit 'Calls' 'calls' "$action" form_close echo '' form_open form_action_submit 'Logs' 'logs' "$action" form_close echo '' echo '' form_open if [ "$action" = "edit" ]; then form_action_submit 'File Editor' 'editor' 'editor' else form_action_submit 'File Editor' 'editor' "$action" fi form_close echo '' form_open form_action_submit 'Commands' 'execute' "$action" form_close echo '' form_open form_action_submit 'Clean Logs' 'trim' "$action" form_close if [ "$disable_backup_button" = "no" ]; then echo '' form_open form_action_submit 'Backup' 'backup' "$action" form_close fi echo '' form_open form_action_submit 'System Status' 'status' "$action" form_close echo '' echo '' table_close div_close if [ "$operation_system_variant" = "" ]; then header 'Script Configuration' echo -n '``` ' echo "The \$operation_system_variant parameter needs to be set first." echo "You need to modify the script and set a value for the \$operation_system_variant parameter." echo ' ```' elif [ "$action" = "sip_peers" ]; then asterisk_header="SIP Peers" asterisk_command="sip show peers" elif [ "$action" = "sip_channels" ]; then asterisk_header="SIP Channels" asterisk_command="sip show channels" elif [ "$action" = "iax_peers" ]; then asterisk_header="IAX Peers" asterisk_command="iax2 show peers" elif [ "$action" = "iax_channels" ]; then asterisk_header="IAX Channels" asterisk_command="iax2 show channels" elif [ "$action" = "show_channels" ]; then asterisk_header="Channels" asterisk_command="show channels" if [ "$asterisk_version" = "1.4" ]; then asterisk_command="core show channels" fi elif [ "$action" = "calls" ]; then header 'Calls' if [ -e "$asterisk_calls" ]; then div_id 'log' table_open echo '' | parse_calls "$asterisk_calls" "$asterisk_cdr" "$show_call_reverse" table_close div_close else file_missing "$asterisk_calls" fi elif [ "$action" = "logs" ]; then header 'Logs' if [ -e "$asterisk_logs" ]; then div_id 'log' table_open echo '' | parse_logs "$asterisk_logs" table_close div_close else file_missing "$asterisk_logs" fi elif [ "$action" = "" ]; then asterisk_header="Asterisk Information" asterisk_command="show version" if [ "$asterisk_version" = "1.4" ]; then asterisk_command="core show version" fi elif [ "$action" = "trim" ]; then header 'Clean Logs' if [ -e "$asterisk_logs" ]; then data=$(head -$keep_log_entry "$asterisk_logs") if [ "$data" != "" ]; then echo "$data" > "$asterisk_logs" fi $asterisk -r -x "logger reload" echo '``` Log file was trimmed. ```' else file_missing "$asterisk_logs" fi if [ -e "$asterisk_calls" ]; then data=$(tail -n $keep_call_entry "$asterisk_calls") if [ "$data" != "" ]; then echo "$data" > "$asterisk_calls" fi echo '``` Call file was trimmed. ```' else file_missing "$asterisk_calls" fi elif [ "$action" = "backup" ]; then header 'Asterisk Configuration Backup' div_id 'backup' echo $backup_text echo ' ' echo -n '
'
$backup_command
echo 'div_close elif [ "$action" = "execute" ]; then header 'Commands' div_id 'command' table_open echo '_Enter 'help' for list of commands_ ' form_open form_action 'execute' if ([ "$command" != "" ] && [ "$message" != "" ]); then form_input_field 'command' "" '30' '120' 'cmd' else form_input_field 'command' "$command" '30' '120' 'cmd' fi echo ' ' form_submit 'Execute' form_close echo '' if (([ "$command" = "" ]) || ([ "$command" != "" ] && [ "$message" != "" ])); then echo ' ' echo '' | list_action "$script_name" "$asterisk_version" fi table_close div_close if [ "$command" != "" ]; then echo -n '``` ' if [ "$message" != "" ]; then echo "$message" echo '' fi $asterisk -r -x "$command" echo ' ```' fi elif [ "$action" = "editor" ]; then if ([ "$content" != "" ] && [ -e "$path/$file" ]); then echo "$content" > "$path/$file" fi header 'File Editor' div_id 'editor' table_open ls -1 $path/. | awk -F'\n' ' { file_path = path "/" $0 if ((system("test -e " file_path) == 0) && ((getline sample < file_path) >= 0)) { close(file_path) print "" print " <form action="\"'$script_name'\"" method="\"post\"">" print ""$0 print "" print "" print " " print "" print "" print "</form> " } }' path="$path" script_name="$script_name" - table_close div_close elif [ "$action" = "edit" ]; then header "File: $file" echo '' if [ -e "$path/$file" ]; then content=$(cat "$path/$file" | sed -e 's/\&/\&/g; s//\>/g') table_open echo '' form_open textarea 'content' '27' '90' echo -n "$content" textarea_close form_hidden_field 'file' "$file" form_action 'editor' echo ' ' form_submit 'Save' form_close echo '' table_close else file_missing "$path/$file" fi elif [ "$action" = "status" ]; then header 'System Status' echo -n '``` ' echo '--------------------------------------------------------------------------------' echo 'free' free | replace_html_chars echo '--------------------------------------------------------------------------------' echo 'df' df | replace_html_chars echo '--------------------------------------------------------------------------------' echo 'mount' mount | replace_html_chars echo '--------------------------------------------------------------------------------' echo 'uptime' uptime | replace_html_chars echo '--------------------------------------------------------------------------------' data=$(ps) echo "$data" | replace_html_chars echo '--------------------------------------------------------------------------------' echo ' ```' fi if [ "$asterisk_command" != "" ]; then header "$asterisk_header" echo -n '``` ' $asterisk -r -x "$asterisk_command" echo ' ```' fi echo '' echo '' echo '' [/i][/i][/i][/i]
Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.