Sign in to follow this  
Followers 0
Chris86

Remote Shutdown

13 posts in this topic

Hi, How get a script to list all users on the network i am on? Im trying to make a script that allows me to remote shutdown my servers, i can do this by type this in Commandline: shutdown -m \\Computername

But how can i find all my server names? this is a shared network but i can't see the servers in share files, but i can shut them down with this shutdown -m \\Computername

how can i list all the servers?

I have searched around in the forums and i didn't find anything useful

Share this post


Link to post
Share on other sites



What is your DHCP server? Query it with a NETSH command if it's a Windows server to see all the IP leases. Just a thought. (This assumes you are using Windows DHCP and are admin to the box)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Look up help docs on StdoutRead(...) and StderrRead(...) in conjunction with:

$c="net view"

Local $p=Run(@ComSpec&" /c "&$c,@SystemDir,@SW_HIDE,$STDERR_CHILD+$STDOUT_CHILD)

Then parse the returned string set in $p. That's it. Will list everyone within your domain, you parse it to... an array for example, then output into your GUI edit box control or w/e.

Additionally, if you only want the list to be comprised of those whom are actually running your "server", I would recommend that you first grab all of these node names using the above method, and then run a TCP connection to each node w/ a timeout in the script("after x many iterations w/ sleep(500) set, break loop"). If the connection is successfull (as returned by TCPConnect), then you can flag that node as being one whom is running your server. Yet again, do this in array for ease of use... a 2 dimensional array. One element for a string parsed from above method, and another for it's true/false flag of whether or not it's running a server.

And if you want to speed things up, you can throw a Ping(...) command in there to preface each nodes TCPConnect(...) test. That way if the node shutdown before the TCPConnect was attempted, you can automatically assume from Ping's result that you won't need to TCPConnect with a timeout, flag it false, and continue to next array index.

Then you'll have a composite list of all servers on your network(whom are running your server), displayable through a GUI or even a File.

Edited by Juppie7

Share this post


Link to post
Share on other sites

Chris86

Try this:

Global Const $SV_TYPE_WORKSTATION = 0x1

$aServers = _NetServerEnum($SV_TYPE_WORKSTATION)

If $aServers[0] = 0 Then Exit

For $i = 1 To $aServers[0]
    RunWait(@ComSpec & " /c shutdown -s -t 3 -m " & $aServers[$i], @SystemDir, @SW_HIDE)
Next

Func _NetServerEnum($iSrvType = -1, $sDomain = '')
    Local $uBufPtr = DllStructCreate("ptr;int;int"), $res[1]=[0], $i
    Local $uRecord = DllStructCreate("dword;ptr"), $iRecLen = DllStructGetSize($uRecord)
    Local $uString = DllStructCreate("char[16]")
    Local $uDomain = DllStructCreate("byte[32]"), $pDomain = 0
    If Not ($sDomain='' Or $sDomain='*') Then
        DllStructSetData($uDomain, 1, StringToBinary($sDomain,2))
        $pDomain = DllStructGetPtr($uDomain)
    EndIf
    Local $ret = DllCall ("netapi32.dll", "int", "NetServerEnum", _
        "ptr", 0, "int", 100, _
        "ptr", DllStructGetPtr($uBufPtr,1), "int", -1, _
        "ptr", DllStructGetPtr($uBufPtr,2), _
        "ptr", DllStructGetPtr($uBufPtr,3), _
        "int", $iSrvType, "ptr", $pDomain, "int", 0 )
    If $ret[0] Then Return SetError(1, $ret[0], '')
    Local $res[DllStructGetData($uBufPtr,3)+1]=[DllStructGetData($uBufPtr,3)]
    For $i=1 To DllStructGetData($uBufPtr,3)
        Local $uRecord = DllStructCreate("dword;ptr", DllStructGetData($uBufPtr,1)+($i-1)*$iRecLen)
        Local $sNBName = DllStructCreate("byte[32]", DllStructGetData($uRecord,2))
        DllStructSetData($uString,1,BinaryToString(DllStructGetData($sNBName,1),2))
        $res[$i] = DllStructGetData($uString,1)
    Next
    $ret = DllCall ("netapi32.dll", "int", "NetApiBufferFree", "ptr", DllStructGetData($uBufPtr,1))
    Return $res
EndFunc

Share this post


Link to post
Share on other sites

Chris86

Try this:

Global Const $SV_TYPE_WORKSTATION = 0x1

$aServers = _NetServerEnum($SV_TYPE_WORKSTATION)

If $aServers[0] = 0 Then Exit

For $i = 1 To $aServers[0]
    RunWait(@ComSpec & " /c shutdown -s -t 3 -m " & $aServers[$i], @SystemDir, @SW_HIDE)
Next

Func _NetServerEnum($iSrvType = -1, $sDomain = '')
    Local $uBufPtr = DllStructCreate("ptr;int;int"), $res[1]=[0], $i
    Local $uRecord = DllStructCreate("dword;ptr"), $iRecLen = DllStructGetSize($uRecord)
    Local $uString = DllStructCreate("char[16]")
    Local $uDomain = DllStructCreate("byte[32]"), $pDomain = 0
    If Not ($sDomain='' Or $sDomain='*') Then
        DllStructSetData($uDomain, 1, StringToBinary($sDomain,2))
        $pDomain = DllStructGetPtr($uDomain)
    EndIf
    Local $ret = DllCall ("netapi32.dll", "int", "NetServerEnum", _
        "ptr", 0, "int", 100, _
        "ptr", DllStructGetPtr($uBufPtr,1), "int", -1, _
        "ptr", DllStructGetPtr($uBufPtr,2), _
        "ptr", DllStructGetPtr($uBufPtr,3), _
        "int", $iSrvType, "ptr", $pDomain, "int", 0 )
    If $ret[0] Then Return SetError(1, $ret[0], '')
    Local $res[DllStructGetData($uBufPtr,3)+1]=[DllStructGetData($uBufPtr,3)]
    For $i=1 To DllStructGetData($uBufPtr,3)
        Local $uRecord = DllStructCreate("dword;ptr", DllStructGetData($uBufPtr,1)+($i-1)*$iRecLen)
        Local $sNBName = DllStructCreate("byte[32]", DllStructGetData($uRecord,2))
        DllStructSetData($uString,1,BinaryToString(DllStructGetData($sNBName,1),2))
        $res[$i] = DllStructGetData($uString,1)
    Next
    $ret = DllCall ("netapi32.dll", "int", "NetApiBufferFree", "ptr", DllStructGetData($uBufPtr,1))
    Return $res
EndFuncoÝ÷ Ûú®¢×Ǻܨ»eÈn·*.®Ç¥}Ú0}ÀÒ-)äʧyçt÷m=Ù·¢·­éÚ殶­sdvÆö&Â6öç7Bb33cµ5eõEUõtõ$µ5DDôâÒ ¢b33c¶6W'fW'2ÒôæWE6W'fW$VçVÒb33cµ5eõEUõtõ$µ5DDôâ ¤bb33c¶6W'fW'5³ÒÒFVâW@ ¤f÷"b33c¶ÒFòb33c¶6W'fW'5³Ð bb33c¶6W'fW'5²b33c¶ÒfÇC²fwC²6ö×WFW&æÖRFVâ'VåvB6öÕ7V2fײgV÷C²ö26WFF÷vâ×2×B2ÖÒb3#²b3#²gV÷C²fײb33c¶6W'fW'5²b33c¶ÒÂ77FVÔF"Â5uôDR¤æW@ ¤gVæ2ôæWE6W'fW$VçVÒb33c¶7'eGRÒÓÂb33c·4FöÖâÒb33²b33²¢Æö6Âb33c·T'VeG"ÒFÆÅ7G'V7D7&VFRgV÷C·G#¶çC¶çBgV÷C²Âb33c·&W5³ÓÕ³ÒÂb33c¶¢Æö6Âb33c·U&V6÷&BÒFÆÅ7G'V7D7&VFRgV÷C¶Gv÷&C·G"gV÷C²Âb33c¶&V4ÆVâÒFÆÅ7G'V7DvWE6¦Rb33c·U&V6÷&B¢Æö6Âb33c·U7G&ærÒFÆÅ7G'V7D7&VFRgV÷C¶6%³eÒgV÷C²¢Æö6Âb33c·TFöÖâÒFÆÅ7G'V7D7&VFRgV÷C¶'FU³3%ÒgV÷C²Âb33c·FöÖâÒ¢bæ÷Bb33c·4FöÖãÒb33²b33²÷"b33c·4FöÖãÒb33²¢b33²FVà¢FÆÅ7G'V7E6WDFFb33c·TFöÖâÂÂ7G&æuFô&æ'b33c·4FöÖâÃ"¢b33c·FöÖâÒFÆÅ7G'V7DvWEG"b33c·TFöÖâ¢VæD`¢Æö6Âb33c·&WBÒFÆÄ6ÆÂgV÷C¶æWF3"æFÆÂgV÷C²ÂgV÷C¶çBgV÷C²ÂgV÷C´æWE6W'fW$VçVÒgV÷C²Âð¢gV÷C·G"gV÷C²ÂÂgV÷C¶çBgV÷C²ÂÂð¢gV÷C·G"gV÷C²ÂFÆÅ7G'V7DvWEG"b33c·T'VeG"ÃÂgV÷C¶çBgV÷C²ÂÓÂð¢gV÷C·G"gV÷C²ÂFÆÅ7G'V7DvWEG"b33c·T'VeG"Ã"Âð¢gV÷C·G"gV÷C²ÂFÆÅ7G'V7DvWEG"b33c·T'VeG"Ã2Âð¢gV÷C¶çBgV÷C²Âb33c¶7'eGRÂgV÷C·G"gV÷C²Âb33c·FöÖâÂgV÷C¶çBgV÷C²Â¢bb33c·&WE³ÒFVâ&WGW&â6WDW'&÷"Âb33c·&WE³ÒÂb33²b33²¢Æö6Âb33c·&W5´FÆÅ7G'V7DvWDFFb33c·T'VeG"Ã2³ÓÕ´FÆÅ7G'V7DvWDFFb33c·T'VeG"Ã2Тf÷"b33c¶ÓFòFÆÅ7G'V7DvWDFFb33c·T'VeG"Ã2¢Æö6Âb33c·U&V6÷&BÒFÆÅ7G'V7D7&VFRgV÷C¶Gv÷&C·G"gV÷C²ÂFÆÅ7G'V7DvWDFFb33c·T'VeG"òb33c¶Ó¢b33c¶&V4ÆVâ¢Æö6Âb33c·4ä$æÖRÒFÆÅ7G'V7D7&VFRgV÷C¶'FU³3%ÒgV÷C²ÂFÆÅ7G'V7DvWDFFb33c·U&V6÷&BÃ"¢FÆÅ7G'V7E6WDFFb33c·U7G&ærÃÄ&æ'Fõ7G&ærFÆÅ7G'V7DvWDFFb33c·4ä$æÖRÃÃ"¢b33c·&W5²b33c¶ÒÒFÆÅ7G'V7DvWDFFb33c·U7G&ærâæW@¢b33c·&WBÒFÆÄ6ÆÂgV÷C¶æWF3"æFÆÂgV÷C²ÂgV÷C¶çBgV÷C²ÂgV÷C´æWD'VffW$g&VRgV÷C²ÂgV÷C·G"gV÷C²ÂFÆÅ7G'V7DvWDFFb33c·T'VeG"â&WGW&âb33c·&W0¤VæDgVæ0

Share this post


Link to post
Share on other sites

ChrisL

but you'll shut yourself down! And I think you need \\ before the name

:P Yes, you are right! Thanks for corrections :P

Share this post


Link to post
Share on other sites

What is your DHCP server? Query it with a NETSH command if it's a Windows server to see all the IP leases. Just a thought. (This assumes you are using Windows DHCP and are admin to the box)

Im not sure :S this is s project on school, all the servers are normal computers running ftp server, and all student computers and teacher computers are connected somehow, so i can turn them off using the shutdown -m \\Computername in cmd to shut them off

Share this post


Link to post
Share on other sites

Im not sure :S this is s project on school, all the servers are normal computers running ftp server, and all student computers and teacher computers are connected somehow, so i can turn them off using the shutdown -m \\Computername in cmd to shut them off

So you are trying to shutdown any computer you want to in school

(Sounds fishy)


READ BEFORE POSTINGMy UDF'sMessagesYou say plz because its shorter than please. I say no because its shorter than yes.Join My Website

Share this post


Link to post
Share on other sites

Im not sure :S this is s project on school, all the servers are normal computers running ftp server, and all student computers and teacher computers are connected somehow, so i can turn them off using the shutdown -m \\Computername in cmd to shut them off

You might have trouble doing that... My school blocked it somehow.... :P

Share this post


Link to post
Share on other sites

Well, our school hasn't :P i have tested it

Share this post


Link to post
Share on other sites

So you are trying to shutdown any computer you want to in school

(Sounds fishy)

No, i just don't want to learn other people at school how to shutdown computers by using commandline, my script will also have a filter, all students pcs names has the two characters of the student name in it, like me, Christian Cock, would be Chco-[Computernumber](those would be filtered) and the servers would just have server1, server2.

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