Skip to content

Instantly share code, notes, and snippets.

@jbauers
Forked from atiti/hp1920-get-config.sh
Created August 22, 2018 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbauers/9af99597e232ae377d60e7e35a7a5710 to your computer and use it in GitHub Desktop.
Save jbauers/9af99597e232ae377d60e7e35a7a5710 to your computer and use it in GitHub Desktop.
Download running config from a HP 1920S switch
#!/bin/bash
#
# Simple script to download the running configuration from the HP 1920S switch
# through the HTTP "APIs"
#
# Run it as:
# $ ./hp1920-getconfig.sh --host="10.1.2.3" --user="admin" --pass="hello" --file=startup-config
#
# Attila Sukosd <attila@airtame.com>
#
HOST=""
USER="admin"
PASS=""
for i in "$@"; do
case $i in
-h=*|--host=*)
HOST="${i#*=}"
shift
;;
-u=*|--user=*)
USER="${i#*=}"
shift
;;
-p=*|--password=*)
PASS="${i#*=}"
shift
;;
-f=*|--file=*)
FILE="${i#*=}"
shift
;;
*)
# unknown option
;;
esac;
done;
if [ "$HOST" == "" -o "$FILE" == "" ]; then
echo "Error. You need to specify at least the host with --host and the output file with --file";
exit 1;
fi;
echo -n "Logging in to the HP switch... "
# Login
CS=$(curl -v -X POST -F "username=$USER" -F "password=$PASS" -H "Expect:" http://$HOST/htdocs/login/login.lua 2>&1 |grep "Set-Cookie" |awk '{print $3}' |cut -d ';' -f1)
if [ "$?" -ne 0 ]; then
echo "Error."
exit 1;
fi;
echo "OK"
# Format the cookies correctly
H=$(echo $CS |sed 's/ /; /g')
TS=$(date +%s000)
echo -n "Requesting to download config... "
# Request config download
PARAMS=$(curl -X POST -F 'file_type_sel%5B%5D=config' -F "http_token=$TS"-H "Referer: http://$HOST/htdocs/pages/base/file_upload_modal.lsp?help=/htdocs/lang/en_us/help/base/help_file_transfer.lsp&filetypes=6&protocol=6" -H "Cookie: $H" -H "Expect:" http://$HOST/htdocs/lua/ajax/file_upload_ajax.lua?protocol=6 2>/dev/null)
if [ "$(echo $PARAMS |grep '"successful": "ready",')" == "" ]; then
echo "Error."
echo $PARAMS
exit 1;
fi;
echo "OK"
PARAMS2=$(echo $PARAMS | cut -d '?' -f 2 | cut -d '"' -f 1)
echo -n "Downloading config... "
curl -H "Referer: http://$HOST/htdocs/pages/base/file_upload_modal.lsp?help=/htdocs/lang/en_us/help/base/help_file_transfer.lsp&filetypes=6&protocol=6" -H "Cookie: $H" -H "Expect:" "http://$HOST/htdocs/pages/base/file_http_download.lsp?$PARAMS2" -o "$FILE" 2>/dev/null
if [ "$?" -ne 0 ]; then
echo "Error."
exit 1;
fi;
echo "OK. Saved to $FILE."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment