DavidW Posted February 7, 2018 Share Posted February 7, 2018 (edited) 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. expandcollapse popup;========================================================================== ; ;========================================================================== 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 February 7, 2018 by DavidW Link to comment Share on other sites More sharing options...
TheXman Posted February 7, 2018 Share Posted February 7, 2018 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 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
DavidW Posted February 7, 2018 Author Share Posted February 7, 2018 (edited) 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 February 7, 2018 by DavidW Link to comment Share on other sites More sharing options...
TheXman Posted February 7, 2018 Share Posted February 7, 2018 I don't quite understand your question, sorry. CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
DavidW Posted February 7, 2018 Author Share Posted February 7, 2018 (edited) Ah well thats fine i just experiment more. Thanks anyways! Edited February 7, 2018 by DavidW Link to comment Share on other sites More sharing options...
TheXman Posted February 7, 2018 Share Posted February 7, 2018 (edited) 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 February 7, 2018 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
TheXman Posted February 7, 2018 Share Posted February 7, 2018 expandcollapse popup#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 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
Imp Posted February 7, 2018 Share Posted February 7, 2018 For perfomance you may use single RunWait() expandcollapse popup;========================================================================== ; ;========================================================================== #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 More sharing options...
Imp Posted February 7, 2018 Share Posted February 7, 2018 (edited) ;......... 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 expandcollapse popup;========================================================================== ; ;========================================================================== #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 February 7, 2018 by Imp Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now