What's new

Solved Odd Request - Anyone know MS Powershell and how to convert to curl?

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

JGrana

Very Senior Member
Before I'm flamed for this post - let me explain. Also, long post. If you don't know MS Powershell - stop here and go to the next thread.

I moved to an area of California that has "so-so" internet access. Like 10Mbit DSL and Hughesnet. I tried DSL - no-go. But T-Mobile has reasonable LTE and a little 5G in my area. So, I am using a Sagemcom T-Mobile Gateway. Most of the time I get 120+Mbits. But, at times like early morning, a little while in the evening - the speeds either degrade to less than 15Mbit or the Gateway goes offline and wants a power cycle.

The reason for posting here? By using Asuswrt-merlin I'm able to use spdMerlin (with a little added code) and a separate ping script to monitor speed/off-line and power cycle the Gateway with a Smart Plug (TP-Link) using a script I ported (github hs100.sh).

It bothers the engineer in me that I power cycle this Gateway 2-4 times a day. I need a way to issue a "reboot" command to the Gateway - in a script.

The best I could find was a PowerShell script that should/might work.
The problem is that I can grok Linux, bash and even curl - but this PowerShell stuff is over my head.
Googling PowerShell to curl resulted in little help...Lots of Linux curl to PowerShell (sadly).

So - anyone that knows how to migrate a PowerShell script to Linux/bash/curl (or even wget), PM me.
 
I'm not an expert in PowerShell but if you post the script we might be able to do something if it's fairly simple.
 
Thanks Colin. Here is the scripts. Once I get it converted, I believe adding a reboot via an invoke would be straightforward.

Code:
$ErrorActionPreference = 'SilentlyContinue'
function token
{
   
$Pass = Read-Host "Enter Password For The Gateway"
$body = @"
{
"username": "admin",
"password": "$Pass"
}
"@
$login = Invoke-RestMethod -Method POST -Uri "http://192.168.12.1/TMI/v1/auth/login" -Body $body
$token = $login.auth.token
$global:header = @{Authorization="Bearer $token"}
}

function Show-Menu
{
    param (
        [string]$Title = 'My Menu'
    )
    Clear-Host
    Write-Host "Options for Gateway"
   
    Write-Host "1: Press '1' to Turn Off 2.4G Wifi."
    Write-Host "2: Press '2' to Turn On 2.4G Wifi."
    Write-Host "3: Press '3' to Turn Off 5G Wifi."
    Write-Host "4: Press '4' to Turn On 5G Wifi."
    Write-Host "5: Press '5' to Reboot Gateway."
    Write-Host "6: Press '6' to Download Config to Verify Changes."
    Write-Host "Q: Press 'Q' to Quit."
}

 
function wifi-off-24
{

$response = Invoke-RestMethod -Uri "http://192.168.12.1/TMI/v1/network/configuration?get=ap" -headers $global:header -o .\config.txt
((Get-Content -path .\config.txt -Raw) -Replace '"2.4ghz":{"isRadioEnabled":true','"2.4ghz":{"isRadioEnabled":false') | Set-Content -Path .\config.txt
$response = Invoke-RestMethod -TimeoutSec 1 -Method POST -Uri "http://192.168.12.1/TMI/v1/network/configuration?set=ap" -headers $global:header -body (Get-Content .\config.txt) -ContentType "application/json"

}
 

 function wifi-on-24
 {

$response = Invoke-RestMethod -Uri "http://192.168.12.1/TMI/v1/network/configuration?get=ap" -headers $global:header -o .\config.txt
((Get-Content -path .\config.txt -Raw) -Replace '"2.4ghz":{"isRadioEnabled":false','"2.4ghz":{"isRadioEnabled":true') | Set-Content -Path .\config.txt
$response = Invoke-RestMethod -TimeoutSec 1 -Method POST -Uri "http://192.168.12.1/TMI/v1/network/configuration?set=ap" -headers $global:header -body (Get-Content .\config.txt) -ContentType "application/json"

}
function wifi-off-5
{

$response = Invoke-RestMethod -Uri "http://192.168.12.1/TMI/v1/network/configuration?get=ap" -headers $global:header -o .\config.txt
((Get-Content -path .\config.txt -Raw) -Replace '"5.0ghz":{"isRadioEnabled":true','"5.0ghz":{"isRadioEnabled":false') | Set-Content -Path .\config.txt
$response = Invoke-RestMethod -TimeoutSec 1 -Method POST -Uri "http://192.168.12.1/TMI/v1/network/configuration?set=ap" -headers $global:header -body (Get-Content .\config.txt) -ContentType "application/json"

}
 

 function wifi-on-5
 {

$response = Invoke-RestMethod -Uri "http://192.168.12.1/TMI/v1/network/configuration?get=ap" -headers $global:header -o .\config.txt
((Get-Content -path .\config.txt -Raw) -Replace '"5.0ghz":{"isRadioEnabled":false','"5.0ghz":{"isRadioEnabled":true') | Set-Content -Path .\config.txt
$response = Invoke-RestMethod -TimeoutSec 1 -Method POST -Uri "http://192.168.12.1/TMI/v1/network/configuration?set=ap" -headers $global:header -body (Get-Content .\config.txt) -ContentType "application/json"

}


function config
{

$response = Invoke-RestMethod -Uri "http://192.168.12.1/TMI/v1/network/configuration?get=ap" -headers $global:header -o .\config.txt

}

function reboot
{

$response = Invoke-RestMethod -TimeoutSec 1 -Method POST -Uri "http://192.168.12.1/TMI/v1/gateway/reset?set=reboot" -headers $global:header

}



function menu
{


Show-Menu -Title 'My Menu'
 $selection = Read-Host "Please make a selection"
 switch ($selection)
 {
     '1' {
         
'Turning off 2.4G Wifi'
          wifi-off-24
  'Returning to Menu'
           Start-Sleep -s 1
          menu
       
     } '2' {
         
'Turning on 2.4G Wifi'
          wifi-on-24
  'Returning to Menu'
 Start-Sleep -s 1
          menu
       
       
     }'3' {
         
'Turning off 5G Wifi'
          wifi-off-5
  'Returning to Menu'
           Start-Sleep -s 1
          menu
       
     } '4' {
         
'Turning on 5G Wifi'
          wifi-on-5
  'Returning to Menu'
 Start-Sleep -s 1
          menu
     } '5' {
         
'Rebooting Gateway'
Start-Sleep -s 1

          reboot
          return
       
     } '6' {
          'Downloading config'
          config
          'Returning to Menu'
          Start-Sleep -s 1
          menu
         
                   
     } 'q' {
         return
     }
 }

 }
token
menu

$response
 
Just a thought - have you tried ChatGPT? It might be able to do that kind of conversion/rewrite task.
 
Just a thought - have you tried ChatGPT? It might be able to do that kind of conversion/rewrite task.

They're starting to notice that it can even predict the stock market. It's starting to get a bit concerning.
 
You may be able to modify a script that already exists on either this forum or the add on forum.

It's called chkwan. It monitors the wan connection. If it's down you can set it to reset the wan connection or reboot the router. In your case you could modify it to reboot your gateway.
 
I wonder if this would reboot your gateway if you entered it into your browser:

Code:
http://admin:password@192.168.12.1/TMI/v1/gateway/reset?set=reboot
 
Just a thought - have you tried ChatGPT? It might be able to do that kind of conversion/rewrite task.
Hmm, never that of that.
Just tried ChatGPT.
Wow.
It actually converted the script. It didn’t convert the entire script, stopped ~70% of the way through it.
I edited the PowerShell to remove all the disable of WiFi’s and it converted the entire PS script into Linux curl.
And, is somewhat working!!! I am able to get the proper Auth token and succesfully read the Gatways config (option 6).
The reboot fails with a 411 Length Required.
At least I can now debug and hopefully have a script method to reboot the Gateway.
For this curious, here is the edited (shortened) PowerShell script:

$ErrorActionPreference = 'SilentlyContinue'
function token
{

$Pass = Read-Host "Enter Password For The Gateway"
$body = @"
{
"username": "admin",
"password": "$Pass"
}
"@
$login = Invoke-RestMethod -Method POST -Uri "http://192.168.12.1/TMI/v1/auth/login" -Body $body
$token = $login.auth.token
$global:header = @{Authorization="Bearer $token"}
}

function Show-Menu
{
param (
[string]$Title = 'My Menu'
)
Clear-Host
Write-Host "Options for Gateway"

Write-Host "5: Press '5' to Reboot Gateway."
Write-Host "6: Press '6' to Download Config to Verify Changes."
Write-Host "Q: Press 'Q' to Quit."
}

function config
{

$response = Invoke-RestMethod -Uri "http://192.168.12.1/TMI/v1/network/configuration?get=ap" -headers $global:header -o .\config.txt

}

function reboot
{

$response = Invoke-RestMethod -TimeoutSec 1 -Method POST -Uri "http://192.168.12.1/TMI/v1/gateway/reset?set=reboot" -headers $global:header

}

function menu
{


Show-Menu -Title 'My Menu'
$selection = Read-Host "Please make a selection"
switch ($selection)
{
'5' {

'Rebooting Gateway'
Start-Sleep -s 1

reboot
return

} '6' {
'Downloading config'
config
'Returning to Menu'
Start-Sleep -s 1
menu


} 'q' {
return
}
}

}
token
menu

$response

And the converted by ChatGPT using the line “Please convert this PowerShell script to Linux using curl”

#!/bin/bash
ErrorActionPreference='SilentlyContinue'

function token {
read -s -p "Enter Password For The Gateway: " PASS
BODY=$(cat <<EOF
{
"username": "admin",
"password": "$PASS"
}
EOF
)
LOGIN=$(curl -s -X POST -H 'Content-Type: application/json' -d "$BODY" 'http://192.168.12.1/TMI/v1/auth/login')
TOKEN=$(echo "$LOGIN" | jq -r '.auth.token')
export HEADER="Authorization: Bearer $TOKEN"
}

function ShowMenu {
clear
echo "Options for Gateway"
echo "5: Press '5' to Reboot Gateway."
echo "6: Press '6' to Download Config to Verify Changes."
echo "Q: Press 'Q' to Quit."
}

function config {
curl -s -H "$HEADER" 'http://192.168.12.1/TMI/v1/network/configuration?get=ap' -o config.txt
}

function reboot {
curl -s -m 1 -X POST -H "$HEADER" 'http://192.168.12.1/TMI/v1/gateway/reset?set=reboot'
}

function menu {
ShowMenu
read -p "Please make a selection: " SELECTION
case $SELECTION in
5)
echo "Rebooting Gateway"
sleep 1
reboot
return;;
6)
echo "Downloading config"
config
echo "Returning to Menu"
sleep 1
menu;;
q|Q)
return;;
esac
}

token
menu
 
I wonder if this would reboot your gateway if you entered it into your browser:

Code:
http://admin:password@192.168.12.1/TMI/v1/gateway/reset?set=reboot
I did try this first, thanks. Interesting enough in that I at least got a response from the Gateway - something about an Invalid procedure.

I am going to play around with both the ChatGPT converted and maybe even this one liner.

Thanks all.
 

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top