Author Topic: I want a simple Ping status up or down program and here is what I came up with.  (Read 3659 times)

0 Members and 1 Guest are viewing this topic.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
I do not get why there is not a _icmp command in qb64 that would give results to a pinged IP address internally.

So what I have done is shelled to dos and run the ping ip -n 1 and captured it to a file then read that file looking or the results sent = a pass or fail number.
I also create a external config file to get the ip of each machine I a testing for up or down.

So I open he config get 1.1.1.1 and then left until I get the the end of the value and pass that to the command to ping with find the results of sent and received as equalling each other then print the entire line in green it if failed the sent and lost would equal and print in red. Ok good then I return to the topish to get the next ip in the config file and do that again.

That is where the issue appears the code wont do the same thing and will sometimes show failed ips on ips that are up.  So not sure about the type of loop I should have used I ended up with gotos as a last resort which got the program to the point it is now.

The next thing I wanted to do was make it run faster and add a second column for the ips when there are more then X number in the first one but that is when I caught the issue and focused on it instead of the other column.

I added some areas to try to see what is going on and the first run you see the values but the next loop is where things go wonky.
Code: QB64: [Select]
  1.  
  2. COLOR 3, 1: CLS
  3. file1 = FREEFILE
  4. Config$ = "Config.ini"
  5. file2 = FREEFILE + 1
  6. Pingfile$ = "ip1.tmp"
  7. IF _FILEEXISTS(Pingfile$) THEN CLOSE: KILL Pingfile$
  8. 1
  9. locateline = 5
  10. LOCATE 2, 33: PRINT "Network up/down"
  11.     LOCATE 3, 24: PRINT "Settings file (" + Config$ + ") found.": GOTO 2
  12. 'No Config file create file for user with sample data
  13. PRINT "File missing, Creating " + Config$ + ""
  14. PRINT "Eg. 192.168.1.1 Router"
  15. PRINT "Eg. 192.168.1.2 File Server"
  16. PRINT "and so on"
  17. OPEN Config$ FOR OUTPUT AS #file1
  18. WRITE #file1, "192.168.1.1 Router"
  19. WRITE #file1, "192.168.1.254 Wifi Access Point"
  20. CLOSE #file1
  21. PRINT "File created with data."
  22. 'open Config file to get ips to check
  23. 2
  24. OPEN Config$ FOR INPUT AS #file1
  25. 'find ip in config file to send to ping command
  26. 3
  27. 'S and T just to trouble shoot and see what is going on
  28. T = 5
  29. S = 12
  30. locateline = locateline + 1
  31. IF EOF(file1) THEN GOTO 99
  32. LINE INPUT #file1, IPvalue$
  33. 'locate IPLength part in config file
  34. LOCATE S, T: PRINT "*" + IPvalue$ + "* line value of config file"
  35. DO UNTIL a$ = " "
  36.     IPLength = IPLength + 1
  37.     a$ = MID$(IPvalue$, IPLength, 1)
  38.     IF MID$(a$, IPLength, 1) = " " THEN EXIT DO
  39. 'Dos Ping
  40. 22
  41. IPCount = IPCount + 1
  42. '*********problem area*********
  43. LOCATE S + 1, T: PRINT "*"; IPLength - 1; "* length of ip -1"
  44. LOCATE S + 2, T: PRINT "*" + LEFT$(IPvalue$, IPLength - 1) + "* value determined from config file"
  45.  
  46. Comand$ = "cmd /c ping " + LEFT$(IPvalue$, IPLength - 1) + " -n 1 > " + Pingfile$
  47. LOCATE S + 3, T: PRINT "*" + Comand$ + "* command to send shell"
  48.  
  49. SHELL _HIDE Comand$
  50. 'Get Ping status
  51. OPEN Pingfile$ FOR INPUT AS #file2
  52. DO WHILE NOT EOF(file2)
  53.     LINE INPUT #file2, Status$
  54.     IF LEFT$(Status$, 32) = "Ping request could not find host" THEN Status$ = "    Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),": EXIT DO
  55.     IF LEFT$(Status$, 12) = "    Packets:" THEN EXIT DO
  56. IF _FILEEXISTS(Pingfile$) THEN CLOSE file2: KILL Pingfile$
  57. 'Set Up Down Statues
  58. Sent = VAL(MID$(Status$, 21, 1))
  59. Receive = VAL(MID$(Status$, 35, 1))
  60. Lost = VAL(MID$(Status$, 45, 1))
  61. 'testing print of values
  62. LOCATE S + 4: PRINT "*", Sent, Receive, Lost, "*", "             Sent         Recived         Lost  Values from shell"
  63. Colum1 = 5
  64. IF Sent = Receive THEN LOCATE locateline, Colum1: COLOR 10, 7: PRINT "UP" + " " + IPvalue$
  65. IF Sent = Lost THEN LOCATE locateline, Colum1: COLOR 4, 7: PRINT "DN" + " " + IPvalue$:
  66. COLOR 3, 1
  67. 'Return to ping start for next value in config
  68. INPUT "press enter for next loop", A
  69. ' close and startover from the top
  70. 99
  71.  
  72.  

A config.ini file would be a txt tile that looks like
1.1.1.1 DNS Server
192.168.1.1 house router
8.8.8.8 DNS Server
google.com google website this would be the next part for me to work on is host names instead of absolute ips.
The program should read over until it finds the space at the end of the ip the rest is the description of what that ip represents.

Marked as best answer by Chris80194 on January 02, 2019, 09:13:04 am

FellippeHeitor

  • Guest
One of your issues is using WRITE to write to the config file. WRITE adds quotation marks around the text you save and when you're reading the lines back, all of them start with CHR$(34), and you're also passing that to the PING command when you SHELL.

Try using PRINT #file1, "192.168.1.1 Router", etc. That won't add the quotation marks and reading back will work as expected.

If you intended to keep using WRITE, then instead of LINE INPUT, just use INPUT to read the values back from the disk, as INPUT will ignore the CHR$(34) character.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
I actually created the config using notepad......
My config has a lot more then the example file that is created so it was easier that way but thanks for catching that.

Something weird is the first pass through the do loop to get the packets line in the ping results
I can get the ip but each pass after it hold the value in IPlength at 16 if I clear that it messes up and is now 0 ummmm let me make sure I put the clear in the right place above the counting of IPlength=IPlength+1. that would solve that mystery maybe.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
I think it is literally one line

FellippeHeitor

  • Guest
I don't have the config file and your program is creating is using WRITE. Now go back to my first reply and you'll see what I'm talking about.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
UPDATED
I fixed a few things and cleared out some unneeded things still having issues at do loop line 35

Code: QB64: [Select]
  1. COLOR 3, 1: CLS
  2. file1 = FREEFILE
  3. Config$ = "Config.ini"
  4. file2 = FREEFILE + 1
  5. Pingfile$ = "ip1.tmp"
  6. Colum1 = 5
  7. IF _FILEEXISTS(Pingfile$) THEN CLOSE: KILL Pingfile$
  8. 1
  9. locateline = 5
  10. LOCATE 2, 33: PRINT "Network up/down"
  11.     LOCATE 3, 24: PRINT "Settings file (" + Config$ + ") found.": GOTO 2
  12. 'No Config file create file for user with sample data
  13. PRINT "File missing, Creating " + Config$ + ""
  14. PRINT "Eg. 192.168.1.1 Router"
  15. PRINT "Eg. 192.168.1.2 File Server"
  16. PRINT "and so on"
  17. OPEN Config$ FOR OUTPUT AS #file1
  18. PRINT #file1, "192.168.1.1 Router"
  19. PRINT #file1, "1.1.1.1 DNS Server"
  20. PRINT #file1, "192.168.1.254 Wifi Access Point"
  21. CLOSE #file1
  22. PRINT "File created with data."
  23. 'open Config file to get ips to check
  24. 2
  25. OPEN Config$ FOR INPUT AS #file1
  26. 'find ip in config file to send to ping command
  27. 3
  28. locateline = locateline + 1
  29. IF EOF(file1) THEN GOTO 99
  30. LINE INPUT #file1, IPvalue$
  31. 'locate IPLength part in config file
  32. A$ = "*": IPLength = 0
  33. DO UNTIL A$ = " "
  34.     IPLength = IPLength + 1
  35.     A$ = MID$(IPvalue$, IPLength, 1)
  36.     IF MID$(A$, IPLength, 1) = " " THEN IPLength = IPLength - 1: EXIT DO
  37. 'Dos Ping
  38. 22
  39. IPCount = IPCount + 1
  40. Comand$ = "cmd /c ping " + LEFT$(IPvalue$, IPLength) + " -n 1 > " + Pingfile$
  41. SHELL _HIDE Comand$
  42. 'Get Ping status
  43. OPEN Pingfile$ FOR INPUT AS #file2
  44. DO WHILE NOT EOF(file2)
  45.     LINE INPUT #file2, Status$
  46.     IF LEFT$(Status$, 32) = "Ping request could not find host" THEN Status$ = "    Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),": EXIT DO
  47.     IF LEFT$(Status$, 12) = "    Packets:" THEN EXIT DO
  48. IF _FILEEXISTS(Pingfile$) THEN CLOSE file2: KILL Pingfile$
  49. 'Set Up Down Statues
  50. Sent = VAL(MID$(Status$, 21, 1))
  51. Receive = VAL(MID$(Status$, 35, 1))
  52. Lost = VAL(MID$(Status$, 45, 1))
  53. 'testing print of values
  54. IF Sent = Receive THEN LOCATE locateline, Colum1: COLOR 10, 7: PRINT "UP" + " " + IPvalue$
  55. IF Sent = Lost THEN LOCATE locateline, Colum1: COLOR 4, 7: PRINT "DN" + " " + IPvalue$:
  56. COLOR 3, 1
  57. 'Return to ping start for next value in config file
  58. ' close files and startover from the top
  59. 99
  60.  
« Last Edit: December 31, 2018, 06:22:49 pm by Chris80194 »

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Thanks to FellippeHeitor this project has become much more streamlined and is a shell of its original self.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
So I got my simple up down monitor thanks to FellippeHeitor for the code snip.
I dressed it up, gave it a splash of color and put in some bits to make it more universal with the code bits he shared and adding things I wanted to beabe to do but could not find a software that did what I wanted, free options for private use are limited Pinger is basic with a bit of logging.  You wont be watching it so nice to know when you had a problem.

Code: QB64: [Select]
  1. SCREEN 0: WIDTH 80, 50
  2. ConfigFile$ = "config.ini"
  3. IF _FILEEXISTS(ConfigFile$) = 0 THEN
  4.     OPEN ConfigFile$ FOR OUTPUT AS #1
  5.     PRINT #1, "192.168.1.1 Router"
  6.     PRINT #1, "1.1.1.1 DNS Server"
  7.     PRINT #1, "192.168.1.254 Wifi Access Point"
  8.     PRINT #1, "qb64.com QB64.com (doesn't exist)"
  9.     PRINT #1, "MUST include Ip, hostname or domain name and a description"
  10.     PRINT #1, "Otherwise the program will not be able to ping propely."
  11.     CLOSE
  12. Readme$ = "read.me.txt"
  13. IF _FILEEXISTS(Readme$) = 0 THEN
  14.     OPEN Readme$ FOR OUTPUT AS #1
  15.     PRINT #1, "Written by Chris Owen Chris.Owen09@gmail.com"
  16.     PRINT #1, "Much thanks goes to FellippeHeitor for the starting code, after seeing how my first work was clumsy he pointed me to what this has become."
  17.     PRINT #1, "The Config.ini file shall contain all the ips you wish to monitor."
  18.     PRINT #1, "The Config.ini file is dynamic so changes can be made without stopping the program, the file is read line by line and not all loaded at one time."
  19.     PRINT #1, "This makes changing the file and reading the data in slower but you can monitor while changes are being done using a text editor."
  20.     PRINT #1, "The program can ping, IPs, domain names and host names, descriptions are necessary. I used ( cmd Ping 1.1.1.1 -n 1 ) for the results"
  21.     PRINT #1, "192.168.1.254 Wifi Access Point will be reported on the main screen as separate items using a UP or DN status."
  22.     PRINT #1, "Since the program has to shell out to run the Cmd PING speed is slow so I have added a -n 1 count instead of the usual 3 count."
  23.     PRINT #1, "This program will show 44 lines of ips, 45 and above will start a scroll effect and will pick up at the first ip in you list."
  24.     PRINT #1, "Downed IPs are logged to a file called ( down????.log ) where ???? is the date it was noticed."
  25.     PRINT #1, "In the log, simple details such as the date, time, and ip and if you put in a description are logged."
  26.     PRINT #1, "ESC exits the program and holding the ( a ) key acknowledges any visual down notification."
  27.     PRINT #1, "The program will self create missing files such as the Config.ini where the IPs are read from, and This read.me.txt file every run."
  28.     PRINT #1, "You MUST include Ip, hostname or domain name and a description.  Otherwise the program will not be able to ping properly."
  29.     CLOSE
  30.  
  31. COLOR 3, 1: CLS
  32. flag = 0: set = 0: AlertLocation = 68: AlertLocationHigh = 10: ipstartline = 4
  33.     DownLog$ = "Down" + DATE$ + ".LOG"
  34.     IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0
  35.     IF flag = 1 AND set = 0 THEN
  36.         set = 1
  37.         COLOR 20, 15
  38.         LOCATE AlertLocationHigh, AlertLocation: PRINT "****"
  39.         COLOR 4, 15
  40.         LOCATE AlertLocationHigh + 2, AlertLocation - 6: PRINT "Hold A to clear"
  41.         LOCATE AlertLocationHigh + 3, AlertLocation - 10: PRINT "Log=" + DownLog$
  42.     END IF
  43.  
  44.     IF flag = 0 AND set = 0 THEN
  45.         set = 2
  46.         COLOR 3, 1
  47.         LOCATE AlertLocationHigh, AlertLocation: PRINT "    "
  48.         LOCATE AlertLocationHigh + 2, AlertLocation - 6: PRINT "                "
  49.         LOCATE AlertLocationHigh + 3, AlertLocation - 10: PRINT "                      "
  50.     END IF
  51.  
  52.     IF INKEY$ = CHR$(27) THEN 999
  53.     IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0
  54.     OPEN ConfigFile$ FOR INPUT AS #1
  55.     OPEN DownLog$ FOR APPEND AS #2
  56.     COLOR 14, 1
  57.     Title1$ = "Network up/down" + "       Press ESC to exit"
  58.  
  59.     Title3$ = "Settings file (" + ConfigFile$ + ") found."
  60.     note$ = "File " + ConfigFile$ + " can be edited while the program runs. IPs scroll."
  61.     LOCATE 3, 41 - LEN(Title1$) / 2: PRINT Title1$, Title2$
  62.     IF _FILEEXISTS(ConfigFile$) THEN LOCATE 4, 41 - LEN(Title3$) / 2: PRINT Title3$
  63.     LOCATE 50, 41 - LEN(note$) / 2: PRINT note$;
  64.     DO
  65.         IF INKEY$ = CHR$(27) THEN 999
  66.         IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0
  67.         IF flag = 1 AND set = 0 THEN
  68.             set = 1
  69.             COLOR 20, 15
  70.             LOCATE AlertLocationHigh, AlertLocation: PRINT "****"
  71.             COLOR 4, 15
  72.             LOCATE AlertLocationHigh + 2, AlertLocation - 6: PRINT "Hold A to clear"
  73.         END IF
  74.  
  75.         IF flag = 0 AND set = 0 THEN
  76.             set = 2
  77.             COLOR 3, 1
  78.             LOCATE AlertLocationHigh, AlertLocation: PRINT "    "
  79.             LOCATE AlertLocationHigh + 2, AlertLocation - 6: PRINT "                "
  80.             LOCATE AlertLocationHigh + 3, AlertLocation - 10: PRINT "                      "
  81.         END IF
  82.  
  83.         IF EOF(1) THEN CLOSE: EXIT DO
  84.         LINE INPUT #1, ipLine$
  85.         IF INKEY$ = CHR$(27) THEN 999
  86.         IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0
  87.         ip$ = LEFT$(ipLine$, INSTR(ipLine$, " ") - 1)
  88.         desc$ = RIGHT$(ipLine$, LEN(ipLine$) - LEN(ip$) - 1)
  89.         COLOR 2, 1
  90.         LOCATE 2, 41 - LEN(DATE$ + " " + LEFT$(TIME$, 5)) / 2: PRINT DATE$ + " " + LEFT$(TIME$, 5)
  91.         linelocate = linelocate + 1: IF linelocate = 45 THEN linelocate = 1
  92.         lineclear$ = "": FOR lclear = 1 TO 50: lineclear$ = lineclear$ + " ": NEXT
  93.         LOCATE linelocate + ipstartline, 1: PRINT lineclear$
  94.         LOCATE linelocate + ipstartline, 1: PRINT linelocate; "Ping ("; ip$; ") ("; desc$; ")";
  95.         IF INKEY$ = CHR$(27) THEN 999
  96.         IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0
  97.         COLOR 7, 1: LOCATE , 52: PRINT "**";
  98.         result& = _SHELLHIDE("ping " + ip$ + " -n 1")
  99.         IF result& = 0 THEN
  100.             'success
  101.             COLOR 2, 15
  102.             LOCATE , 52: PRINT "UP"
  103.             COLOR 7, 1
  104.         ELSE
  105.             COLOR 4, 15
  106.             LOCATE , 52: PRINT "DN"
  107.             COLOR 7, 1
  108.             PRINT #2, DATE$ + " " + TIME$ + " " + "Ping (" + ip$ + ") (" + desc$ + ")" + "   Down": flag = 1: set = 0
  109.         END IF
  110.         IF INKEY$ = CHR$(27) THEN 999
  111.         IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0
  112.     LOOP
  113.     _LIMIT 1 'once per second
  114. 999
  115. PRINT "Shutting down"
  116.  

So how is my Frankenstein???
The words that come to my mind are "Its alive Its alive"
Considering where this started from I'm happy so far.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
So was doing some more tinkering
this runs fast on a system with no over head  Still have some INKEY issues particularly with A L and E functions.
Chr$27 works great.
I am happy with its process and until I can figure out or maybe get a few hints I think this is good right here.
Frankenstein was built with parts so was not a real polished man.
Code: QB64: [Select]
  1. SCREEN 0: WIDTH 80, 50
  2. DIM SHARED ConfigFile$, ReadMe$
  3. ConfigFile$ = "config.ini"
  4. ReadMe$ = "read.me.txt"
  5. IF _FILEEXISTS(ConfigFile$) = 0 THEN CALL example(ConfigFile%, ReadMe%)
  6. IF _FILEEXISTS(ReadMe$) = 0 THEN CALL example(ConfigFile%, ReadMe%)
  7. COLOR 3, 1: CLS
  8. flag = 0: set = 0: AlertLocation = 68: AlertLocationHigh = 10: ipstartline = 4
  9.     DownLog$ = "Down" + DATE$ + ".LOG"
  10.     IF UCASE$(INKEY$) = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
  11.     IF UCASE$(INKEY$) = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
  12.     IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0
  13.     IF flag = 1 AND set = 0 THEN
  14.         set = 1
  15.         COLOR 20, 15
  16.         LOCATE AlertLocationHigh, AlertLocation: PRINT "****"
  17.         COLOR 4, 15
  18.         LOCATE AlertLocationHigh + 2, AlertLocation - 6: PRINT "Hold A to clear"
  19.         LOCATE AlertLocationHigh + 3, AlertLocation - 10: PRINT "Log=" + DownLog$
  20.         LOCATE AlertLocationHigh + 4, AlertLocation - 10: PRINT "Press L For Todays Log"
  21.     END IF
  22.  
  23.     IF flag = 0 AND set = 0 THEN
  24.         set = 2
  25.         COLOR 3, 1
  26.         LOCATE AlertLocationHigh, AlertLocation: PRINT "    "
  27.         LOCATE AlertLocationHigh + 2, AlertLocation - 6: PRINT "                "
  28.         LOCATE AlertLocationHigh + 3, AlertLocation - 10: PRINT "                      "
  29.         LOCATE AlertLocationHigh + 4, AlertLocation - 10: PRINT "Press L For Todays Log"
  30.         LOCATE AlertLocationHigh + 5, AlertLocation - 11: PRINT "Press E to edit IP list"
  31.     END IF
  32.  
  33.     IF INKEY$ = CHR$(27) THEN 999
  34.     IF UCASE$(INKEY$) = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
  35.     IF UCASE$(INKEY$) = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
  36.     IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0
  37.     OPEN ConfigFile$ FOR INPUT AS #1
  38.     OPEN DownLog$ FOR APPEND AS #2
  39.     COLOR 14, 1
  40.     Title1$ = "Network up/down" + "       Press ESC to exit"
  41.  
  42.     Title3$ = "Settings file (" + ConfigFile$ + ") found."
  43.     note$ = "File " + ConfigFile$ + " can be edited while the program runs. IPs scroll."
  44.     LOCATE 3, 41 - LEN(Title1$) / 2: PRINT Title1$, Title2$: COLOR 4, 15: LOCATE 2, 7: PRINT "Local IP (" + LTRIM$(GetLocalIP$) + ")"
  45.     IF _FILEEXISTS(ConfigFile$) THEN COLOR 13, 1: LOCATE 4, 41 - LEN(Title3$) / 2: PRINT Title3$
  46.     LOCATE 50, 41 - LEN(note$) / 2: PRINT note$;
  47.     DO
  48.         IF INKEY$ = CHR$(27) THEN 999
  49.         IF UCASE$(INKEY$) = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
  50.         IF UCASE$(INKEY$) = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
  51.         IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0
  52.         IF flag = 1 AND set = 0 THEN
  53.             set = 1
  54.             COLOR 20, 15
  55.             LOCATE AlertLocationHigh, AlertLocation: PRINT "****"
  56.             COLOR 4, 15
  57.             LOCATE AlertLocationHigh + 2, AlertLocation - 6: PRINT "Hold A to clear"
  58.             LOCATE AlertLocationHigh + 3, AlertLocation - 10: PRINT "Log=" + DownLog$
  59.             LOCATE AlertLocationHigh + 4, AlertLocation - 10: PRINT "Press L For Todays Log"
  60.         END IF
  61.  
  62.         IF flag = 0 AND set = 0 THEN
  63.             set = 2
  64.             COLOR 3, 1
  65.             LOCATE AlertLocationHigh, AlertLocation: PRINT "    "
  66.             LOCATE AlertLocationHigh + 2, AlertLocation - 6: PRINT "                "
  67.             LOCATE AlertLocationHigh + 3, AlertLocation - 10: PRINT "                      "
  68.             LOCATE AlertLocationHigh + 4, AlertLocation - 10: PRINT "Press L For Todays Log"
  69.             LOCATE AlertLocationHigh + 5, AlertLocation - 11: PRINT "Press E to edit IP list"
  70.  
  71.         END IF
  72.  
  73.         IF EOF(1) THEN CLOSE: EXIT DO
  74.         LINE INPUT #1, ipLine$
  75.         IF INKEY$ = CHR$(27) THEN 999
  76.         IF UCASE$(INKEY$) = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
  77.         IF UCASE$(INKEY$) = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
  78.         IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0
  79.         ip$ = LEFT$(ipLine$, INSTR(ipLine$, " ") - 1)
  80.         desc$ = RIGHT$(ipLine$, LEN(ipLine$) - LEN(ip$) - 1)
  81.         COLOR 8, 15: b = 5: c = 2 'b moves IP and description status line over while c moves **/up/down around that line
  82.         LOCATE 2, 64 - LEN(DATE$ + " " + LEFT$(TIME$, 5)) / 2: PRINT DATE$ + " " + LEFT$(TIME$, 5)
  83.         linelocate = linelocate + 1: IF linelocate = 45 THEN linelocate = 1
  84.         lineclear$ = "": FOR lclear = 1 TO 52: lineclear$ = lineclear$ + " ": NEXT
  85.         LOCATE linelocate + ipstartline, b: COLOR 3, 1: PRINT lineclear$
  86.         IF yesno = 0 THEN COLOR 15, 1 ELSE COLOR 15, 6
  87.         LOCATE linelocate + ipstartline, b: PRINT "Ping ("; ip$; ") ("; desc$; ")";
  88.         IF INKEY$ = CHR$(27) THEN 999
  89.         IF UCASE$(INKEY$) = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
  90.         IF UCASE$(INKEY$) = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
  91.         IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0
  92.         COLOR 7, 1: LOCATE , c: PRINT "**";
  93.         result& = _SHELLHIDE("ping " + ip$ + " -n 1")
  94.         IF result& = 0 THEN
  95.             'success
  96.             COLOR 2, 15
  97.             LOCATE , c: PRINT "UP"
  98.             COLOR 7, 1
  99.         ELSE
  100.             COLOR 4, 15
  101.             LOCATE , c: PRINT "DN"
  102.             COLOR 7, 1
  103.             PRINT #2, DATE$ + " " + TIME$ + " " + "Ping (" + ip$ + ") (" + desc$ + ")" + "   Down": flag = 1: set = 0
  104.         END IF
  105.         IF INKEY$ = CHR$(27) THEN 999
  106.         IF UCASE$(INKEY$) = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
  107.         IF UCASE$(INKEY$) = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
  108.         IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0
  109.     LOOP
  110.     _LIMIT 1 'once per second
  111.     IF yesno = 0 THEN yesno = 1 ELSE yesno = 0
  112. 999
  113. PRINT "Shutting down"
  114.  
  115. FUNCTION GetLocalIP$
  116.     fill = 3
  117.     SHELL _HIDE "cmd /c ipconfig > IPconfig.tmp"
  118.     OPEN "IPconfig.tmp" FOR INPUT AS #fill
  119.     DO
  120.         LINE INPUT #fill, ipline$
  121.         IF LEFT$(LTRIM$(ipline$), 2) = "IP" THEN
  122.             CLOSE #fill
  123.             GetLocalIP$ = MID$(ipline$, INSTR(ipline$, ":") + 1)
  124.             IF LEFT$(ipline$, 3) = "169" THEN ipline$ = "Invalid IP" + ipline$
  125.             KILL "IPconfig.tmp" 'kill the messenger?
  126.             EXIT DO
  127.         END IF
  128.     LOOP UNTIL EOF(fill)
  129.  
  130. SUB example (ConfigFile%, ReadMe%)
  131.     IF _FILEEXISTS(ConfigFile$) = 0 THEN
  132.         OPEN ConfigFile$ FOR OUTPUT AS #1
  133.         PRINT #1, "192.168.1.1 Router"
  134.         PRINT #1, "1.1.1.1 DNS Server"
  135.         PRINT #1, "192.168.1.254 Wifi Access Point"
  136.         PRINT #1, "qb64.com QB64.com (doesn't exist)"
  137.         PRINT #1, "MUST include Ip, hostname or domain name and a description"
  138.         PRINT #1, "Otherwise the program will not be able to ping properly."
  139.         CLOSE
  140.     END IF
  141.     ReadMe$ = "read.me.txt"
  142.     IF _FILEEXISTS(ReadMe$) = 0 THEN
  143.         OPEN ReadMe$ FOR OUTPUT AS #1
  144.         PRINT #1, "Written by Chris Owen Chris.Owen09@gmail.com"
  145.         PRINT #1, "This program came about from a necessity of keeping tabs on servers, switches, a router, a access point, and some printers, other windows pings were not able to accommodate my needs."
  146.         PRINT #1, "Much thanks goes to FellippeHeitor for the starting code, after seeing how my first work was clumsy he pointed me to what this has become."
  147.         PRINT #1, "The Config.ini file shall contain all the ips you wish to monitor. It is recommended to edit it to your needs."
  148.         PRINT #1, "The Config.ini file is dynamic so changes can be made without stopping the program, the file is read line by line and not all loaded at one time."
  149.         PRINT #1, "This makes changing the file and reading the data in slower but you can monitor while changes are being done using a text editor."
  150.         PRINT #1, "The program can ping, IPs, domain names and host names, descriptions are necessary. I used ( cmd Ping 1.1.1.1 -n 1 ) for the results"
  151.         PRINT #1, "192.168.1.254 Wifi Access Point will be reported on the main screen as separate items using a UP or DN status."
  152.         PRINT #1, "Since the program has to shell out to run the Cmd PING speed is slow so I have added a -n 1 count instead of the usual 3 count."
  153.         PRINT #1, "This program will show 44 lines of ips, 45 and above will start a scroll effect and will pick up at the first ip in you list."
  154.         PRINT #1, "Downed IPs are logged to a file called ( down????.log ) where ???? is the date it was noticed."
  155.         PRINT #1, "In the log, simple details such as the date, time, and ip and if you put in a description are logged."
  156.         PRINT #1, "ESC exits the program and holding the ( a ) key acknowledges any visual down notification."
  157.         PRINT #1, "The program will self create missing files such as the Config.ini where the IPs are read from, and This read.me.txt file every run."
  158.         PRINT #1, "You MUST include Ip, hostname or domain name and a description.  Otherwise the program will not be able to ping properly."
  159.         PRINT #1, "The pinger now changes color each completed loop so you can see its progress.  On a fast computer with little overhead this thing is blazing."
  160.         PRINT #1, "In addition to ESC and A to acknowledge I added E to launch notepad to edit the IP config file and L to launch the Log of the day."
  161.         PRINT #1, "Tap the buttons a few times to launch The Inkey does not seem to catch even with them spread everywhere in the program."
  162.         PRINT #1, "Added a search function for the IP that the machine is running on since I had a few issues with DHCP not working on a DDWRT that caused a headache. "
  163.         CLOSE
  164.     END IF
  165.     SHELL _DONTWAIT "notepad " + ReadMe$
  166.     SHELL _DONTWAIT "notepad " + ConfigFile$
  167.     SYSTEM
  168.  
And this would be how I catch spelling and keyboard errors.
Reply if anyone has ideas on how to clean this up some
Thanks,
« Last Edit: January 03, 2019, 11:06:03 am by Chris80194 »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
The issue is here:

Log$ = "Down" + DATE$ + ".LOG"
    IF UCASE$(INKEY$) = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
    IF UCASE$(INKEY$) = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
    IF UCASE$(INKEY$) = "A" THEN flag = 0: set = 0

Each call to INKEY$ reads a character from the input buffer.  Type “AA”, and it’ll fail to run as the first inkey$ is “A”, the second inkey$ is “A”, andthe third inkey$ is “”...

Fix is simple:

  Char$ = UCASE$(INKEY$)
  IF Char$ = “E” THEN...
  IF Char$ = “L” THEN ...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline visionmercer

  • Newbie
  • Posts: 8
    • View Profile
The times I have needed to know if a host is up or down i have used the _shellhide function, so i didn't need an output to file.

a = _SHELLHIDE("ping " + Networkaddress$ + " -n 1 -w 100")

if a is 0 then the host is up
if a is 1 the host is down

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
To address SMcNeill you are right. It was late when I added the extra INKEY items and was thrilled the program does pretty much what I wanted with out the limitations I had found in the ones I had tried and this morning fresh and clear headed looked at your post I remembered that from when I learned Qbasic and was given a QB compiler package way back in the 90s.  Extra eyes are helpful especially after 10 hours of beating on keys.  Wish I could see code like I used to its coming back with yalls help.  Thanks

visionmercer thanks FellippeHeitor pointed me in that similar direction and I am grateful for yours too I have a few others programs I am working on so ill try this out and see if it runs any faster.

Ping is such a basic command.  I wrote this thinking I would need about 10 devices monitored but the list is about 24 now and testing shows that it can do 24 lines and seems limitless. FellippeHeitor's help with reading in and cypher out the config file made that happen.

Let me see how the suggestion works out.  It almost rivals the professionals speed wise.
Thanks,

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
So its been running great for a while but I made a few tweaks. To highlight down nodes better.
Code: QB64: [Select]
  1. SCREEN 0: WIDTH 80, 50
  2. DIM SHARED ConfigFile$, ReadMe$
  3. ConfigFile$ = "config.ini"
  4. ReadMe$ = "read.me.txt"
  5. IF _FILEEXISTS(ConfigFile$) = 0 THEN CALL example(ConfigFile%, ReadMe%)
  6. IF _FILEEXISTS(ReadMe$) = 0 THEN CALL example(ConfigFile%, ReadMe%)
  7. COLOR 3, 1: CLS
  8. flag = 0: set = 0: AlertLocation = 68: AlertLocationHigh = 10: ipstartline = 4
  9.     DownLog$ = "Down" + DATE$ + ".LOG"
  10.     IF Char$ = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
  11.     IF Char$ = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
  12.     IF Char$ = "A" THEN flag = 0: set = 0
  13.     IF flag = 1 AND set = 0 THEN
  14.         set = 1
  15.         COLOR 20, 15
  16.         LOCATE AlertLocationHigh, AlertLocation: PRINT "****"
  17.         COLOR 4, 15
  18.         LOCATE AlertLocationHigh + 2, AlertLocation - 6: PRINT "Hold A to clear"
  19.         LOCATE AlertLocationHigh + 3, AlertLocation - 10: PRINT "Log=" + DownLog$
  20.         LOCATE AlertLocationHigh + 4, AlertLocation - 10: PRINT "Press L For Todays Log"
  21.     END IF
  22.  
  23.     IF flag = 0 AND set = 0 THEN
  24.         set = 2
  25.         COLOR 3, 1
  26.         LOCATE AlertLocationHigh, AlertLocation: PRINT "    "
  27.         LOCATE AlertLocationHigh + 2, AlertLocation - 6: PRINT "                "
  28.         LOCATE AlertLocationHigh + 3, AlertLocation - 10: PRINT "                      "
  29.     END IF
  30.  
  31.     Char$ = INKEY$: IF Char$ = CHR$(27) THEN 999
  32.     IF Char$ = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
  33.     IF Char$ = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
  34.     IF Char$ = "A" THEN flag = 0: set = 0
  35.     OPEN ConfigFile$ FOR INPUT AS #1
  36.     OPEN DownLog$ FOR APPEND AS #2
  37.     COLOR 14, 1
  38.     Title1$ = "Network up/down"
  39.     Title2a$ = "Press ESC to exit"
  40.     Title2b$ = "Press L For Todays Log"
  41.     Title2c$ = "Press E to edit IP list"
  42.     Title3$ = "Settings file (" + ConfigFile$ + ") found."
  43.     note$ = "File " + ConfigFile$ + " can be edited while the program runs. IPs scroll."
  44.     LOCATE AlertLocationHigh - 1, AlertLocation - 8: PRINT Title2a$
  45.     LOCATE AlertLocationHigh + 4, AlertLocation - 10: PRINT Title2b$
  46.     LOCATE AlertLocationHigh + 5, AlertLocation - 11: PRINT Title2c$
  47.  
  48.     LOCATE 3, 41 - LEN(Title1$) / 2: PRINT Title1$: COLOR 4, 15: LOCATE 2, 7: PRINT "Local IP (" + LTRIM$(GetLocalIP$) + ")"
  49.     IF _FILEEXISTS(ConfigFile$) THEN COLOR 13, 1: LOCATE 4, 41 - LEN(Title3$) / 2: PRINT Title3$
  50.     LOCATE 50, 41 - LEN(note$) / 2: PRINT note$;
  51.     DO
  52.         Char$ = INKEY$: IF Char$ = CHR$(27) THEN 999
  53.         IF Char$ = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
  54.         IF Char$ = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
  55.         IF Char$ = "A" THEN flag = 0: set = 0
  56.         IF flag = 1 AND set = 0 THEN
  57.             set = 1
  58.             COLOR 20, 15
  59.             LOCATE AlertLocationHigh, AlertLocation: PRINT "****"
  60.             COLOR 4, 15
  61.             LOCATE AlertLocationHigh + 2, AlertLocation - 6: PRINT "Hold A to clear"
  62.             LOCATE AlertLocationHigh + 3, AlertLocation - 10: PRINT "Log=" + DownLog$
  63.             LOCATE AlertLocationHigh + 4, AlertLocation - 10: PRINT "Press L For Todays Log"
  64.         END IF
  65.  
  66.         IF flag = 0 AND set = 0 THEN
  67.             set = 2
  68.             COLOR 3, 1
  69.             LOCATE AlertLocationHigh, AlertLocation: PRINT "    "
  70.             LOCATE AlertLocationHigh + 2, AlertLocation - 6: PRINT "                "
  71.             LOCATE AlertLocationHigh + 3, AlertLocation - 10: PRINT "                      "
  72.  
  73.         END IF
  74.  
  75.         IF EOF(1) THEN CLOSE: EXIT DO
  76.         LINE INPUT #1, ipLine$
  77.         Char$ = INKEY$: IF Char$ = CHR$(27) THEN 999
  78.         IF Char$ = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
  79.         IF Char$ = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
  80.         IF Char$ = "A" THEN flag = 0: set = 0
  81.         ip$ = LEFT$(ipLine$, INSTR(ipLine$, " ") - 1)
  82.         desc$ = RIGHT$(ipLine$, LEN(ipLine$) - LEN(ip$) - 1)
  83.         COLOR 8, 15: b = 5: c = 2 'b moves IP and description status line over while c moves **/up/down around that line
  84.         LOCATE 2, 64 - LEN(DATE$ + " " + LEFT$(TIME$, 5)) / 2: PRINT DATE$ + " " + LEFT$(TIME$, 5)
  85.         linelocate = linelocate + 1: IF linelocate = 45 THEN linelocate = 1
  86.         lineclear$ = "": FOR lclear = 1 TO 52: lineclear$ = lineclear$ + " ": NEXT
  87.         LOCATE linelocate + ipstartline, b: COLOR 3, 1: PRINT lineclear$
  88.         IF yesno = 0 THEN COLOR 15, 1 ELSE COLOR 15, 6
  89.         LOCATE linelocate + ipstartline, b: PRINT "Ping ("; ip$; ") ("; desc$; ")";
  90.         Char$ = INKEY$: IF Char$ = CHR$(27) THEN 999
  91.         IF Char$ = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
  92.         IF Char$ = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
  93.         IF Char$ = "A" THEN flag = 0: set = 0
  94.         COLOR 7, 1: LOCATE , c: PRINT "** ";
  95.         ' Shell to ping and get results 0 up 1 down
  96.         result& = _SHELLHIDE("ping " + ip$ + " -n 1 -w 100")
  97.         IF result& = 0 THEN
  98.             'success
  99.             COLOR 2, 15
  100.             LOCATE , c: PRINT "UP"
  101.             COLOR 7, 1
  102.         ELSE
  103.             COLOR 4, 15
  104.             LOCATE , c: PRINT "DN"; " Ping ("; ip$; ") ("; desc$; ")";
  105.             COLOR 7, 1
  106.             PRINT #2, DATE$ + " " + TIME$ + " " + "Ping (" + ip$ + ") (" + desc$ + ")" + "   Down": flag = 1: set = 0
  107.         END IF
  108.         Char$ = UCASE$(INKEY$): IF Char$ = CHR$(27) THEN 999
  109.         IF Char$ = "E" THEN SHELL _DONTWAIT "notepad " + ConfigFile$
  110.         IF Char$ = "L" THEN SHELL _DONTWAIT "notepad " + DownLog$
  111.         IF Char$ = "A" THEN flag = 0: set = 0
  112.     LOOP
  113.     _LIMIT 1 'once per second
  114.     IF yesno = 0 THEN yesno = 1 ELSE yesno = 0
  115. 999
  116. PRINT "Shutting down"
  117.  
  118. FUNCTION GetLocalIP$
  119.     fill = 3
  120.     SHELL _HIDE "cmd /c ipconfig > IPconfig.tmp"
  121.     OPEN "IPconfig.tmp" FOR INPUT AS #fill
  122.     DO
  123.         LINE INPUT #fill, ipline$
  124.         IF LEFT$(LTRIM$(ipline$), 2) = "IP" THEN
  125.             CLOSE #fill
  126.             GetLocalIP$ = MID$(ipline$, INSTR(ipline$, ":") + 1)
  127.             IF LEFT$(ipline$, 3) = "169" THEN ipline$ = "Invalid IP" + ipline$
  128.             KILL "IPconfig.tmp" 'kill the messenger?
  129.             EXIT DO
  130.         END IF
  131.     LOOP UNTIL EOF(fill)
  132.  
  133. SUB example (ConfigFile%, ReadMe%)
  134.     IF _FILEEXISTS(ConfigFile$) = 0 THEN
  135.         OPEN ConfigFile$ FOR OUTPUT AS #1
  136.         PRINT #1, "192.168.1.1 Router"
  137.         PRINT #1, "1.1.1.1 DNS Server"
  138.         PRINT #1, "192.168.1.254 Wifi Access Point"
  139.         PRINT #1, "qb64.com QB64.com (doesn't exist)"
  140.         PRINT #1, "MUST include Ip, hostname or domain name and a description"
  141.         PRINT #1, "Otherwise the program will not be able to ping properly."
  142.         CLOSE
  143.     END IF
  144.     ReadMe$ = "read.me.txt"
  145.     IF _FILEEXISTS(ReadMe$) = 0 THEN
  146.         OPEN ReadMe$ FOR OUTPUT AS #1
  147.         PRINT #1, "Written by Chris Owen Chris.Owen09@gmail.com"
  148.         PRINT #1, "This program came about from a necessity of keeping tabs on servers, switches, a router, a access point, and some printers, other windows pings were not able to accommodate my needs."
  149.         PRINT #1, "Much thanks goes to FellippeHeitor for the starting code, after seeing how my first work was clumsy he pointed me to what this has become."
  150.         PRINT #1, "visionmercer pointed out another ping parameter that I added as a tweak  looking to make this fast as it can be."
  151.         PRINT #1, "SMcNeill pointed out a flaw staring me in the face.  In the interest of reduction of variables I knew I wanted to check INKEY values but by that I created a problem where a simpler approach was needed. Thanks"
  152.         PRINT #1, "The Config.ini file shall contain all the ips you wish to monitor. It is recommended to edit it to your needs."
  153.         PRINT #1, "The Config.ini file is dynamic so changes can be made without stopping the program, the file is read line by line and not all loaded at one time."
  154.         PRINT #1, "This makes changing the file and reading the data in slower but you can monitor while changes are being done using a text editor."
  155.         PRINT #1, "The program can ping, IPs, domain names and host names, descriptions are necessary. I used ( cmd Ping 1.1.1.1 -n 1 ) for the results"
  156.         PRINT #1, "192.168.1.254 Wifi Access Point will be reported on the main screen as separate items using a UP or DN status."
  157.         PRINT #1, "Since the program has to shell out to run the Cmd PING speed is slow so I have added a -n 1 count instead of the usual 3 count."
  158.         PRINT #1, "This program will show 44 lines of ips, 45 and above will start a scroll effect and will pick up at the first ip in you list."
  159.         PRINT #1, "Downed IPs are logged to a file called ( down????.log ) where ???? is the date it was noticed."
  160.         PRINT #1, "In the log, simple details such as the date, time, and ip and if you put in a description are logged."
  161.         PRINT #1, "ESC exits the program and holding the ( a ) key acknowledges any visual down notification."
  162.         PRINT #1, "The program will self create missing files such as the Config.ini where the IPs are read from, and This read.me.txt file every run."
  163.         PRINT #1, "You MUST include Ip, hostname or domain name and a description.  Otherwise the program will not be able to ping properly."
  164.         PRINT #1, "The pinger now changes color each completed loop so you can see its progress.  On a fast computer with little overhead this thing is blazing."
  165.         PRINT #1, "In addition to ESC and A to acknowledge I added E to launch notepad to edit the IP config file and L to launch the Log of the day."
  166.         PRINT #1, "Tap the buttons a few times to launch The Inkey does not seem to catch even with them spread everywhere in the program."
  167.         PRINT #1, "Added a search function for the IP that the machine is running on since I had a few issues with DHCP not working on a DDWRT that caused a headache. "
  168.         CLOSE
  169.     END IF
  170.     SHELL _DONTWAIT "notepad " + ReadMe$
  171.     SHELL _DONTWAIT "notepad " + ConfigFile$
  172.     SYSTEM
  173.  
  174.