Announcement

Collapse
1 of 2 < >

New Sub-Forum

In an effort to help make sure there are appropriate categories for topics of discussion that are happening, there is now a sub-forum for databases and database programming under Special Interest groups. Please direct questions, etc., about this topic to that sub-forum moving forward. Thank you.
2 of 2 < >

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

ping site with console program

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBCC ping site with console program

    a program that i altered from Paul Dwyer's ping program.
    i removed some code and added very little so any credit should be sent Paul Dwyer's way.

    the purpose of the program is ping a site and return an errlevel for batch programs. i use this program inside a batch fille that is run every time my computer is booted up and it does very well for laptop computers.
    i will list the batch file that i have created and i wrote a program i name sleep.exe to put some sleep states in the batch file.

    this program is very helpful if your internet service is down a lot or your testing when your internet service starts back up. simply by placing a beep or song or some weird graphic display in the batch file.

    my program, sleep, should be listed somewhere here on in the source code section.
    my program ,sleepbrk, does is almost the same program as sleep except it allows you to break the sleepbrk program by hitting a key.


    both the batch file and two programs can be download from http://pdptemp.dyndns.org/zipcode for a short time from this date.

    Code:
    'compiled with pbcc 4.03
    'pingstrt.bas 
    
    #COMPILE EXE
    
    #INCLUDE "WIN32API.INC"
    
    
    %IP_SUCCESS                     = 0
    %IP_STATUS_BASE                 = 11000
    %IP_BUF_TOO_SMALL               = (11000 + 1)
    %IP_DEST_NET_UNREACHABLE        = (11000 + 2)
    %IP_DEST_HOST_UNREACHABLE       = (11000 + 3)
    %IP_DEST_PROT_UNREACHABLE       = (11000 + 4)
    %IP_DEST_PORT_UNREACHABLE       = (11000 + 5)
    %IP_NO_RESOURCES                = (11000 + 6)
    %IP_BAD_OPTION                  = (11000 + 7)
    %IP_HW_ERROR                    = (11000 + 8)
    %IP_PACKET_TOO_BIG              = (11000 + 9)
    %IP_REQ_TIMED_OUT               = (11000 + 10)
    %IP_BAD_REQ                     = (11000 + 11)
    %IP_BAD_ROUTE                   = (11000 + 12)
    %IP_TTL_EXPIRED_TRANSIT         = (11000 + 13)
    %IP_TTL_EXPIRED_REASSEM         = (11000 + 14)
    %IP_PARAM_PROBLEM               = (11000 + 15)
    %IP_SOURCE_QUENCH               = (11000 + 16)
    %IP_OPTION_TOO_BIG              = (11000 + 17)
    %IP_BAD_DESTINATION             = (11000 + 18)
    %IP_ADDR_DELETED                = (11000 + 19)
    %IP_SPEC_MTU_CHANGE             = (11000 + 20)
    %IP_MTU_CHANGE                  = (11000 + 21)
    %IP_UNLOAD                      = (11000 + 22)
    %IP_ADDR_ADDED                  = (11000 + 23)
    %IP_GENERAL_FAILURE             = (11000 + 50)
    %MAX_IP_STATUS                  = (11000 + 50)
    %IP_PENDING                     = (11000 + 255)
    %PING_TIMEOUT                   = 3000
    %WS_VERSION_REQD                = &H101
    %MIN_SOCKETS_REQD               = 1
    %SOCKET_ERROR                   = -1
    %INADDR_NONE                    = &HFFFFFFFF
    %MAX_WSADescription             = 256
    %MAX_WSASYSStatus               = 128
    
    TYPE ICMP_OPTIONS
        Ttl             AS BYTE
        Tos             AS BYTE
        Flags           AS BYTE
        OptionsSize     AS BYTE
        OptionsData     AS LONG
    END TYPE
    
    TYPE ICMP_ECHO_REPLY
        Address         AS LONG
        lStatus          AS LONG
        RoundTripTime   AS LONG
        DataSize        AS LONG 'formerly integer
       'Reserved        As Integer
        DataPointer     AS LONG
        Options         AS ICMP_OPTIONS
        sData            AS STRING * 250
    END TYPE
    
    TYPE RTT    'Round Trip Time info
        RTT AS LONG
        ErrorCode AS LONG
        ErrorText AS STRING * 24
    END TYPE
    
    DECLARE FUNCTION inet_addr LIB "wsock32.dll" ALIAS "inet_addr" (BYVAL s AS STRING) AS LONG
    DECLARE FUNCTION GetStatusCode(STATUS AS LONG) AS STRING
    DECLARE SUB ping(sHost AS STRING, lTimeOutMs AS LONG, payload AS STRING, oRTT AS RTT)
    DECLARE FUNCTION ResolveHost(sHost AS STRING) AS STRING
    DECLARE FUNCTION IcmpCreateFile LIB "icmp.dll" ALIAS "IcmpCreateFile"  () AS LONG
    DECLARE FUNCTION IcmpCloseHandle LIB "icmp.dll" ALIAS "IcmpCloseHandle"(BYVAL IcmpHandle AS LONG) AS LONG
    
    DECLARE FUNCTION IcmpSendEcho LIB "icmp.dll" ALIAS "IcmpSendEcho" _
       (BYVAL IcmpHandle AS LONG, _
        BYVAL DestinationAddress AS LONG, _
        BYVAL RequestData AS STRING, _
        BYVAL RequestSize AS LONG, _
        BYVAL RequestOptions AS LONG, _
        ReplyBuffer AS ICMP_ECHO_REPLY, _
        BYVAL ReplySize AS LONG, BYVAL lTimeout AS LONG) AS LONG
    
    '===================================================================
    
    FUNCTION GetStatusCode(STATUS AS LONG) AS STRING
    
       DIM msg AS STRING
    
       SELECT CASE STATUS
          CASE %IP_SUCCESS:               msg = "Success"
          CASE %INADDR_NONE:              msg = "Bad IP format"
          CASE %IP_BUF_TOO_SMALL:         msg = "Buffer too small"
          CASE %IP_DEST_NET_UNREACHABLE:  msg = "Dest net unreachable"
          CASE %IP_DEST_HOST_UNREACHABLE: msg = "Dest host unreachable"
          CASE %IP_DEST_PROT_UNREACHABLE: msg = "Dest prot unreachable"
          CASE %IP_DEST_PORT_UNREACHABLE: msg = "Dest port unreachable"
          CASE %IP_NO_RESOURCES:          msg = "No resources"
          CASE %IP_BAD_OPTION:            msg = "Bad option"
          CASE %IP_HW_ERROR:              msg = "HW Error"
          CASE %IP_PACKET_TOO_BIG:        msg = "Packet too big"
          CASE %IP_REQ_TIMED_OUT:         msg = "Req timed out"
          CASE %IP_BAD_REQ:               msg = "Bad req"
          CASE %IP_BAD_ROUTE:             msg = "Bad route"
          CASE %IP_TTL_EXPIRED_TRANSIT:   msg = "TTL expired transit"
          CASE %IP_TTL_EXPIRED_REASSEM:   msg = "TTL expired reassem"
          CASE %IP_PARAM_PROBLEM:         msg = "Param problem"
          CASE %IP_SOURCE_QUENCH:         msg = "Source quench"
          CASE %IP_OPTION_TOO_BIG:        msg = "Option too big"
          CASE %IP_BAD_DESTINATION:       msg = "Bad destination"
          CASE %IP_ADDR_DELETED:          msg = "Addr deleted"
          CASE %IP_SPEC_MTU_CHANGE:       msg = "Spec MTU change"
          CASE %IP_MTU_CHANGE:            msg = "MTU change"
          CASE %IP_UNLOAD:                msg = "Unload"
          CASE %IP_ADDR_ADDED:            msg = "Addr added"
          CASE %IP_GENERAL_FAILURE:       msg = "General failure"
          CASE %IP_PENDING:               msg = "Pending"
          CASE %PING_TIMEOUT:             msg = "Ping timeout"
          CASE ELSE:                      msg = "Unknown Err " & FORMAT$(STATUS)
       END SELECT
    
       GetStatusCode =  msg
    
    END FUNCTION
    
    '===================================================================
    
    SUB ping(sHost AS STRING, lTimeOutMs AS LONG, payload AS STRING, oRTT AS RTT)
    
        DIM hPort AS LONG
        DIM dwAddress AS LONG
        DIM ECHO AS ICMP_ECHO_REPLY
        DIM pOptions AS LONG
        DIM ResolvedHost AS STRING
    
        'convert the address into a long representation
        dwAddress = inet_addr(sHost)
    
        IF dwAddress = -1 THEN
            'Might be a host name, try to resolve first.
             ResolvedHost = ResolveHost(sHost)
             dwAddress = inet_addr(ResolvedHost)
    
             IF dwAddress = -1 THEN    'If still bad then fail
    
                oRTT.ErrorCode = -1
                oRTT.ErrorText = "DNS Resolution Failure"
                EXIT SUB
             END IF
        END IF
    
       'open a port
        hPort = IcmpCreateFile()
        IF hPort THEN
    
            'ping it.
            CALL IcmpSendEcho(hPort, dwAddress, payload, LEN(payload), pOptions, ECHO, LEN(ECHO), lTimeOutMs)
    
            'return the status as ping succes and close
            oRTT.ErrorCode = ECHO.lstatus
            oRTT.ErrorText = GetStatusCode(ECHO.lstatus)
            oRTT.RTT = ECHO.RoundTripTime
            CALL IcmpCloseHandle(hPort)
    
        END IF
    
    END SUB
    
    '================================================================================
    
    FUNCTION ResolveHost(sHost AS STRING) AS STRING
    
        DIM p AS BYTE PTR
        DIM IP AS DWORD
        HOST ADDR sHost TO IP
        p = VARPTR(IP)
        ResolveHost = USING$("#_.#_.#_.#", @p, @p[1], @p[2], @p[3])
    
    END FUNCTION
    
    '===================================================================
    
    FUNCTION PBMAIN() AS LONG
    
        DIM sHost AS STRING
        DIM TimeOutSecs AS LONG
        DIM Payload AS STRING
        DIM RoundTripInfo AS RTT
    
        
        sHost = trim$(COMMAND$)
        IF LEN(sHost)=0 THEN 
            FUNCTION=2
            STDOUT "ERRORLEVEL 2 returned"
            EXIT FUNCTION
        END IF 
    TimeOutSecs = 2000
        Payload = "abcdefghijklmnop"
    
        CALL ping(sHost, TimeOutSecs, Payload, RoundTripInfo)
        STDOUT  "pinging " & sHost
        STDOUT  "Status: " & FORMAT$(RoundTripInfo.ErrorCode) & " (" & TRIM$(RoundTripInfo.ErrorText) & ")"
        STDOUT  "Time: " & FORMAT$(RoundTripInfo.RTT) & " ms"
        IF roundtripinfo.errorcode=0 THEN
            FUNCTION=0
            STDOUT "ERRORLEVEL 0 returned"
            ELSE
            FUNCTION=1
            STDOUT "ERRORLEVEL 1 returned"
        END IF
    
    END FUNCTION
    the batch file i use
    Code:
    rem ckinternet.bat
    echo off
    c:
    cd \wsbat
    :internet
    cls
    echo _
    echo           checking internet in 1 seconds
    echo _
    sleep 1.25
    echo           checking for www.yahoo.com
    SLEEP 1
    pingstrt www.yahoo.com
    IF ERRORLEVEL == 1 GOTO AROUND1
    IF ERRORLEVEL == 0 goto endofcheck
    :AROUND1
    echo _
    echo _         checking for google.com
    SLEEP 1
    pingstrt www.google.com
    IF ERRORLEVEL == 1 GOTO AROUND2
    IF ERRORLEVEL == 0 goto endofcheck
    :AROUND2
    SLEEP 1
    goto internet
    :endofcheck
    exit
    Last edited by Paul Purvis; 22 Oct 2007, 05:03 AM. Reason: made program exit with errorlevel 2 if nothing given on command tail
    p purvis

  • #2
    problems with networked printers or print servers or just about any network device?
    i did, print servers and i never thought about using the program above to help users test and identify a network or device problem and help the user in a more human way to check equipment to see if it responds to a ping.
    here is a batch file i just created to help users solves some problems with print servers. it that may be changed for your needs. the wording is not quiet polished yet but you can do that.

    i did not want to make a new thread but thought this to be more of a source code kind of thing due to batch file programing , and it does not need any discussion.
    see source code section for the source code to sleepbrk
    http://www.powerbasic.com/support/pb...ad.php?t=37234

    Code:
    echo off
    c:
    cls
    echo  this program only checks for existence of network devices
    echo  this program does not check if device is connected to this computer
    echo  devices checked are print servers and/or
    echo      printers with builtin network capabilities 
    echo ========================================================================
    echo  checking existence of 192.168.0.96
    SLEEP 1
    pingstrt 192.168.0.96 > NUL:
    IF ERRORLEVEL == 1 GOTO AROUND1
    IF ERRORLEVEL == 0 goto ENDOFPRTR1
    :AROUND1
    echo ERROR     no response from print server at 192.168.0.96
    echo ERROR     print server name is LK??????
    echo ERROR     possible print server location - in hallway 
    echo ERROR     ip address of print server 192.168.0.96
    echo --------------------------------------------------------
    GOTO CKPRTR2
    :ENDOFPRTR1
    echo  the device can be seen from this machine
    echo --------------------------------------------------------
    :CKPRTR2
    echo  checking existence of 192.168.0.91
    SLEEP 1
    pingstrt 192.168.0.91 > NUL:
    IF ERRORLEVEL == 1 GOTO AROUND2
    IF ERRORLEVEL == 0 goto ENDOFPRTR2
    :AROUND2
    echo ERROR     no response from print server at ip location 192.168.0.91
    echo ERROR     print server name is LK661F2A
    echo ERROR     the possible location of print server - collection office
    echo ERROR     ip address of print server 192.168.0.91
    echo --------------------------------------------------------
    GOTO CKPRTR3
    :ENDOFPRTR2
    echo  the device can be seen from this machine
    echo --------------------------------------------------------
    :CKPRTR3
    echo  checking existence of 192.168.0.92
    SLEEP 1
    pingstrt 192.168.0.92 > NUL:
    IF ERRORLEVEL == 1 GOTO AROUND3
    IF ERRORLEVEL == 0 goto ENDOFPRTR3
    :AROUND3
    echo ERROR     no response from print server at ip location 192.168.0.92
    echo ERROR     print server name is LK90C640
    echo ERROR     the possible location of print server - front counter
    echo ERROR     ip address of print server 192.168.0.92
    echo --------------------------------------------------------
    GOTO ENDOFCHECKING
    :ENDOFPRTR3
    echo  the device can be seen from this machine
    echo --------------------------------------------------------
    :ENDOFCHECKING
    echo ========================================================================
    echo           finished checking print servers
    echo           press any key to continue or in 120 seconds continue will be automatic
    sleepbrk 120
    exit
    Last edited by Paul Purvis; 26 Apr 2008, 03:21 PM.
    p purvis

    Comment


    • #3
      humm
      the above program returns a successful ping in windows vista on my laptop

      so there is my batch file to ping a site once again on the internet
      i did not know ping command would return a result
      it does in vista sp1 64-bit

      i use this batch program to let me know my computer sees the internet when i am on a laptop.

      Code:
      echo off
      c:
      cd \wsbat
      :internet
      cls
      echo -
      echo -          Checking for a connection to the internet in 2 seconds.
      echo -
      sleep 2
      echo -          looking for www.yahoo.com
      ping www.yahoo.com > nul
      IF ERRORLEVEL == 1 GOTO AROUND1
      IF ERRORLEVEL == 0 goto endofcheck
      :AROUND1
      echo _
      echo _          looking for www.google.com
      SLEEP 1
      ping www.google.com > nul
      IF ERRORLEVEL == 1 GOTO AROUND2
      IF ERRORLEVEL == 0 goto endofcheck
      :AROUND2
      echo _
      echo            No connection found.
      SLEEP 1
      goto internet
      :endofcheck
      cls
      echo .
      echo .
      echo .              HOORAY! HOORAY! HOORAY!
      echo .
      echo .          A internet connection was found.
      sleep 4
      cls
      exit
      p purvis

      Comment

      Working...
      X