Jump to content

Suggestions on my script


DavidW
 Share

Recommended Posts

Hello everyone,

 

I've been experimenting with a script that has the following purpose: it checks all ip in a certain subnet  if its pingable it does nothing, if not pingable it will delete the lease in in the DHCP server. also it will deliver a time logging for when its getting deleted.

 

If there are any suggestions to my script in terms of performance or readability feel free to suggest.

 

;==========================================================================
;
;==========================================================================
Func test()

    ConsoleWrite(StringFormat("Date/Time = %s", _SqlDateTime()) & @CRLF)

EndFunc

Func _SqlDateTime($bMsecs = False)
    Local $sSqlDateTime = StringReplace( _NowCalc(), "/", "-" )
    If $bMsecs Then $sSqlDateTime &= ("." & @MSEC)
    Return $sSqlDateTime
 EndFunc


Local $i = 100
Do
    ;MsgBox($MB_SYSTEMMODAL, "", "The value of $i is: " & $i) ; Display the value of $i.

    Local $iPing = Ping("172.16.10."& $i, 250)

    If $iPing Then ; If a value greater than 0 was returned then display the following message.
        ;MsgBox($MB_SYSTEMMODAL, "", "The roundtrip-time took: " & $iPing & "ms.")
        ConsoleWrite ( $iPing + $i & " ping" & @CRLF)
        FileWriteLine ( @scriptdir &"\yes ping.txt" , "172.16.10." & $i)
    Else
        ;MsgBox($MB_SYSTEMMODAL, "", "An error occurred with @error value of: " & @error)
        ConsoleWrite ( $iPing + $i & " no ping" & " deleted " & _SqlDateTime() & @CRLF)
        FileWriteLine ( @scriptdir &"\noping5.txt" , "172.16.10." & $i)
        RunWait(@ComSpec & " /c " & "netsh dhcp server \\dc01 scope 172.16.0.0 delete lease 172.16.10." & $i, "", @SW_HIDE)

    EndIf


    $i = $i + 1 ; Or $i += 1 can be used as well.
Until $i = 255 ; Increase the value of $i until it equals the value of 10.

 

 

 

Edited by DavidW
Link to comment
Share on other sites

A very simple example:

 

#include <Date.au3>

test()


;==========================================================================
;
;==========================================================================
Func test()

    ConsoleWrite(StringFormat("Date/Time = %s", _SqlDateTime()) & @CRLF)
    ConsoleWrite(StringFormat("Date/Time = %s", _SqlDateTime(1)) & @CRLF)

EndFunc

Func _SqlDateTime($bMsecs = False)
    Local $sSqlDateTime = StringReplace( _NowCalc(), "/", "-" )
    If $bMsecs Then $sSqlDateTime &= ("." & @MSEC)
    Return $sSqlDateTime
EndFunc

 

Link to comment
Share on other sites

3 minutes ago, TheXman said:

A very simple example:

 

#include <Date.au3>

test()


;==========================================================================
;
;==========================================================================
Func test()

    ConsoleWrite(StringFormat("Date/Time = %s", _SqlDateTime()) & @CRLF)
    ConsoleWrite(StringFormat("Date/Time = %s", _SqlDateTime(1)) & @CRLF)

EndFunc

Func _SqlDateTime($bMsecs = False)
    Local $sSqlDateTime = StringReplace( _NowCalc(), "/", "-" )
    If $bMsecs Then $sSqlDateTime &= ("." & @MSEC)
    Return $sSqlDateTime
EndFunc

 

Thank you for the example, i guess i forgot my main question in the OP. 

Is it possible that i use the the DATE function combined with the following code

ConsoleWrite ( $iPing + $i & " no ping" & @CRLF)
        FileWriteLine ( @scriptdir &"\noping3.txt" , "172.16.10." & $i)

So that i get a return of everything  in 1 sentence? (172.16.10.105 not pinged, delete dlease at time XXX)

Or do i have two create 2 individual sentences

 

Edited by DavidW
Link to comment
Share on other sites

I don't quite understand your question, sorry. :(

Link to comment
Share on other sites

If you just want the datetime string, you simply call the function where you want the value.

 

ConsoleWrite ( $iPing + $i & " no ping" & " deleted " & _SqlDateTime() & @CRLF)

 

Edited by TheXman
Link to comment
Share on other sites

#cs ----------------------------------------------------------------------------


 Script Function:
    Cleanup DHCP Leases.

    http://www.techotopia.com/index.php/Managing_a_Windows_Server_2008_DHCP_Server_from_the_Command_Line

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


#include <MsgBoxConstants.au3>
#include <date.au3>


#include <Date.au3>

test()


Func Test()
    Local $i = 100
    Do
        ;MsgBox($MB_SYSTEMMODAL, "", "The value of $i is: " & $i) ; Display the value of $i.

        Local $iPing = Ping("172.16.10."& $i, 250)

        If $iPing Then ; If a value greater than 0 was returned then display the following message.
            ;MsgBox($MB_SYSTEMMODAL, "", "The roundtrip-time took: " & $iPing & "ms.")
            ConsoleWrite ( $iPing + $i & " ping" & @CRLF)
            FileWriteLine ( @scriptdir &"\yes ping.txt" , "172.16.10." & $i)
        Else
            ;MsgBox($MB_SYSTEMMODAL, "", "An error occurred with @error value of: " & @error)
            ConsoleWrite ( $iPing + $i & " no ping" & " deleted " & _SqlDateTime() & @CRLF)
            FileWriteLine ( @scriptdir &"\noping3.txt" , "172.16.10." & $i)
            RunWait(@ComSpec & " /c " & "netsh dhcp server \\dc01 scope 172.16.0.0 delete lease 172.16.10." & $i, "", @SW_HIDE)

        EndIf


        $i = $i + 1 ; Or $i += 1 can be used as well.
    Until $i = 255 ; Increase the value of $i until it equals the value of 10.
EndFunc


Func _SqlDateTime($bMsecs = False)
    Local $sSqlDateTime = StringReplace( _NowCalc(), "/", "-" )
    If $bMsecs Then $sSqlDateTime &= ("." & @MSEC)
    Return $sSqlDateTime
EndFunc

 

Link to comment
Share on other sites

For perfomance you may use single RunWait()

;==========================================================================
;
;==========================================================================

#include <File.au3>

Func test()

    ConsoleWrite(StringFormat("Date/Time = %s", _SqlDateTime()) & @CRLF)

EndFunc

Func _SqlDateTime($bMsecs = False)
    Local $sSqlDateTime = StringReplace( _NowCalc(), "/", "-" )
    If $bMsecs Then $sSqlDateTime &= ("." & @MSEC)
    Return $sSqlDateTime
EndFunc

Local $i = 100
Local $sCmd = "@echo off" & @CRLF

Do
    ;MsgBox($MB_SYSTEMMODAL, "", "The value of $i is: " & $i) ; Display the value of $i.

    Local $iPing = Ping("172.16.10."& $i, 250)

    If $iPing Then ; If a value greater than 0 was returned then display the following message.
        ;MsgBox($MB_SYSTEMMODAL, "", "The roundtrip-time took: " & $iPing & "ms.")
        ConsoleWrite ( $iPing + $i & " ping" & @CRLF)
        FileWriteLine ( @scriptdir &"\yes ping.txt" , "172.16.10." & $i)
    Else
        ;MsgBox($MB_SYSTEMMODAL, "", "An error occurred with @error value of: " & @error)
        ConsoleWrite ( $iPing + $i & " no ping" & " deleted " & _SqlDateTime() & @CRLF)
        FileWriteLine ( @scriptdir &"\noping5.txt" , "172.16.10." & $i)
        $sCmd &= "netsh dhcp server \\dc01 scope 172.16.0.0 delete lease 172.16.10." & $i & @CRLF

    EndIf


    $i = $i + 1 ; Or $i += 1 can be used as well.
Until $i = 255 ; Increase the value of $i until it equals the value of 10.

Local $sFile = _TempFile( @TempDir, "_", ".cmd" )
FileWrite( $sFile, $sCmd )
RunWait(@ComSpec & " /c " & $sFile, "", @SW_HIDE)
FileDelete( $sFile )

 

Link to comment
Share on other sites

;.........

Local $sCmd = ""

;..........

        $sCmd &= "dhcp server \\dc01 scope 172.16.0.0 delete lease 172.16.10." & $i & @CRLF

;..........

Local $sFile = _TempFile( @TempDir, "_", ".txt" )
FileWrite( $sFile, $sCmd )
RunWait(@ComSpec & " /c netsh <" & $sFile, "", @SW_HIDE)
FileDelete( $sFile )

More speed up.

And more

;==========================================================================
;
;==========================================================================

#include <AutoItConstants.au3>

Func test()

    ConsoleWrite(StringFormat("Date/Time = %s", _SqlDateTime()) & @CRLF)

EndFunc

Func _SqlDateTime($bMsecs = False)
    Local $sSqlDateTime = StringReplace( _NowCalc(), "/", "-" )
    If $bMsecs Then $sSqlDateTime &= ("." & @MSEC)
    Return $sSqlDateTime
EndFunc

Local $i = 100
Local $hNetSh = Run( "netsh", "", @SW_HIDE, $STDIN_CHILD ) ; No wait, redirect input stream

Do
    ;MsgBox($MB_SYSTEMMODAL, "", "The value of $i is: " & $i) ; Display the value of $i.

    Local $iPing = Ping("172.16.10."& $i, 250)

    If $iPing Then ; If a value greater than 0 was returned then display the following message.
        ;MsgBox($MB_SYSTEMMODAL, "", "The roundtrip-time took: " & $iPing & "ms.")
        ConsoleWrite ( $iPing + $i & " ping" & @CRLF)
        FileWriteLine ( @scriptdir &"\yes ping.txt" , "172.16.10." & $i)
    Else
        ;MsgBox($MB_SYSTEMMODAL, "", "An error occurred with @error value of: " & @error)
        ConsoleWrite ( $iPing + $i & " no ping" & " deleted " & _SqlDateTime() & @CRLF)
        FileWriteLine ( @scriptdir &"\noping5.txt" , "172.16.10." & $i)
        StdInWrite( $hNetSh, "dhcp server \\dc01 scope 172.16.0.0 delete lease 172.16.10." & $i & @CR ) ; send command to netsh

    EndIf


    $i = $i + 1 ; Or $i += 1 can be used as well.
Until $i = 255 ; Increase the value of $i until it equals the value of 10.

StdInWrite( $hNetSh ) ; Without second parameter, close input streaam

 

Edited by Imp
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...