Community discussions

MikroTik App
 
snowdogging
just joined
Topic Author
Posts: 16
Joined: Tue Dec 20, 2016 6:23 pm

cookie based auth using fetch

Thu Dec 28, 2023 6:47 pm

I am trying to convert some python code to a Mikrotik script. The code captures the current antenna strength from an ubiquiti lightbeam. The python code uses a session to pass cookie based auth between two calls. Is there any way to use the fetch tool to do this?

Here is what I have so far which successfully authenticates. But, the cookie is not in the received payload.
/tool
{
    :local username myuser
    :local password mypass
    :local ip 1.1.1.1
    :local loginurl "https://$ip/api/auth"
    :local statusurl "http://$ip/status.cgi"

    fetch mode=https url=$loginurl http-method=post http-data="username=$username&password=$password"

    #insert next fetch command using retrieved cookie
} 
 
snowdogging
just joined
Topic Author
Posts: 16
Joined: Tue Dec 20, 2016 6:23 pm

Re: cookie based auth using fetch

Thu Dec 28, 2023 7:06 pm

actually if I pass output=user-with-headers I see the cookie info. Now I just need to parse out the cookie and pass it. Anyone done this before?
 
snowdogging
just joined
Topic Author
Posts: 16
Joined: Tue Dec 20, 2016 6:23 pm

Re: cookie based auth using fetch

Mon Jan 01, 2024 5:40 pm

##### mikrotik script to fetch the current signal strength from an lbe or af60 using cookie based authentication
/tool
{
:local username <user>
:local password <pass>
:local ip <lbeip>
:local loginurl "https://$ip/api/auth"
:local statusurl "https://$ip/status.cgi"
:local strData [fetch mode=https url=$loginurl http-method=post http-data="username=$username&password=$password" output=user-with-headers as-value ];
:local headerData ([:toarray $strData ]->"http-headers");
:local headerData ($strData->"http-headers");
:local cookieData [:pick $headerData 4]
:local cookie [:pick $cookieData ([:find $cookieData " "]+1) [:find $cookieData ";"]]
:local header "content-type: application/json,Cookie: $cookie; ok=1" ;
:put $header
:set strData [fetch mode=https url=$statusurl http-method=get http-header-field=$header output=user as-value ];
:set strData ($strData->"data");
:local startSignal [:pick $strData ([:find $strData "\"signal\":"]+9) [:len $strData]]
:local signalStrenght [:pick $startSignal 0 [:find $startSignal ","]]
:put "Current signal strength = $signalStrenght"
}

Who is online

Users browsing this forum: No registered users and 8 guests