Sign in to follow this  
Followers 0
JRSmile

Opsview Windows Rest API Client

1 post in this topic

#1 ·  Posted (edited)

Hi there folks,

every two years or so i have a new script for you...

below you will find my approach to get some infos from the opsview.com rest api.

i might extend this to a full software but for now here is the basic authentication and information gathering of hashtags proove of concept.

Spoiler
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\..\..\..\Pictures\favicon.ico
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Res_Comment=JR's Opsview Client
#AutoIt3Wrapper_Res_Description=JOC
#AutoIt3Wrapper_Res_Fileversion=0.1.0.0
#AutoIt3Wrapper_Res_LegalCopyright=JRSmile
#AutoIt3Wrapper_Res_Language=1031
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_Run_Au3Stripper=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.14.1
    Author:         JRSmile

    Script Function:
    Opsview Windows Client

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include-once
#include "winhttp.au3"
#include "OO_JSON.au3"
Opt("MustDeclareVars", 1)

Global Const $STR_ENDISSTART = 0 ; End acts as next start when end = start
Global Const $STR_ENDNOTSTART = 1 ; End does not act as new start when end = start
Global Const $STR_REGEXPARRAYGLOBALMATCH = 3 ; Return array of global matches.



;Proxy Config
Global $aIEproxy = _WinHttpGetIEProxyConfigForCurrentUser()
Global $sProxy = $aIEproxy[2]

Global $aToken = ""
Global $sToken = ""
Global $oJSON = _OO_JSON_Init()
Global $obj = ""

Global $sUserName = "username"
Global $sPassword = "password"
Global $sDomain = "opsview.server.local"
Global $sPage = "rest/login"
Global $sAdditionalData = '{ username => "' & $sUserName & '", password => "' & $sPassword & '" }'
;~ Global $hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) JOC/0.1", $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $sProxy) ; with ie proxy settings
Global $hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) JOC/0.1") ; without ie proxy settings
Global $hConnect = _WinHttpConnect($hOpen, $sDomain)
Global $sReturned = _WinHttpSimpleSSLRequest($hConnect, "POST", $sPage, Default, $sAdditionalData, "Content-Type: text/x-data-dumper" & @CRLF)
_WinHttpCloseHandle($hConnect)
$aToken = _StringBetween($sReturned, "'", "'", $STR_ENDNOTSTART, True)
$sToken = $aToken[1]
$hConnect = _WinHttpConnect($hOpen, $sDomain)
$sReturned = _WinHttpSimpleSSLRequest($hConnect, "GET", "rest/status/viewport/HASHTAG", Default, $sAdditionalData, "Content-Type: application/json" & @CRLF & "X-Opsview-Username: " & $sUserName & @CRLF & "X-Opsview-Token: " & $sToken & @CRLF)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
; See what's returned
Global $obj = $oJSON.parse($sReturned)

For $i = 0 To $obj.list.length - 1
    ConsoleWrite($obj.list.item($i).services.item(0).state & ": ")
    ConsoleWrite($obj.list.item($i).name & " --> ")
    ConsoleWrite($obj.list.item($i).services.item(0).output & @CRLF)
Next


Func _StringBetween($sString, $sStart, $sEnd, $iMode = $STR_ENDISSTART, $bCase = False)
    ; If starting from beginning of string
    $sStart = $sStart ? "\Q" & $sStart & "\E" : "\A"

    ; Set mode
    If $iMode <> $STR_ENDNOTSTART Then $iMode = $STR_ENDISSTART

    ; If ending at end of string
    If $iMode = $STR_ENDISSTART Then
        ; Use lookahead
        $sEnd = $sEnd ? "(?=\Q" & $sEnd & "\E)" : "\z"
    Else
        ; Capture end string
        $sEnd = $sEnd ? "\Q" & $sEnd & "\E" : "\z"
    EndIf

    ; Set correct case sensitivity
    If $bCase = Default Then
        $bCase = False
    EndIf

    Local $aReturn = StringRegExp($sString, "(?s" & (Not $bCase ? "i" : "") & ")" & $sStart & "(.*?)" & $sEnd, $STR_REGEXPARRAYGLOBALMATCH)
    If @error Then Return SetError(1, 0, 0)
    Return $aReturn
EndFunc   ;==>_StringBetween

 

you will need the winhttp.au3 and oo_json.au3 (use the forum search), additional thanks to trancexx for being awesome.

Edited by JRSmile

$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])))''Chr("a")&"HI"Next;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • wakillon
      By wakillon
      HttpHeaderWatcher v1.0.1.3
       

      Some time ago, some members asked how to see the Http Requests.
      There are quite a few external applications but not in AutoIt!
      HttpHeaderWatcher in association with WinPcap, very modestly solves this lack.
       
      Once done this http watcher, i asked me : why not Re-build a WinHttp Request in AutoIt from a selected Request in one Click ?
      So i have added a "Create au3" button who open the WinHttp Request of your choice in AutoIt format in SciTE Editor.
      Voila voila, hope it can help ! 
      Buttons were made online with chimply.com the easy and free buttons generator !
      See Help for more infos.
      previous downloads : 253
      source and executable are available in the Download Section
      Hope you like it !
    • jandings
      By jandings
      Hello there,
      since I spent some time to access the REST-API of the web shop system Shopware, I'd like to share a few lines to make life easier for others.
      With this you can access your Shopware database, reading and writing all kind of data. 
      Replies are JSON style.
      To work with JSON I usually depend on either just own AutoIt string operations or this AutoIt library:
      ; File        : Json.au3 (2015.01.08)
      ; Purpose    : A Non-Strict JavaScript Object Notation (JSON) Parser UDF
      ; Author    : Ward
      $UserName="xxxx" ;Shopware credentials of Shopware user who has the API checkbox ticked $PassWord="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;API-Key of this user, NOT the Password $URL="www.XXX.com/api/" $Command="orders/64682?useNumberAsId=true" ;A list of commands and options is available through shopware REST-API help; Here we read order number 64682 $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "https://" & $URL & $Command , False) $oHTTP.SetCredentials($UserName, $PassWord, 0) $oHTTP.Send() $response = $oHTTP.ResponseText ConsoleWrite ($response & @CRLF & @CRLF)  
    • jandings
      By jandings
      Hello there,
      and one more: how to access the REST-API of the project management system plan.io (which is based on Redmine), I'd like to share a few lines to make life easier for others.
      With this you can access your plan.io data, reading and writing all kind of data. 
      Replies are JSON style (though XML is possible, that's an option)
      To work with JSON I usually depend on either just own AutoIt string operations or this AutoIt library, which I find best 11.2017:
      ; File        : Json.au3 (2015.01.08)
      ; Purpose    : A Non-Strict JavaScript Object Notation (JSON) Parser UDF
      ; Author    : Ward
      $UserName="xxxxxx" ;plan.io credentials (usually e-mail address) $PassWord="xxxx" ;plan.io credentials $APIKey="xxxxxxxxxxxxxxx" ;This is the API Key from PlanIO, you can get it from the frontend, just google or ask plan.io help $URL="xxxx.plan.io/" $Command="issues.json?limit=50" ;A list of commands and options is available through plan.io REST-API help $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "https://" & $URL & $Command &" X-Redmine-API-Key: " & $APIKey, False) $oHTTP.SetCredentials($UserName, $PassWord, 0) $oHTTP.Send() $response = $oHTTP.ResponseText ConsoleWrite ($response & @CRLF & @CRLF)
    • nhardel
      By nhardel
      I have been working on trying to develop some scripts to interface with the REST/JSON API from the Orion SDK.  This is where I will ask my questions and hopefully get some community responses that could help benefit others. 
      https://github.com/solarwinds/OrionSDK
      I am trying to create examples of how to interface with the API from autoit.  This should be a knowledge dump for this task. 
    • n3wbie
      By n3wbie
      How to send Requests on https Website
      I tried using
      ObjCreate("winhttp.winhttprequest.5.1")
      But m not Receiving Any response
      m able to retrive https://google.com but same is not available on other site( https://gst.gov.in )
      kindly help me