0

I would like to simulate different ping times for the same host (my local machine with Mac OS X). For example, I would like ping localhost and ping myhostname.mydomain.com return different ping times. Can I do it ?

Can I configure a few new host names (myhost1 localhost, myhost2 localhost) in /etc/hosts and simulate different ping times for ping myhost1 and ping myhost2 ?

Michael
  • 41,026
  • 70
  • 193
  • 341
  • A ping takes as long as it takes. What are you trying to do here? – deceze Jan 28 '14 at 16:12
  • I am trying to _simulate_ different network delays in my own machine for testing. – Michael Jan 28 '14 at 16:13
  • `ping` doesn't do simulations though, it sends an actual packet through the TCP stack to an IP address. That will take however long it takes. Hostnames won't change that. The hostname would need to resolve to an actually different IP address to take a different path through the network... – deceze Jan 28 '14 at 16:25
  • @deceze Ok. I am asking if I can add network delay as a function of the hostname. – Michael Jan 28 '14 at 16:28
  • Only if you replace parts of your network stack and/or configure some real or simulated network to introduce delays in packet routing. Hostnames cannot do this, no. – deceze Jan 28 '14 at 16:56
  • I think that _maybe_ I can configure network delays (I know I can do it in Mac) as a function of hostname using a network proxy for instance. – Michael Jan 28 '14 at 17:06

1 Answers1

0

You could make a wrapper around ping like this.

1) rename your real ping as ping.real

2) write a new ping and store it where original ping was, and have it do a wait of a few seconds before execing ping.real depending on the parameter (IP address/or hostname).

Ok, here is an alternative method if you don't really use the ping executable... how about using LD_PRELOAD to replace the sendto() and/or recvfrom() calls and add delays in there? See example for replacing malloc() here.

Community
  • 1
  • 1
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks. However the problem is not the ping command itself. I want to "cheat" an application, which uses `ping time` as part of its internal= algorithm. This application does not invoke the `ping` command. – Michael Jan 28 '14 at 16:30
  • 1
    You could try this, which recommends using ipfw... http://www.quora.com/What-is-the-best-tool-to-simulate-a-slow-internet-connection-on-a-Mac – Mark Setchell Jan 28 '14 at 17:11
  • Thank you but I do not want to write a library replacement for the ping. – Michael Jan 29 '14 at 16:10