Jump to content

Get automatic token as hostids increase API LogmeIn


great77
 Share

Recommended Posts

Thanks to you guys  that is helping on this forum. I am having this issue and really want to finish off with this project.

I have been working on this to automatically input variable for the host ids and get my API. The manual entry of numbers is working fine and can run the hardware inventory report, but the hostid might increase and am looking for putting automatically generated
hostids into variable and put it in below code. But error keep coming up.... "error deserializing the object type LMI PublicInventopry Unexpected character "$".

FileWrite($sFilePath, "]]" )
Local $Trim_final_coma = StringReplace(FileReadLine($sFilePath), ",]","")
;;;;ShellExecute($sFilePath)
MsgBox(0,"",  $Trim_final_coma)
Global $great = [112561236, 112842551, 45678967,444444,34567, 234334445]  ;;; this is few of the hostids and they are up to 50;;;;
 

$oHTTP.SetRequestHeader("Content-Type", "application/JSON; charset=utf-8")
$oHTTP.SetRequestHeader("Accept", 'application/json; charset=utf-8')

;;;;; $cmd1 = '{"hostIds": [112561236, 112842551]}'  ;;; This bit is manual entry of host id which is working

$cmd2 = '{"hostIds": $great}'  ;;;;;;; This bit not working

$oHTTP.Send($cmd2)

 

How can I manipulate the red highlighted code to take in this variables. Cheers

Edited by great77
Link to comment
Share on other sites

great77,

I suspect that you are going to have to construct a command string following the syntax of $cmd1.  Something like...

Global $great = [112561236, 112842551, 45678967,444444,34567, 234334445]  ;;; this is few of the hostids and they are up to 50;;;;
;;;;; $cmd1 = '{"hostIds": [112561236, 112842551]}'  ;;; This bit is manual entry of host id which is working

$cmd2 = '{"hostIds": ['

for $i = 0 to ubound($great) - 1
    $cmd2 &= $great[$i] & ( $i = ubound($great)-1 ? '' : ',' )
Next

$cmd2 &= ']}'

msgbox(0,0,$cmd2)

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

15 minutes ago, kylomas said:

great77,

I suspect that you are going to have to construct a command string following the syntax of $cmd1.  Something like...

Global $great = [112561236, 112842551, 45678967,444444,34567, 234334445]  ;;; this is few of the hostids and they are up to 50;;;;
;;;;; $cmd1 = '{"hostIds": [112561236, 112842551]}'  ;;; This bit is manual entry of host id which is working

$cmd2 = '{"hostIds": ['

for $i = 0 to ubound($great) - 1
    $cmd2 &= $great[$i] & ( $i = ubound($great)-1 ? '' : ',' )
Next

$cmd2 &= ']}'

msgbox(0,0,$cmd2)

kylomas

Thanks Kylomas.  It is returning "hostIds":[]} . Any reason why ? Thanks

Link to comment
Share on other sites

_FileCreate(@scriptdir & "\list.txt")
Local $sFilePath = @scriptdir & "\list.txt"
 FileWrite($sFilePath, "[" )
For $m = 3 To UBound($aResult) - 1

    ;_FileWriteFromArray($sFilePath, $aResult , 3, Default)

 FileWrite($sFilePath, $aResult[$m] & "," )


Next
FileWrite($sFilePath, "]]" )
Global $Trim_final_coma = StringReplace(FileReadLine($sFilePath), ",]","")
;;;;ShellExecute($sFilePath)
;;;;MsgBox(0,"",  $Trim_final_coma)

; List all the files in the current script directory.









; Creating the object
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("post", 'https://secure.logmein.com/public-api/v1/inventory/hardware/REPORTS', false)
$oHTTP.SetRequestHeader("Authorization", '{"companyId":"1dddd","psk":"00_l8gggphj"}')
;; $oHTTP.SetRequestHeader("User-Agent", "User-Agent=Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0")
$oHTTP.SetRequestHeader("Content-Type", "application/JSON; charset=utf-8")
$oHTTP.SetRequestHeader("Accept", 'application/json; charset=utf-8')
;;$cmd1 = '{"hostIds": [112561236, 112842551]}' ; ;"fields": ["cpuType", "cpuSpeed", "cpuNumberOfCores"]}'
Global $great = "[112561236, 112842551]"  ;;; this is few of the hostids and they are up to 50;;;;
 ;;;$cmd1 = '{"hostIds": [112561236, 112842551]}'  ;;; This bit is manual entry of host id which is working

$cmd2 = '{"hostIds": ['

for $y = 0 to ubound($great) - 1
    $cmd2 &= $great[$y] & ( $y = ubound($great)-1 ? '' : ',' )
Next

$cmd2 &= ']}'

msgbox(0,0,$cmd2)
;$cmd1 = '{"hostIds": $great}'



; Performing the Request


$oHTTP.Send($cmd2)

That is the code and it is working as I perceive, it just that it returning empty. There may need to be a tweaking around.

Link to comment
Share on other sites

17 minutes ago, mikell said:

A similar way...

#include <Array.au3>

Global $great = [112561236, 112842551, 45678967,444444,34567, 234334445]  ;;; this is few of the hostids and they are up to 50;;;;
 
$str = "[" & _ArrayToString($great, ",") & "]"
$cmd2 = '{"hostIds":' & $str & '}' 
Msgbox(0,"", $cmd2)

 

It is returning "hostIds":-1} . Thanks

Link to comment
Share on other sites

Obviously it's quite different if $great is a string and not an array...

Global $great = "[112561236, 112842551, 45678967,444444,34567, 234334445]"  ;;; this is few of the hostids and they are up to 50;;;;
 
$cmd2 = '{"hostIds":' & $great & '}' 
Msgbox(0,"", $cmd2)

Edit
Confusion caused by quotes missing in post #1

Edited by mikell
Link to comment
Share on other sites

5 minutes ago, mikell said:

Obviously it's quite different if $great is a string and not an array...

Global $great = "[112561236, 112842551, 45678967,444444,34567, 234334445]"  ;;; this is few of the hostids and they are up to 50;;;;
 
$cmd2 = '{"hostIds":' & $great & '}' 
Msgbox(0,"", $cmd2)

 

Thanks. It is working when l leave your quote with the few hostids, and when I use the whole variable about 70 hostids which is generated from an excel file , it is returning [].

Please see below the variable generated [17473462,24597552,27549918,28888061,30751956,30982721,31123310,31274429,37484619,40521545,40751610,42808467,47764071,49790571,50608746,50900789,53603099,55044399,56452198,57499521,62276565,63094230,65426381,65639556,65647705,75753329,75803986,78137743,78162454,81862402,81864310,85126671,85219533,115810898,115885689,115900428,116622142,116745395,117006517,117026391,117340217,117557068,117648261,117773770,117838277,117946540,117951462,118349313,118507137,119130767,119212976,119887439,119948448,120095186,120111132,120111397,120112285,120125743,120192892,120351925,120485248,120518717,121016412,121099159,121113177]

Link to comment
Share on other sites

13 minutes ago, kylomas said:

remove the quotes...

Global $great = "[112561236, 112842551]"

 

Yours is working when I remove the quote however it only works for few hostids. The hostids are generated from excel file and then read from text file which generated this ...  It will change and am looking to automate with the application.[17473462,24597552,27549918,28888061,30751956,30982721,31123310,31274429,37484619,40521545,40751610,42808467,47764071,49790571,50608746,50900789,53603099,55044399,56452198,57499521,62276565,63094230,65426381,65639556,65647705,75753329,75803986,78137743,78162454,81862402,81864310,85126671,85219533,115810898,115885689,115900428,116622142,116745395,117006517,117026391,117340217,117557068,117648261,117773770,117838277,117946540,117951462,118349313,118507137,119130767,119212976,119887439,119948448,120095186,120111132,120111397,120112285,120125743,120192892,120351925,120485248,120518717,121016412,121099159,121113177]

Link to comment
Share on other sites

great77,

You are probably leaving the array definition in your code.  This is my understanding of your process..

; generate text file of hostid's

local $sFname = @scriptdir & '\hostids.txt', $sOut
for $i = 1 to 50
    for $j = 1 to 9
        $sOut &= random(1,9,1)
    Next
    $sOut &= @CRLF
Next

filewrite($sFname,$sOut)
shellexecute($sFname)

; CREATE $cmd2

locaL $great = FileReadToArray($sFname)

$cmd2 = '{"hostIds": ['

for $i = 0 to ubound($great) - 1
    $cmd2 &= $great[$i] & ( $i = ubound($great)-1 ? '' : ',' )
Next

$cmd2 &= ']}'

msgbox(0,0,$cmd2)

Obviously substitute your txt file.  For further help you must post code, not just describe what it is doing.

Good Luck,

kylomas

Edited by kylomas
spelling

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

34 minutes ago, kylomas said:

great77,

You are probably leaving the array definition in your code.  This is my understanding of your process..

; generate text file of hostid's

local $sFname = @scriptdir & '\hostids.txt', $sOut
for $i = 1 to 50
    for $j = 1 to 9
        $sOut &= random(1,9,1)
    Next
    $sOut &= @CRLF
Next

filewrite($sFname,$sOut)
shellexecute($sFname)

; CREATE $cmd2

locaL $great = FileReadToArray($sFname)

$cmd2 = '{"hostIds": ['

for $i = 0 to ubound($great) - 1
    $cmd2 &= $great[$i] & ( $i = ubound($great)-1 ? '' : ',' )
Next

$cmd2 &= ']}'

msgbox(0,0,$cmd2)

Obviously substitute your txt file.  For further help you must post code, not just describe what it is doing.

Good Luck,

kylomas

Here is the code , apologies not posting the whole code.... Cheers and thank you

#NoTrayIcon
; *** Start added by AutoIt3Wrapper ***
#include <APIDiagConstants.au3>
; *** End added by AutoIt3Wrapper ***
Opt("WinTitleMatchMode", 1) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
;; Global Const $SEM_FAILCRITICALERRORS = 1

; This is the line. You are telling: I'll handle possible displaying of critical errors myself
SetErrorMode($SEM_FAILCRITICALERRORS)

ConsoleWrite(FileOpen("A:") & @CRLF) ; This line would create system's critical-error-handler message box for my system if I haven't called SetErrorMode passing SEM_FAILCRITICALERRORS argument


Func SetErrorMode($iMode)
    Local $aCall = DllCall("kernel32.dll", "dword", "SetErrorMode", "dword", $iMode)
    If @error Then Return SetError(1, 0, 0)
    Return $aCall[0]
EndFunc   ;==>SetErrorMode
Opt("TrayMenuMode", 1) ;0=append, 1=no default menu,
Opt("TrayAutoPause", 0) ;0=no pause, 1=Pause

#include <array.au3>
#include <Timers.au3>
#include <File.au3>
#include <String.AU3>
#include <Array.au3>; Not Required, Only Needed For _ArrayDisplay()
#include <Inet.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>
#include <GUIConstants.au3>
#Include <Constants.au3>
#include <APIConstants.au3>
#include <WinAPIEx.au3>
#include <EventLog.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#Include <GDIPlus.au3>
#include <EventLog.au3>
#include <IE.au3>
#include "WinHttp.au3"
#include <excel.au3>
;#include "JSON.au3"
;#include "curl.au3"






Local $cmd1,$cmd2, $Final_token_id,  $search_token, $sPD, $token_id, $oHTTP, $q, $oHTTP2,$Domain, $Domain2, $ade, $status, $ade1,  $file2, $ade2, $sPublicIP, $MyFile, $objWMIService, $colSMBIOS, $oReceived2, $Serial, $Assettag, $longlatstring, $longlatstring2, $i, $j,$aSRE, $aRegion, $ade3, $h, $sdd, $url_temp
Local $text1,$text2,$text3,$sVar, $CurrentDrive,$mydrive, $cI_Compname,$Users, $myfile,$file, $url,$headerCL, $headerServer, $headerD, $headerLM, $headerCT,$headerAllResponse, $headerSuccess,$headerAllComputerStatus,$oStatusCode,$oReceived, $restReq
Opt("MustDeclareVars", 1)


;$MyFile = @ComputerName & ".txt"

;HotKeySet("{ESC}", "_Quit")

Local $url, $WinHttpReq, $oErr, $headerLM, $headerD,$ade

$oErr = ObjEvent("AutoIt.Error", "_ErrorHandler")
;;;;$restReq = ObjCreate("winhttp.winhttprequest.5.1")

While 1
    ;$file = FileOpen($MyFile, 2)




; The data to be sent

$myfile =@ScriptDir & "\Report_Files_API\Get_Host_List.xls"

Local $oExcel =  _Excel_Open()
_Excel_Bookopen($oExcel, $myfile)
Local $oAppl = _Excel_Open()

If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Local $oWorkbook = _Excel_BookOpen($oAppl, @ScriptDir & "\Report_Files_API\Get_Host_List.xls")
global $iCounter = 4
Global $aResult = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("A:A"), 2, False) ;; 2 return formulat and True, transpose
;;;Local $idlist = _ArrayDisplay($aResult, "Excel UDF: _Excel_RangeRead Example 3 - Formulas in column K", "3:", 1 , "," ) ; 1 transpose the array

_FileCreate(@scriptdir & "\list.txt")
Local $sFilePath = @scriptdir & "\list.txt"

 FileWrite($sFilePath, '[' )
For $m = 3 To UBound($aResult) - 1

    ;_FileWriteFromArray($sFilePath, $aResult , 3, Default)

 FileWrite($sFilePath, $aResult[$m] & "," )


Next
FileWrite($sFilePath, ']]' )
Global $Trim_final_coma = StringReplace(FileReadLine($sFilePath), ",]", "")
;;;;ShellExecute($sFilePath)
MsgBox(0,"return",  $Trim_final_coma)

; List all the files in the current script directory.









; Creating the object
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("post", 'https://secure.logmein.com/public-api/v1/inventory/hardware/REPORTS', false)
$oHTTP.SetRequestHeader("Authorization", '{"companyId":"113","psk":"00_lotn3kb3h1koqvt078gggphj"}')
;; $oHTTP.SetRequestHeader("User-Agent", "User-Agent=Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0")
$oHTTP.SetRequestHeader("Content-Type", "application/JSON; charset=utf-8")
$oHTTP.SetRequestHeader("Accept", 'application/json; charset=utf-8')
;;$cmd1 = '{"hostIds": [112561236, 112842551]}' ; ;"fields": ["cpuType", "cpuSpeed", "cpuNumberOfCores"]}'
Global $great = $Trim_final_coma ;;; this is few of the hostids and they are up to 50;;;;
 ;;;$cmd1 = '{"hostIds": [112561236, 112842551]}'  ;;; This bit is manual entry of host id which is working
$cmd2 = '{"hostIds":' & $great  & '}'
;$cmd2 = '{"hostIds": ['

;for $y = 0 to ubound($great) - 1
 ;   $cmd2 &= $great[$y] & ( $y = ubound($great)-1 ? '' : ',' )
;Next

;$cmd2 &= ']}'

msgbox(0,0,$cmd2)
;$cmd1 = '{"hostIds": $great}'

;$oHTTP.SetRequestHeader("hostIds", '17473462, 1234567891')
;$oHTTP.SetRequestHeader("fields", "cpuType", "cpuSpeed", "cpuNumberOfCores")

; Performing the Request


$oHTTP.Send($cmd2)

; Download the body response if any, and get the server status response code.
$oReceived = $oHTTP.ResponseText
sleep(1000)
 $token_id = StringTrimRight($oReceived, 2)
 $Final_token_id = StringTrimleft($token_id, 43)
sleep(1000)
MSGBOX(0, "Token details ", $final_token_id)

$oStatusCode = $oHTTP.Status
MsgBox(4096, "Result", $oHTTP.ResponseText())
MsgBox(4096, "Status", $oHTTP.Status)
$oHTTP.Send
If $oStatusCode <> 200 then
 MsgBox(4096, "Response code", $oStatusCode)
EndIf

; Saves the body response regardless of the Response code
 $file = FileOpen("Received.txt", 2) ; The value of 2 overwrites the file if it already exists
 FileWrite($file, $oReceived)
$oHTTP2 = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP2.Open("Get", 'https://secure.logmein.com/public-api/v1/inventory/hardware/REPORTS/' & $final_token_id, false)
$oHTTP2.SetRequestHeader("Authorization", '{"companyId":"143","psk":"00_lotbz8w550t078gggphj"}')
sleep(1000)
 $oHTTP2.Send()
$oReceived2 = $oHTTP2.ResponseText
MsgBox(4096, "Result", $oHTTP2.ResponseText())
 $file2 = FileOpen("hardware_inventory.txt", 2) ; The value of 2 overwrites the file if it already exists
 FileWrite($file2, $oReceived2)

 FileClose($file)





    ;MsgBox(4096, "Result", $restReq.ResponseText())
   ; MsgBox(4096, "Status", $restReq.Status)


;;; EndIf ; end if of check no error



   ; $headerLM = $WinHttpReq.GetResponseHeader("Last-Modified")
   ; $headerD = $WinHttpReq.GetResponseHeader("Date")
    ;$headerCL = $WinHttpReq.GetResponseHeader("Content-Length")
    ;$headerServer = $WinHttpReq.GetResponseHeader("Server")



  $headerAllResponse = $restReq.GetAllResponseHeaders()

$headerAllComputerStatus = $restReq.ResponseText



    ;; FileWriteLine($file, " All Details: " & @CRLF & "..................................................................." & @CRLF)

    ;FileWriteLine($file,  @CRLF &  $headerAllResponse & @CRLF)
   FileWriteLine($file,  @CRLF &  $headerAllComputerStatus & @CRLF)
   Sleep(3000)
    ExitLoop ; not there originally remove to allow constant exchange of API
WEnd

Func _Quit()
    Exit
EndFunc

Func _ErrorHandler()
    ;;;; ConsoleWrite($oErr.description & @CRLF)

;;; FileWriteLine($file, " The : " & $oErr.description & @CRLF)

EndFunc









;◾Example:
; Request:      https://secure.logmein.com/ws/api/reportGet.aspx?companyid=<companyid>&psk=<psk>&reportid=computerstatus

;;;Response:     OK
   ;;;; Host ID Group ID  Computer Description    Product Type    Subscription Type   Start Date  Renewal Date    Status  Last online Last Remote Session Last known IP address   Install date    Software version
    ;;;;12345678    12345678    "abc" "LogMeIn Backup"  "Yearly"  2007-08-02 10:52:00   2007-08-02 11:12:31 "Marked for Cancel"  NULL    NULL    "192.168.1.165"  2007-08-02 10:52:00 580

Code has been edited now, the full code.....

excel_forum.xlsx

Edited by great77
Link to comment
Share on other sites

great77,

Can you post an example of your spread sheet (or some redacted version of it)?

kylomas

edit: can you also post the complete code.  Your while...wend is incomplete and you do not have an error handler function.

Edited by kylomas
additional info

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

great77,

This will work for constructing $cmd2.  As for the rest, I have not used the HTTP UDF so can't help much with that.  However, I would suggest putting together a meaningful error handler function.

#include <excel.au3>
#include <array.au3>

; Target hosts

local $myfile = @ScriptDir & "\Report_Files_API\excel_forum.xlsx"

Local $oExcel = _Excel_Open()
if @error then exit msgbox(17,'Excel Error', 'COM error code is ' & @extended)

local $oWorkbook = _Excel_BookOpen($oExcel, $myfile, false, false)
if @error then exit msgbox(17,'BookOpen Error', 'ERROR code is ' & @ERROR)

local $aResult = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("A:A"), 2, False) ;; 2 return formulat and True, transpose
if @error then exit msgbox(17,'RangeRead Error', 'ERROR code is ' & @ERROR)

_Excel_Close($oExcel)
if @error then exit msgbox(17,'Close Error', 'ERROR code is ' & @ERROR)

local $cmd2 ='{"hostIds":'
for $y = 3 to ubound($aResult) - 1
   $cmd2 &= $aResult[$y] & ( $y = ubound($aResult)-1 ? '' : ',' )
Next

$cmd2 &= ']}'

MsgBox(0, 0, $cmd2)

kylomas

Edited by kylomas
add _excel_close()

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Hi Kylomas,

 

Thank you so much. It is working now with the generated hostids automatically, I discovered that this  method makes me to find the problem of 3 hosts ID that has value "2" in the  Column B is causing the issue. When I removed them. It works great. It issues the token and also pull out the report.

My question is how whould I automatically remove rows that have value "2" in column B

The status of 2 might mean corrupt stuff.  Thanks

Link to comment
Share on other sites

great77,

Assuming that you are using the code I posted this will remove any hosted where column B = 2...

#include <excel.au3>
#include <array.au3>

; Target hosts

local $myfile = @ScriptDir & "\Report_Files_API\excel_forum.xlsx"

Local $oExcel = _Excel_Open()
if @error then exit msgbox(17,'Excel Error', 'COM error code is ' & @extended)

local $oWorkbook = _Excel_BookOpen($oExcel, $myfile, false, false)
if @error then exit msgbox(17,'BookOpen Error', 'ERROR code is ' & @ERROR)

local $aResult = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("A:B"), 2, False) ;; 2 return formulat and True, transpose
if @error then exit msgbox(17,'RangeRead Error', 'ERROR code is ' & @ERROR)

_Excel_Close($oExcel)
if @error then exit msgbox(17,'Close Error', 'ERROR code is ' & @ERROR)

_arraydisplay($aResult)

local $cmd2 ='{"hostIds":'
for $y = 3 to ubound($aResult) - 1
   if $aResult[$y][1] <> '2' then 
       $cmd2 &= $aResult[$y][0] & ( $y = ubound($aResult)-1 ? '' : ',' )
   Else
       ConsoleWrite('Dropped HostID = ' & $aResult[$y][0] & @CRLF)
    endif
Next

$cmd2 &= ']}'

MsgBox(0, 0, $cmd2)

;102673532|2

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...