Skip to content

Instantly share code, notes, and snippets.

@nebuxadnezzar
Last active April 25, 2024 20:25
Show Gist options
  • Save nebuxadnezzar/ee2262798257a070f6a5a0220008181f to your computer and use it in GitHub Desktop.
Save nebuxadnezzar/ee2262798257a070f6a5a0220008181f to your computer and use it in GitHub Desktop.
directory listing script used for busybox httpd server
#!/bin/bash +x
folder=${REQUEST_URI#${SCRIPT_NAME}}
echo "Content-Type: text/html; charset=UTF-8"
echo "Connection: close"
echo ""
cat <<EOF
<!doctype html>
<html lang="en-US">
<head>
<meta http-equiv="Pragma" content="no-cache">
<style>
table {
font-size: 18px;
}
.fc{padding-left: 10px}
</style>
</head>
<body>
EOF
#echo SCRIPT_NAME $SCRIPT_NAME
#echo '<br/>'
#echo FOLDER "[$folder]"
echo '<h2>'
if [ ! -z "$folder" -a "$folder" != " " -a "$folder" != '/' -a "$folder" != '/cgi-bin' ]; then
echo "Index of $folder<br/>"
f=${REQUEST_URI##*/}
printf "<a href=\"%s\">../</a>\n" ${REQUEST_URI%/${f}}
else
echo "Index of /"
folder=""
fi;
echo '</h2>'
dirs=( $(find ..${folder} -maxdepth 1 -mindepth 1 -type d -print | sort) )
echo '<table>'
for ((i = 0; i < ${#dirs[@]}; i++ )) ;do
d=${dirs[$i]}
n=${d#..${folder}}
d=`echo $d|sed -E "s|\.\.+|$SCRIPT_NAME|g"`
if [ "$n" != "/cgi-bin" ]; then
printf "<tr><td><a href=\"%s\">%s</a></tr></td>\n" $d $n ;
fi
done
echo '</table>'
files=( $(find ..${folder} -maxdepth 1 -mindepth 1 -not -type d -print | sort) )
echo '<table>'
for ((i = 0; i < ${#files[@]}; i++ )) ;do
f=${files[$i]}
#echo $f
n=${f#..${folder}/}
if [ "$n" == 'httpd.conf' ]; then
continue
fi;
printf "<tr><td class=\"fc\"><a href=\"${folder}/%s\">%s</a></tr></td>\n" $n $n
done
cat <<EOF
</table>
</body>
</html>
EOF
: <<'comment'
to test from command line
SCRIPT_NAME=/cgi-bin/index.cgi REQUEST_URI="/cgi-bin/index.cgi/cam/" index.cgi
comment
@nebuxadnezzar
Copy link
Author

put this script into your cgi-bin folder. it will be executed if index.html is missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment