bashpostweb-applicationswebapp2

Using Bash to make a POST request


I have a 100 Jetpacks that I have to sign in to configure. I am trying to do it in a bash script but I am having no luck. I can connect to the wifi no problem but my POST request are not achieving anything. Any Advice? Here is link to my github. I have copies of what I captured on Burp suite https://github.com/Jdelgado89/post_Script

TYIA

#!/bin/bash

nmcli device wifi rescan
nmcli device wifi list

echo "What's they last four?"
read last4

echo "What's the Key?"
read key

nmcli device wifi connect Ellipsis\ \Jetpack\ $last4 password $key

echo "{"Command":"SignIn","Password":"$key"}" > sign_on.json
echo "{"CurrentPassword":"$key","NewPassword":"G1l4River4dm1n","SecurityQuestion":"NameOfStreet","SecurityAnswer":"Allison"}" > change_admin.json
echo "{"SSID":"GRTI Jetpack","WiFiPassword":"G1l4River3r","WiFiMode":0,"WiFiAuthentication":6,"WiFiEncription":4,"WiFiChannel":0,"MaxConnectedDevice":8,"PrivacySeparator":false,"WMM":true,"Command":"SetWifiSetting"}" > wifi.json

cat sign_on.json
cat change_admin.json
cat wifi.json

sleep 5
curl -X POST -H "Cookie: jetpack=6af5e293139d989bdcfd66257b4f5327" -H "Content-Type: application/json" -d @sign_on.json http://192.168.1.1/cgi-bin/sign_in.cgi
sleep 5
curl -X POST -H "Cookie: jetpack=6af5e293139d989bdcfd66257b4f5327" -H "Content-Type: application/json" -d @change_admin.json http://192.168.1.1/cgi-bin/settings_admin_password.cgi
sleep 5
curl -X POST -H "Cookie: jetpack=6af5e293139d989bdcfd66257b4f5327" -H "Content-Type: application/json" -d @wifi.json http://192.168.1.1/cgi-bin/settings_admin_password.cgi

Solution

  • This is not correct:

    echo "{"Command":"SignIn","Password":"$key"}" > sign_on.json
    

    The double quotes are not being put literally into the file, they're just terminating the shell string beginning with the previous double quote. So this is writing

    {Command:SignIn,Password:keyvalue}
    

    into the file, with no double quotes. You need to escape the nested double quotes.

    echo "{\"Command\":\"SignIn\",\"Password\":\"$key\"}" > sign_on.json
    

    However, it would be best if you used the jq utility instead of formatting JSON by hand. See Create JSON file using jq.

    jq -nc --arg key "$key" '{"Command":"SignIn","Password":$key}' >sign_on.json