+ Post New Thread
Results 1 to 8 of 8
Scripts Thread, How do I put Nslookup exit status into a variable! DelProf2.exe script in Coding and Web Development; I have a problem. I have wrote a script that gets an IP address from Nslookup and passes it to ...
  1. #1

    Join Date
    Oct 2012
    Location
    Leicester
    Posts
    5
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Exclamation How do I put Nslookup exit status into a variable! DelProf2.exe script

    I have a problem. I have wrote a script that gets an IP address from Nslookup and passes it to DelProf2.exe.
    However, I obviously need some error handling in the script so I can influence the flow of the batch file as it is run (if something goes wrong GOTO function name).
    I'd like to control the flow upon Nslookup succeeding or failing.

    How do I access the exit status and assign it to a variable?

    Thanks in advance. This is my first post. This has been doing my head in!

    Note: I will post up finished bat file upon completion so you can all benefit if needed.
    Last edited by psutton00; 9th October 2012 at 02:26 PM.

  2. #2

    jinnantonnixx's Avatar
    Join Date
    Mar 2011
    Location
    In the Calamatorium.
    Posts
    1,985
    Thank Post
    114
    Thanked 505 Times in 340 Posts
    Blog Entries
    2
    Rep Power
    286
    How do I access the exit status and assign it to a variable?
    Microsoft chose not to include something as useful as an exit code for NSLOOKUP. What a disgrace.

    Try this. It's very nasty code that uses a temp file but this code should do the trick nonetheless.

    Code:
    @echo off
    
    nslookup DOMAIN_TO_TEST 2> %temp%\temp.txt
    
    
    for /f "tokens=3 delims= "  %%a in ('find /c /i "Non-existent domain" %temp%\temp.txt') do (
    	if %%a NEQ 0 (set Found=false) else (set Found=true)
    )
    
    
    if %found% == true (
    	echo The domain exists
    ) else (
    	echo The domain does not exist
    )
    Last edited by jinnantonnixx; 9th October 2012 at 03:00 PM.

  3. #3

    Join Date
    Oct 2012
    Location
    Leicester
    Posts
    5
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Typical Microshite.

    Thanks for the reply. I have been waiting all day!

    Is there any way to use the FIND command to search the output in CMD rather than use a temp file?

  4. #4

    Join Date
    Oct 2012
    Location
    Leicester
    Posts
    5
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    I have tried piping with no avail.

  5. #5

    jinnantonnixx's Avatar
    Join Date
    Mar 2011
    Location
    In the Calamatorium.
    Posts
    1,985
    Thank Post
    114
    Thanked 505 Times in 340 Posts
    Blog Entries
    2
    Rep Power
    286
    FIND isn't too clever, either, you'll be shocked to learn.

    The trouble is that an error from NSLOOKUP is sent through a different stream which isn't handled correctly by piping to FIND. It just doesn't work, which is why I had to use a file. I chose to pipe the STDERR output (which is the "2>" bit) to a file then run FIND against that looking for a specific message.

    Unless another 'geeker with a 3rd Dan in script-fu knows of a better way?

    SS64 is an tremendous site for all your scripty requisites.
    Command Redirection, Pipes | SS64.com
    Last edited by jinnantonnixx; 9th October 2012 at 03:29 PM.

  6. #6

    Join Date
    Oct 2012
    Location
    Leicester
    Posts
    5
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Basically we need some legal legislation to be implemented to hold programmers responsible for there code. IE. if they don't put a god damn exit status in there code they are shot!
    I am a student on placement and it was one the first I learnt at A-Level! Never write a program that doesn't tell other programs it has ran successfully or failed!

  7. #7

    Join Date
    Oct 2012
    Location
    Leicester
    Posts
    5
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    I will give it a bash, no pun intended, and let you know. It may take a while as I am having to change a fair amount of my code to adjust. Thanks for your support.
    Last edited by psutton00; 9th October 2012 at 03:47 PM. Reason: Grammatical

  8. #8

    jinnantonnixx's Avatar
    Join Date
    Mar 2011
    Location
    In the Calamatorium.
    Posts
    1,985
    Thank Post
    114
    Thanked 505 Times in 340 Posts
    Blog Entries
    2
    Rep Power
    286
    Quote Originally Posted by psutton00 View Post
    I will give it a bash, no pun intended, and let you know. It may take a while as I am having to change a fair amount of my code to adjust. Thanks for your support.
    Just put the NSLOOKUP check routine it in another file and use a 'call' to treat it as a function. If you use a SET command to set the variables, then it's a way of getting values visible between script routines. Messy, but in this case DOS batch scripting is very primitive. I'd probably try it in Powershell. That said, I find Powershell frustrating as it's an object-oriented scripting langauge, but the syntax is verb-based, not object-based! Gah!

    DOS Batch - Function Tutorial
    Last edited by jinnantonnixx; 9th October 2012 at 04:01 PM.

SHARE:
+ Post New Thread

Similar Threads

  1. How do you redirect a users desktop into the their home folder?
    By morganw in forum How do you do....it?
    Replies: 10
    Last Post: 13th June 2013, 06:34 PM
  2. [Android] How do I put Andrioid on my HTC Touch HD Windows naffbox?
    By oalcock in forum Netbooks, PDA and Phones
    Replies: 1
    Last Post: 24th August 2010, 05:25 PM
  3. How much extra time do you put in out of hours?
    By beeswax in forum General Chat
    Replies: 12
    Last Post: 10th January 2006, 09:38 AM

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

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