Last active
January 12, 2024 15:20
-
-
Save atiti/45894675ccd38c188c9b8f4c1d04fd5a to your computer and use it in GitHub Desktop.
Download running config from a HP 1920S switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" 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" 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://localhost:8082/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" "http://localhost:8082/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." |
First of all, thank you for this very useful script, atiti!
xosepe, I tried on my switch, that has the same PD.01.05, failed too. Have you solved?
thx 4 the script Attila
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi atiti! It´s not working for me with HPE OfficeConnect Switch 1920S 24G 2SFP PPoE+ (185W) JL384A, PD.01.05, Linux 3.6.5. Which firmware version have you tested it with? This is the error I get:
[root@server]# ./hp1920-get-config.sh --host="10.x.x.x" --user="admin" --pass="pass" --file=startup-config
Logging in to the HP switch... OK
Requesting to download config... Error.
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>417 - Expectation Failed</title> </head> <body> <h1>417 - Expectation Failed</h1> </body> </html>
[root@server]#
Thank you very much for your effort!