locked
Capture IP In Batch File RRS feed

  • Question

  • I created a logon/logoff script and assigned it to a GPO.  It captures the date, time, username, and computer name.  I would like to capture the clients IP address and/or the client name.  Is this possible?
    Friday, October 9, 2009 11:57 PM

Answers

  • Hi,

    Are the the users logging on to terminal server and you would like to get the client IP address? If so, there is no simple way to retrieve the information. To retrieve it, you may use the WTSInfoClass, which includes WTSClientAddress, of the WTSQuerySessionInformation Function. For more information, please refer to the following MSDN link:

    WTSQuerySessionInformation Function
    http://msdn.microsoft.com/en-us/library/aa383838(VS.85).aspx

    The MSDN forum is the best place for this kind of problem, in order to solve this issue in a timely manner, it’s suggested to submit a new post in the MSDN forum to get more suggestions.

    MSDN Forum
    http://forums.microsoft.com/MSDN/default.aspx?SiteID=1

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Mervyn Zhang Monday, October 19, 2009 11:04 AM
    Friday, October 16, 2009 10:18 AM

All replies

  • Hi

    Add the below line to your batch file...

    IPCONFIG |FIND "IP" > %temp%\TEMPIP.txt

    in place of %temp% config the path where you need to store the IP address..

    Please let us know incase of further information required.

    Regards
    Rajesh J S
    Saturday, October 10, 2009 1:44 AM
  • Thanks for your reply.  I am not sure exactly how to write this out.  Below is my logon script:

    echo Log In %Date% %TIME% %USERNAME% %@IPADDRESS% >> \\fileserver\e$\Files\User\Computer\%COMPUTERNAME%.log
    echo Log In %Date% %TIME% %@IPADDRESS% %COMPUTERNAME% >> \\fileserver\e$\Files\User\%USERNAME%.log

    It creates a text file, i.e., username.log and writes the following information:

    Log In Wed 10/07/2009 18:03:52.91 PCTS02 

    I want to write the IP address to the above line.  However, I am confused as to where to put the tag you described above.  I tried different combinations, but it is not working.  Could you please advise.

    Thanks for your help.

    Saturday, October 10, 2009 2:19 AM
  • Many networks run some variation of this.  I would recommed that you not use a batch file, but replace it with a VB script.  You will have alot more flexibility with the code  In addition, you would probably be better writing the information to a database.  Your DB solution doesnt have to be fancy, just use SQL Express.  There are alot of VB samples on the net.


    Visit my blog: http://www.anITKB.com

    Saturday, October 10, 2009 3:28 PM
  • Something like this (example):

    @echo off & cls
    
    Set "logFile=%Computername%.log"  provide full networkpath to logfile!
    
    ::# Get IP Address
    for /f "skip=1 tokens=2 delims=[]" %%* in (
       'ping.exe -n 1 %Computername%') Do (set "IP=%%*" & goto:exitFor1)
    :exitFor1
    
    ::# Get Full Name of User
    If /i "%USERDOMAIN%"=="%Computername%" (
       for /f "skip=1 tokens=2,* delims= " %%a in (
         'net.exe user "%Username%"') do (
           (set "DisplayName=%%b" & goto:exitFor2))
     ) Else (
       for /f "skip=3 tokens=2,* delims= " %%a in (
         'net.exe user "%Username%" /domain') do (
           (set "DisplayName=%%b" & goto:exitFor2))
     )
    :exitFor2
       If defined DisplayName Set "DisplayName=%DisplayName:)=^)%"
    
    ::# Append to log
    >>"%logFile%" (
       echo\*** Logon
       echo\User       : %USERDOMAIN%\%Username%  (%DisplayName%^)
       echo\Date/Time  : %date% - %time%
       echo\To computer: %IP% (%Computername%^)
    echo\-----------------------------------------------------------------)
    
    

    Certifications: MCSA 2003 MCSE 2003
    • Proposed as answer by Hannes Schurig Wednesday, July 20, 2011 2:01 PM
    Monday, October 12, 2009 2:13 AM
  • Hi,

    There is no environment variable "%@IPADDRESS%". You just need add the line ipconfig | find "IPv4 Address" to your script:

    echo Log In %Date% %TIME% %USERNAME% >> \\fileserver\e$\Files\User\Computer\%COMPUTERNAME%.log
    ipconfig | find "IPv4 Address" >> \\fileserver\e$\Files\User\Computer\%COMPUTERNAME%.log

    Thanks.

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, October 12, 2009 2:48 AM
  • Thanks for your reply.  This tag worked, however, it writes the IP address of the server the user is connecting into.  I need to write the IP address the user is connecting from.  Is that possible?
    Monday, October 12, 2009 3:28 PM
  • I appreciate your script.  I tried this one, however, it did not write any IP information.  This was the result:

    *** Logon
    User       : USERDOMAIN\username  (last, first)
    Date/Time  : Mon 10/12/2009 -  9:18:46.26
    To computer: ::1 (PCTS02)

    I am looking to write the IP address the user is connecting from not the IP address the user is connecting into.  Do you have any suggestions on how to write the IP address the user is connecting in from? 

    Monday, October 12, 2009 4:26 PM
  • Hi,

    Are the the users logging on to terminal server and you would like to get the client IP address? If so, there is no simple way to retrieve the information. To retrieve it, you may use the WTSInfoClass, which includes WTSClientAddress, of the WTSQuerySessionInformation Function. For more information, please refer to the following MSDN link:

    WTSQuerySessionInformation Function
    http://msdn.microsoft.com/en-us/library/aa383838(VS.85).aspx

    The MSDN forum is the best place for this kind of problem, in order to solve this issue in a timely manner, it’s suggested to submit a new post in the MSDN forum to get more suggestions.

    MSDN Forum
    http://forums.microsoft.com/MSDN/default.aspx?SiteID=1

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Mervyn Zhang Monday, October 19, 2009 11:04 AM
    Friday, October 16, 2009 10:18 AM
  • Hi,

    Do you need any other assistance? If there is anything we can do for you, please let us know.

    Thanks.

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, October 19, 2009 11:04 AM
  • Thank you.  I need to figure out how to write the client IP address  So I will be posting in the MSDN forum. 

    Thanks again for all your help.
    Monday, October 19, 2009 2:06 PM
  • Hi,

    Thank you for update. If you have more questions in the future, you’re welcomed to this forum.

    Thanks.

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, October 20, 2009 1:42 AM