xk6-icmp
    Preparing search index...

    Function pingAsync

    • Sends ICMP echo requests (pings) to the specified target.

      Parameters

      • target: string

        Hostname or IP address to ping.

      • OptionaloptsOrCallback: PingOptions | PingCallback

        Optional ping options or callback function.

      • Optionalcallback: PingCallback

        Optional callback function for ping results.

      Returns Promise<boolean>

      Promise that resolves to true if the number of successful pings is greater than or equal to the threshold, otherwise false.


      import { pingAsync } from "k6/x/icmp"

      export default async function () {
      const host = "8.8.8.8"

      console.log(`Pinging ${host} with callback:`);

      const opts = {
      timeout: 1000,
      retries: 2,
      count: 5
      };

      const result = await pingAsync(host, opts, (err, { target, sent_at, received_at, seq, ttl, size, options }) => {
      if (err) {
      console.error(`${target}: ${err}`);

      return
      }

      const rtt = received_at - sent_at;

      console.log(`${size} bytes from ${target}: icmp_seq=${seq} ttl=${ttl} time=${rtt} ms`);
      });

      if (result) {
      console.log(`Host ${host} is reachable`);
      } else {
      console.error(`Host ${host} is unreachable`);
      }
      }