Results 1 to 10 of 10

Thread: I am trying to write a batch file to ping IP addresses of PLC in field locations

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked
    0 times in 0 posts

    I am trying to write a batch file to ping IP addresses of PLC in field locations

    I am an Automation technician for a major "energy" company. we have PLCs in field locations that have IP addresses, but do not have host names. I am in charge of approximately 180 sites. I want to be able to Ping these sites and generate a text file with the PLC name, instead of the IP address on the report. I have created a batch file, mostly from scraps i have found on forums such as this one, that works, but it has a problem. I was hoping someone could look at it and tell me how to fix it. Instead of pinging the addresses once, it pings the list once, then goes back to the beginning and pings them again and again. but it does generate the report after the 3rd round of pinging.

    here is my batch file:

    @echo off
    echo serching for AWTPLCs.txt
    echo.
    if exist AWTPLCs.txt goto Label1
    echo.
    echo Cannot Find AWTPLCs.txt
    echo.
    pause
    goto :eof

    :label1
    echo file found!
    echo.
    echo Commencing AWT Ping Test...
    echo.
    echo PingTest executed on %date% at %time% > report.txt
    echo ================================================== >> report.txt
    for /f "usebackq tokens=2 delims=:" %%i in (AWTPLCs.txt) do for /f "usebackq tokens=1 delims=:" %%n in (AWTPLCs.txt) do (
    set name=%%n
    call :Sub %%i
    )
    pause
    notepad report.txt
    goto :eof

    :Sub
    echo Testing %name%
    set state=alive
    ping -n 1 %1 | find /i "bytes=" || set state=dead
    echo %name% is %state% >> report.txt


    My txt file, AWTPLCs.txt, that contains PLC ip addresses is formatted like this:

    GS 42: 192.168.0.1
    GS 35: 192.168.0.2
    GS 21: 192.168.0.3

    I have not populated the list yet aside from 3 sites, and am open suggestions. i have changed the IP addresses as well for "corporate security reasons"

    Thank you all for your help,

    -Jason

  2. #2
    <<== UT3 Player spoon_'s Avatar
    Join Date
    Nov 2008
    Location
    London
    Posts
    2,071
    Thanks
    113
    Thanked
    139 times in 131 posts

    Re: I am trying to write a batch file to ping IP addresses of PLC in field locations

    Not the cleanest of solutions but I just woke up...

    :Sub
    echo Testing %name%
    set state=alive
    ping -n 1 %1 | find /i "bytes=" || set state=dead

    TYPE NUL > %TEMP%\WAVE1
    GOTO label1
    TYPE NUL > %TEMP%\WAVE2
    GOTO label1
    TYPE NUL > %TEMP%\WAVE3

    IF EXISTS %TEMP%\WAVE3
    echo %name% is %state% >> report.txt

  3. #3
    root Member DanceswithUnix's Avatar
    Join Date
    Jan 2006
    Location
    In the middle of a core dump
    Posts
    12,335
    Thanks
    714
    Thanked
    1,406 times in 1,188 posts
    • DanceswithUnix's system
      • Motherboard:
      • Asus X470-PRO
      • CPU:
      • 3700X
      • Memory:
      • 32GB 3200MHz ECC
      • Storage:
      • 1TB Linux, 1TB Games (Win 10)
      • Graphics card(s):
      • Asus Strix RX Vega 56
      • PSU:
      • 650W Corsair TX
      • Case:
      • Antec 300
      • Operating System:
      • Fedora 33 + Win 10 Pro 64 (yuk)
      • Monitor(s):
      • Benq XL2730Z 1440p + Iiyama 27" 1440p
      • Internet:
      • Zen 80Mb/20Mb VDSL

    Re: I am trying to write a batch file to ping IP addresses of PLC in field locations

    Rewrite it in a better scripting language. Really, investing time in converting it early will pay off many fold in the long run.

    I would recommend Python, easy enough for kids to learn but a fully capable programming language if you need to go that far.
    Ruby is good as well, but I think it is a bit harder to get started.

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked
    0 times in 0 posts

    Re: I am trying to write a batch file to ping IP addresses of PLC in field locations

    Spoon_,
    I cannot seem to get your code to work, now it is pinging the first IP, then jumps to label1 and starts over pinging the first address.

    DanceswithUnix,
    Unfortunately I am just a field technician, I lack the necessarry permissions to install or run unauthorized software on our PCN or Process Control Network servers. I am sure it is obvious that I know very little about programming or writing code, is Python or Ruby going to require installation on the machine running the script?

    Thank you for your feedback

  5. #5
    <<== UT3 Player spoon_'s Avatar
    Join Date
    Nov 2008
    Location
    London
    Posts
    2,071
    Thanks
    113
    Thanked
    139 times in 131 posts

    Re: I am trying to write a batch file to ping IP addresses of PLC in field locations

    Quote Originally Posted by senator1983 View Post
    Spoon_,
    I cannot seem to get your code to work, now it is pinging the first IP, then jumps to label1 and starts over pinging the first address.
    What happens if you only have 1 IP address in your text file?

  6. #6
    Chaos Monkey Apex's Avatar
    Join Date
    Jul 2003
    Location
    Huddersfield
    Posts
    4,528
    Thanks
    957
    Thanked
    233 times in 163 posts
    • Apex's system
      • Motherboard:
      • Asus Z87M-PLUS
      • CPU:
      • Intel i5-4670K
      • Memory:
      • 32 GiB
      • Storage:
      • 14 TiB
      • Graphics card(s):
      • R9 480X 8Gib
      • PSU:
      • 750
      • Case:
      • Core View 21
      • Operating System:
      • Windows 10 pro
      • Monitor(s):
      • Dell S2721DGFA
      • Internet:
      • 200Mb nTL Cable

    Re: I am trying to write a batch file to ping IP addresses of PLC in field locations

    Do you have a Unix/Linux box at work because this could be done very easy with a shell script.



  7. #7
    HEXUS.social member Agent's Avatar
    Join Date
    Jul 2003
    Location
    Internet
    Posts
    19,168
    Thanks
    735
    Thanked
    1,607 times in 1,045 posts

    Re: I am trying to write a batch file to ping IP addresses of PLC in field locations

    Quote Originally Posted by senator1983 View Post
    Unfortunately I am just a field technician, I lack the necessarry permissions to install or run unauthorized software on our PCN or Process Control Network servers. I am sure it is obvious that I know very little about programming or writing code, is Python or Ruby going to require installation on the machine running the script?
    Ahh, we've all been in the same boat at some point.

    You can get portable versions of Python: http://portablepython.com/

    It's really quite a nice language.
    Quote Originally Posted by Saracen View Post
    And by trying to force me to like small pants, they've alienated me.

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked
    0 times in 0 posts

    Re: I am trying to write a batch file to ping IP addresses of PLC in field locations

    Quote Originally Posted by spoon_ View Post
    What happens if you only have 1 IP address in your text file?
    the file seems to run as many times as there are addresses in my txt file.

  9. #9
    root Member DanceswithUnix's Avatar
    Join Date
    Jan 2006
    Location
    In the middle of a core dump
    Posts
    12,335
    Thanks
    714
    Thanked
    1,406 times in 1,188 posts
    • DanceswithUnix's system
      • Motherboard:
      • Asus X470-PRO
      • CPU:
      • 3700X
      • Memory:
      • 32GB 3200MHz ECC
      • Storage:
      • 1TB Linux, 1TB Games (Win 10)
      • Graphics card(s):
      • Asus Strix RX Vega 56
      • PSU:
      • 650W Corsair TX
      • Case:
      • Antec 300
      • Operating System:
      • Fedora 33 + Win 10 Pro 64 (yuk)
      • Monitor(s):
      • Benq XL2730Z 1440p + Iiyama 27" 1440p
      • Internet:
      • Zen 80Mb/20Mb VDSL

    Re: I am trying to write a batch file to ping IP addresses of PLC in field locations

    Quote Originally Posted by senator1983 View Post
    DanceswithUnix,
    Unfortunately I am just a field technician, I lack the necessarry permissions to install or run unauthorized software on our PCN or Process Control Network servers. I am sure it is obvious that I know very little about programming or writing code, is Python or Ruby going to require installation on the machine running the script?

    Thank you for your feedback
    I feel for you, yes many of us have been there.
    Sometimes some idiot even trots out the "A good workman never blames his tools" line. A hundred years ago if you wanted to learn something like carpentry then you did an apprenticeship and as part of that *you made your own tools*, so yes a good workman doesn't blame them partly because they are his and partly because he knows which one he should use. A bad workman blames the tools because, no that's not a hammer, that's a screwdriver.

    You are trying to hammer something in with a lump of lard. You might get there in the end, but really, ask what is acceptable to put on a machine, if there is any wiggle room at all.

    Here is the answer in Python. I tried it in Windows 7, it coped with a live host, an unreachable host and a timed out host. I left a trap in there for "Unknown" if the parsing fails to try and avoid false reports. The world is a nicer place if you don't smother it with goto statements

    Edit to add: I believe Python programs can be compiled into an ".exe" file, in theory that is no more dangerous than running your .cmd script on a machine as you compiled it. Perhaps you could run that instead of your script?

    My Python is really rusty, but I knocked this out in about half an hour including installing Python and finding a tutorial on how to run the "idle" environment for writing and debugging it.



    from subprocess import Popen, PIPE

    reportFile = open( "report.txt", "w" )
    for hostLine in open( "AWTPLCs.txt" ):
    splitLine = hostLine.strip().split( ":" )
    hostName = splitLine[0]
    hostIP = splitLine[1]
    print( "Testing "+hostIP )
    cmd = Popen("ping -n 1 "+hostIP, stdout=PIPE)
    for line in cmd.stdout:
    if "time=" in str( line ):
    hostState = "Alive"
    break
    elif "unreachable" in str( line ):
    hostState = "Down"
    break
    elif "timed out" in str( line ):
    hostState = "Down"
    break
    else:
    hostState = "Unknown"

    reportFile.write( hostName + " is " + hostState + "\n" )
    reportFile.close()
    Last edited by DanceswithUnix; 13-08-2013 at 09:05 AM.

  10. #10
    Chaos Monkey Apex's Avatar
    Join Date
    Jul 2003
    Location
    Huddersfield
    Posts
    4,528
    Thanks
    957
    Thanked
    233 times in 163 posts
    • Apex's system
      • Motherboard:
      • Asus Z87M-PLUS
      • CPU:
      • Intel i5-4670K
      • Memory:
      • 32 GiB
      • Storage:
      • 14 TiB
      • Graphics card(s):
      • R9 480X 8Gib
      • PSU:
      • 750
      • Case:
      • Core View 21
      • Operating System:
      • Windows 10 pro
      • Monitor(s):
      • Dell S2721DGFA
      • Internet:
      • 200Mb nTL Cable

    Re: I am trying to write a batch file to ping IP addresses of PLC in field locations

    Code:
    #!/bin/ksh
    #
    #Script Name   :<removed>
    #Written by     :Andrew Foster
    #Cron time      :06:30
    #Description    :
    #Creation date :21/02/2012
    #Amendments   :<removed>
    #
    #############################################################
    #
    #Set up variables
    #
    BASEDIR=/usr/local/scripts
    #
    #Logging directory
    LOGDIR=$BASEDIR/LOGS
    #
    IPLIST=`cat $BASEDIR/iplist.txt`
    #
    #
    #Begin Script
    cat $LOGDIR/iplog.txt > $LOGDIR/iplog.txt
    cat $LOGDIR/pingresults.txt > $LOGDIR/pingresults.txt
    #
    echo Start of IP Check $(date) >> $LOGDIR/pingresults.txt
    #
    for ip in $IPLIST
    do
    ping -c 2 $ip >>$LOGDIR/iplog.txt
    if [ $? -eq 0 ]
    then
    echo $ip "PINGS">>$LOGDIR/pingresults.txt
    else
    echo $ip "DOESN'T PING">>$LOGDIR/pingresults.txt
    fi
    done
    echo End of IP Check $(date) >> $LOGDIR/pingresults.txt
    done
    mail -s "subject goes in here" $(date)" email address address goes here < $LOGDIR/pingresults.txt
    Thats in korne shell on a unix box, if you need any pointers just ask (this was one of the first scripts i put together from bits and bats)
    Last edited by Apex; 14-08-2013 at 08:17 PM.



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •