Jump to content

Script defying logic


Panacea
 Share

Recommended Posts

Hello,
 
I am relatively new to scripting, and i was 
 
a) Tying to open Command prompt to ping to a group of IP address
B) The IP addresses are in Progression
 
e.g :- They are 192.168.0.1-192.168.0.15 
 
Problem 1 :-
 
I was trying to use the use Recursive functions and found out that only this method worked.

Pinglu(1)
Func Pinglu($ip)
   Send("#r")
   Sleep(1000); Used this function because the computer was not fast enough to execute all the list of command is succession, even 100ms proved
   Send("cmd.exe {Enter}");futile
   Sleep(500)
   Send("ping 10.1.9.")
   Sleep(1000)
   Send($ip)
    Sleep(1000)
   Send(" -t  {Enter} ")
   WinWaitActive(""); Used this same as the sleep function but not really neccessary
   #Sleep(1000)
   $ip += 1
   If $ip < 15  Then Return
   Pinglu($ip)
   Sleep(1000)

EndFunc

I know my code is not really good Any suggustion to improve. I thought. OMG, that is an ugly piece of code. 

Problem 2 :-

I executed the code all fine. And this morning i rewrote the code again, and found that when i say 

If $ip < 15  Then Return
   Pinglu($ip)
   Sleep(1000)

EndFunc

It is executing only one recursive iteration. I was debugging a bit and said let me change the booelian expression direction.

so i did this 

If $ip > 15  Then Return
   Pinglu($ip)
   Sleep(1000)

EndFunc

and bang it worked much to my dismay and suprise , means i do not not how the variable are funtions are returned. Does anyone know why this can happen please.

Thanks

Bharat C P

I really want to improve my coding or scripting, if anyone can help me i will be glad. 

Link to comment
Share on other sites

Have you tried using the Ping command from AutoIt?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Mr. Brew Man,

Thank you for that let me try to use that, may be i was trying to overcomplicate things, i will get and try with this.

BTW.

Is there any book on auto it or scripting in general that you can recommend.

Thanks

Bharat C P 

Link to comment
Share on other sites

I'd recommend the help file, and the forum here. Also look at the Wiki link at the top of the page, lots of good information in there.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

$ip = "192.168.1."

For $i = 1 To 15
    ConsoleWrite("$ip = " & $ip & $i & @CRLF)
    $iPing = Ping($ip & $i, 3000)
    ConsoleWrite("$iPing = " & $iPing & @CRLF)
    Next
:P

Edit: fixed

Edited by l3ill
Link to comment
Share on other sites

Your ping command also needs the $i

$iPing = ($ip & $i, 3000)

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Hi Panacea

If this is only needed to ping a range of ip addresses then you may '?do=embed' frameborder='0' data-embedContent>?do=embed' frameborder='0' data-embedContent>>take a look here.
in this example there is a way (one of many)  to use that udf to continuously ping a range of IP addresses and report the result in a ListView.

You need first to download that udf and save in the same directory of your script (calling it MultiPing.au3)  from this link:

'?do=embed' frameborder='0' data-embedContent>?do=embed' frameborder='0' data-embedContent>>

bye

; this is to ping continuously a list of IP addresses, get and display ping result "live"
; it simulates the dos "ping -t" command but performed simultaneously on many IP
; presenting the results in a ListView highlighting not responding devices with a red box

#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include 'MultiPing.au3' ; <-- take this from the following link:
; http://www.autoitscript.com/forum/topic/156395-versatile-multi-ping

Local $IP_range = "192.168.1.1-15" ; range to be pinged

Opt("GUIOnEventMode", 1)
HotKeySet("{esc}", "_button1")

Local $Win_X = 250, $Win_Y = 500 ; dimension of window
$PingGui = GUICreate("IP addresses monitor", $Win_X, $Win_Y, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "_button1", $PingGui)
$listview = GUICtrlCreateListView("", 10, 10, $Win_X - 20, $Win_Y - 40)
_GUICtrlListView_AddColumn($listview, "IP Address", 120)
_GUICtrlListView_AddColumn($listview, "Roundtrip (ms)", 100)
_GUICtrlListView_SetExtendedListViewStyle($listview, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
; Generate colored square images
$hImage = _GUIImageList_Create()
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0x00FF00, 16, 16)) ; green
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0xFF0000, 16, 16)) ; red
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($listview, 0xFFFF00, 16, 16)) ; yellow
_GUICtrlListView_SetImageList($listview, $hImage, 1)

$button1 = GUICtrlCreateButton("Exit", 10, $Win_Y - 25, $Win_X - 20, 20)
GUICtrlSetTip(-1, "End of program")
GUICtrlSetOnEvent(-1, "_button1")
GUISetState(@SW_SHOW)

$MyArray = _nPing($IP_range, 0, 0) ; first call is to generate the array
_ArrayDelete($MyArray, 0) ; remove first item
_GUICtrlListView_AddArray($listview, $MyArray) ; and fill ListView

While 1 ; continuously ping addresses
    Sleep(10)
    _nPing($MyArray, 0, 0, 0, "_refresh") ; PING required addresses and call the _refresh() function
    ;                                       for each terminated ping (reasults of ping are passed to function)
WEnd

Func _button1() ; Button 1 clicked
    Exit
EndFunc   ;==>_button1

Func _refresh($Params) ; this receive ping results and displays them in the ListView
    _GUICtrlListView_SetItemImage($listview, $Params[5], 2) ; set colour to Yellow
    _GUICtrlListView_SetItemText($listview, $Params[5], "", 1); clear the roundtrip column
    Sleep(50) ; a little wait
    If $Params[4] = -1 Then ; Device not responding to ping
        ;   _GUICtrlListView_SetItemText($listview, $Params[5], "", 1); clear the roundtrip column
        _GUICtrlListView_SetItemImage($listview, $Params[5], 1) ; set colour to RED
        _GUICtrlListView_EnsureVisible($listview, $Params[5]) ; Position view to this item
    Else ; Device responds to ping
        _GUICtrlListView_SetItemText($listview, $Params[5], $Params[4], 1) ; show roundtrip
        _GUICtrlListView_SetItemImage($listview, $Params[5], 0) ; set colour to GREEN
    EndIf
EndFunc   ;==>_refresh

edit:listing

changed from:    _nPing($IP_range, 0, 0, 0, "_refresh")

to:                       _nPing($MyArray, 0, 0, 0, "_refresh")

result is the same in this case but in this way is passed the already generated array instead of the range so the array is not generated again in the function

Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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...