#NoTrayIcon #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_outfile=UserLoggedOn.exe #AutoIt3Wrapper_Change2CUI=y #AutoIt3Wrapper_Res_Description=Checks to see if a user is logged onto a PC. #AutoIt3Wrapper_Res_Fileversion=0.4.0.0 #AutoIt3Wrapper_Res_Field=Compiled Script|AutoIt v3 Script : 3.3.6.1 #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.6.1 Author: Adam Lawrence (AdamUL) Script Function: Checks to see if a user is logged on a PC or Mac. #ce ---------------------------------------------------------------------------- #include If $CMDLINE[0] Then $sPCName = StringStripWS($CMDLINE[1], 3) ;Takes about 0.7 seconds for computer turned on, and 43 seconds for computer turned off. If StringInStr($sPCName, "libs-imac") Then $fUserLoggedOn = _UserLoggedOniMac($sPCName, "admin", "adminpassword") Else $fUserLoggedOn = _UserLoggedOnPC($sPCName) EndIf ;Takes about 43 seconds for computer turned on, and 85 seconds for a computer turned off. ;~ $fUserLoggedOnMac = _UserLoggedOniMac($sPCName, "admin", "adminpassword") ;~ $iUserLoggedOnMacError = @error ;~ $fUserLoggedOnPC = _UserLoggedOnPC($sPCName) ;~ $fUserLoggedOnPCError = @error ;~ $fUserLoggedOn = $iUserLoggedOnMacError And $fUserLoggedOnPCError If $fUserLoggedOn Then ConsoleWrite(@CRLF & $fUserLoggedOn & @CRLF) Exit 0 Else ConsoleWrite(@CRLF & $fUserLoggedOn & @CRLF) Exit 1 EndIf Else ConsoleWrite(@CRLF & "Enter Computer Name" & @CRLF) Exit 2 EndIf ;Uses WMI. ; #FUNCTION# ==================================================================================================================== ; Name ..........: _UserLoggedOnPC ; Description ...: Checks to see if a user is logged into the console on a PC. ; Syntax ........: _UserLoggedOnPC($sComputer) ; Parameters ....: $sComputer - A string of the host server name or IP Address. ; Return values .: Success - 1 - User logged into the computer. ; Failure - 0, sets @error to: ; |0 - User not logged in. ; |1 - Unable to create WMI object. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _UserLoggedOnPC($sComputer) Local $wbemFlagReturnImmediately = 0x10, _ ;DO NOT CHANGE $wbemFlagForwardOnly = 0x20 ;DO NOT CHANGE Local $colComputer, $objWMIService, $objComputer Local $sUserName = '', $sUserDomain = '' $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sComputer & "\root\cimv2") If Not IsObj($objWMIService) Then Return SetError(1, 2, True) ;Returns an Error is the it is $objWMIService is not an object. EndIf $colComputer = $objWMIService.ExecQuery("Select * from Win32_ComputerSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly) For $objComputer in $colComputer $sUserName = $objComputer.UserName ;MsgBox(0, "", $sComputer & ", Is logged into by "& $sUserName & ", " & _NowTime()) Next If $sUserName = "" Then Return False Return True EndFunc ;Uses Plink and SSH. ; #FUNCTION# ==================================================================================================================== ; Name ..........: _UserLoggedOniMac ; Description ...: Checks to see if a user is logged into the console on a Macintosh computer. ; Syntax ........: _UserLoggedOniMac($sComputer, $sSSHUserName, $sSSHPassword) ; Parameters ....: $sComputer - A string of the host server name or IP Address. ; $sSSHUserName - A string of the SSH User Name. ; $sSSHPassword- A string of the SSH Password. ; Return values .: Success - 1 - User logged into the computer. ; Failure - 0, sets @error to: ; |0 - User not logged in. ; |1 - Unable to connect to computer. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _UserLoggedOniMac($sComputer, $sSSHUserName, $sSSHPassword) Local $iPIDPlink = _PlinkConnect($sComputer, $sSSHUserName, $sSSHPassword) If @error Then Return SetError(1, 0, True) Local $sCmdLine = "who" & @CR _PlinkSend($iPIDPlink, $sCmdLine) Local $sUserName = _PlinkRead($iPIDPlink) _PlinkSend($iPIDPlink, "exit" & @CR) _PlinkExit($iPIDPlink) ;Cleanup output to show only user names by removing the command and the prompt. $sUserName = StringStripWS(StringReplace(StringReplace($sUserName, $sCmdLine, ""), $sComputer & ":~ " & $sSSHUserName & "$", ""), 3) Local $aUserName = StringSplit($sUserName, " console", 1) ;Split on " console" to get the desktop user. If @error Then $sUserName = "" Else $sUserName = $aUserName[1] EndIf ;~ ConsoleWrite(@CRLF & @CRLF & "$sUserName: " & $sUserName & @CRLF & @CRLF) ;For testing. If $sUserName = "" Then Return False Return True EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _PlinkConnect ; Description ...: Use Plink to connect to a remote server using SSH. ; Syntax ........: _PlinkConnect($sHostName, $sUserName, $sPassword) ; Parameters ....: $sHostName - A string of the host server name or IP Address. ; $sUserName - A string of the SSH User Name. ; $sPassword - A string of the SSH Password. ; Return values .: Success - $iPID - the PID of the Plink session. ; Failure - 0, sets @error to: ; |1 - Plink.exe not found in @ScriptDir. ; |2 - Error running Plink.exe. ; Author ........: spudw2k ; Modified ......: Adam Lawrence (AdamUL) ; Remarks .......: ; Related .......: _PlinkExit ; Link ..........: http://www.autoitscript.com/forum/topic/130536-interacting-with-a-remote-computer-via-ssh/page__p__910252#entry910252 ; Example .......: No ; =============================================================================================================================== Func _PlinkConnect($sHostName, $sUserName, $sPassword) $sEXE = @ScriptDir & "\plink.exe" ;~ $sEXE = "plink.exe" If Not FileExists($sEXE) Then Return SetError(1, 0, 0) $iPID = Run('"' & $sEXE & '" -ssh -pw ' & $sPassword & " " & $sUserName & "@" & $sHostName, @ScriptDir, @SW_HIDE, 0x1 + 0x8) ;Run SSH.EXE If Not $iPID Then Return SetError(2, 0, 0) $sReturn = _PlinkRead($iPID) ;Check for Login Success - Prompt ;~ ConsoleWrite(@CRLF & "$sReturn: " & $sReturn & @CRLF) ;For Testing. If StringInstr($sReturn, "Store key in cache? (y/n)") Then _PlinkSend($iPID, "y" & @CR) ;~ _PlinkSend($iPID, "n" & @CR) ;For Testing. $sReturn = _PlinkRead($iPID) ;~ ConsoleWrite(@CRLF & "$sReturn cache key loop: " & $sReturn & @CRLF) ;For Testing. EndIf If StringInstr($sReturn, "Access denied") Or StringInstr($sReturn, "FATAL") Or StringInstr($sReturn, "Using keyboard-interactive authentication") _ Or StringInstr($sReturn, "Unable to open connection") Or Not ProcessExists($iPID)Then _PlinkExit($iPID) Return SetError( 3, 0, 0) EndIf Return $iPID EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _PlinkRead ; Description ...: Read text data returned from the connected server. ; Syntax ........: _PlinkRead($iPID) ; Parameters ....: $iPID - PID returned from _PlinkConnect. ; Return values .: Success - String returned from StdOutRead of Plink. ; Failure - -1, sets @error to: ; |1 - Invaild Plink PID. ; Author ........: spudw2k ; Modified ......: Adam Lawrence (AdamUL) ; Remarks .......: ; Related .......: _PlinkSend ; Link ..........: http://www.autoitscript.com/forum/topic/130536-interacting-with-a-remote-computer-via-ssh/page__p__910252#entry910252 ; Example .......: No ; =============================================================================================================================== Func _PlinkRead($iPID) If Not $iPID Then Return SetError(1, 0, -1) Local $sDataA Local $sDataB Do $sDataB = $sDataA Sleep(100) $sDataA &= StdOutRead($iPID) ;~ ConsoleWrite(@CRLF & "$sDataA Loop: " & $sDataA & @CRLF) If @error Then ExitLoop Until $sDataB = $sDataA And $sDataA And $sDataB ;~ ConsoleWrite(@CRLF & "$sDataA: " & $sDataA & @CRLF) Return $sDataA EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _PlinkSend ; Description ...: Send text data to the connected server. ; Syntax ........: _PlinkSend($iPID, $sCmd) ; Parameters ....: $iPID - PID returned from _PlinkConnect. ; $sCmd - A string of the command to send. ; Return values .: Success - 1 ; Failure - 0, sets @error to: ; |StdinWrite @error code. ; Author ........: spudw2k ; Modified ......: Adam Lawrence (AdamUL) ; Remarks .......: ; Related .......: _PlinkRead ; Link ..........: http://www.autoitscript.com/forum/topic/130536-interacting-with-a-remote-computer-via-ssh/page__p__910252#entry910252 ; Example .......: No ; =============================================================================================================================== Func _PlinkSend($iPID, $sCmd) $iChars = StdinWrite($iPID,$sCmd) Return SetError(@error, 0, $iChars) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _PlinkExit ; Description ...: End a Plink session. ; Syntax ........: _PlinkExit($iPID) ; Parameters ....: $iPID - PID returned from _PlinkConnect. ; Return values .: Success - 1 ; Failure - 0, sets @error to: ; |ProcessClose @error code. ; Author ........: spudw2k ; Modified ......: Adam Lawrence (AdamUL) ; Remarks .......: ; Related .......: _PlinkConnect ; Link ..........: http://www.autoitscript.com/forum/topic/130536-interacting-with-a-remote-computer-via-ssh/page__p__910252#entry910252 ; Example .......: No ; =============================================================================================================================== Func _PlinkExit($iPID) $iClosed = ProcessClose($iPID) Return SetError(@error, 0, $iClosed) EndFunc