Solved

Ping time stamping

Posted on 2013-12-17
10
313 Views
Last Modified: 2014-01-30
I have two pc's on the same network.
One of them I know the current time, the other is inaccessible by me.
I have found that you can ping another machine to get a timestamp from it.

So it would look like ...

PC1 time ping sent
PC2 time ping received
PC2 time return ping sent
PC1 time return recieved

I have the following code

Dim pingbyte(31) As Byte
        Dim timestamp As New Net.NetworkInformation.PingOptions
        PingReturn = New Net.NetworkInformation.Ping
        sReturn = PingReturn.Send("xxx.xxx.xxx.xxx")

        Dim properties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()
        Dim statistics As IcmpV4Statistics = properties.GetIcmpV4Statistics()
        MsgBox("Timestamp Requests ... Sent: " & statistics.TimestampRequestsSent.ToString & " Received: " & statistics.TimestampRequestsReceived.ToString & vbCr & "Timestamp Replies ... Sent: " & statistics.TimestampRepliesSent.ToString & " Received: " & statistics.TimestampRepliesReceived.ToString)

The message box always has 0 for sent and received ... What am I missing?
0
Comment
Question by:scprogs
  • 5
  • 5
10 Comments
 

Author Comment

by:scprogs
ID: 39724137
These are the Imports used

Imports System.Net.NetworkInformation
Imports System.Net.NetworkInformation.Ping
Imports System.Net.NetworkInformation.IcmpV4Statistics

These three lines are in the declarations section.

' Ping
    Dim PingReturn As System.Net.NetworkInformation.Ping
    Dim sReturn As System.Net.NetworkInformation.PingReply
    Dim TimeReturn As System.Net.NetworkInformation.IcmpV4Statistics
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39724164
See if your numbers match what you see in netstat -s
(run netstat -s from a command prompt and look in the ICMP4 stats section)
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39724254
I don't know if there is a user-friendly way to send ICMP Get Timestamp messages using VB.NET. If you really need to do this, you may need to build the packet manually, byte by byte.
0
 

Author Comment

by:scprogs
ID: 39724273
Yes they are the same.
How do you do a ping and get the data to post?
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 39724295
A regular ping request does not ask for a timestamp. You will most likely notice that on netstat -s and on your application, the echos and echo replies look right. That's what you sent.

To get the timestamp you need to send an ICMP timestamp message. To my knowledge (and my searching the web just now), there is no built in way to do that with VB. You would need to manually create the packet.
Here is the spec for the packet:
http://en.wikipedia.org/wiki/Internet_Control_Message_Protocol#Timestamp

There is probably an easier way to do this.
Perhaps the NetRemoteTOD function will get you what you want.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa370612%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa370612%28v=vs.85%29.aspx
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:scprogs
ID: 39724331
Yes I found the wiki page that is why I'm trying this in the first place.
Also found the C++ pages but I'm writing in vb.net.

According to a quick search from google a few days ago I was able to ascertain that one could achieve this with the new api's in .net 4.5

Can this be done via a batch file or cmd command?
0
 

Author Comment

by:scprogs
ID: 39724426
Convert C# to VB.NET web page does not covert the code you provided error on line 10

#include <stdio.h>
#include <windows.h>
#include <lm.h>
#pragma comment(lib, "netapi32.lib")

#ifndef UNICODE
#define UNICODE
#endif

int wmain(int argc, wchar_t *argv[])
{
   LPTIME_OF_DAY_INFO pBuf = NULL;
   NET_API_STATUS nStatus;
   LPTSTR pszServerName = NULL;

   if (argc > 2)
   {
      fwprintf(stderr, L"Usage: %s [\\\\ServerName]\n", argv[0]);
      exit(1);
   }
   // The server is not the default local computer.
   //
   if (argc == 2)
      pszServerName = (LPTSTR) argv[1];
   //
   // Call the NetRemoteTOD function.
   //
   nStatus = NetRemoteTOD((LPCWSTR) pszServerName,
                          (LPBYTE *)&pBuf);
   //
   // If the function succeeds, display the current date and time.
   //
   if (nStatus == NERR_Success)
   {
      if (pBuf != NULL)
      {
         fprintf(stderr, "\nThe current date is: %d/%d/%d\n",
                 pBuf->tod_month, pBuf->tod_day, pBuf->tod_year);
         fprintf(stderr, "The current time is: %d:%d:%d\n",
                 pBuf->tod_hours, pBuf->tod_mins, pBuf->tod_secs);
      }
   }
   //
   // Otherwise, display a system error.
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);
   //
   // Free the allocated buffer.
   //
   if (pBuf != NULL)
      NetApiBufferFree(pBuf);

   return 0;
}
0
 

Author Comment

by:scprogs
ID: 39724431
An error occured converting your code, probably due to a syntax error:
 -- line 10 col 1: EOF expected
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39724441
Keep in mind that some people consider it a security vulnerability to respond to that type of ping request so there are not many tools that work with it and you might have to change some configurations to get the remote machine to actually respond.
http://capec.mitre.org/data/definitions/295.html

hping and nping are both open source tools that will send ICMP timestamp requests. I don't think there is any built in way to do it from the command prompt.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39724448
Which line is line 10?

Just use main, and char instead of wmain and wchar_t....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Tool Box 2 35
vb.net - How to check if current user is an administrator? 6 34
Vb. Net application freezes 9 30
Need a starter for ETL protocol? 4 42
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now