0

I'm searching for a method to ping local devices on my network, like if I use de ping command in my terminal, to ping my router or any other devices connected. I already found way to ping server like google.com but it seems it's not working on local address. Is that possible or not ? Here is my typescript code:


private source = interval(3000);

  constructor(private _http: HttpClient) { }

...


getStatutDevice(ip: string){

    //IP Value look like "192.168.1.20" for example

    this.source.subscribe(() => {
      this._http.get(ip, { observe: 'response' })
        .pipe(first())
        .subscribe(resp => {
          if (resp.status === 200 ) {
            console.log(true);
          } else {
            console.log(false);
          }
        }, err => console.log(err));
    });
}

I got this error in the console:

zone-evergreen.js:2863 GET http://localhost:4200/192.168.1.20 404 (Not Found)

If I put "https://" before the ip address, I got:

GET https://192.168.1.20/ net::ERR_CONNECTION_REFUSED

I don't know what this means to find a solution.

Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59

2 Answers2

0

There is this typescript utility. It gives the ability to send a tcp ping.

Ad Fortia
  • 333
  • 1
  • 3
  • 12
  • Thank you for your quick answer ! I'll check that ! – Matthieu Barbe Mar 18 '21 at 10:38
  • It seems that this typescript utility only work on node.js, but not in browser. I saw that it was impossible to ping local address like that from a browser, for securities reason. i don't knwo if it's true, if there is alternative. I wanted to ping my local devices from my angular application – Matthieu Barbe Mar 19 '21 at 00:15
  • You can create a serverside service and call it with a http request from your angular application. Probably, this is simpler thant trying to ping directly from browser. – Ad Fortia Mar 19 '21 at 08:14
0

You cannot do an ICMP Ping ("like in the terminal") in a browser. Any attempt to do it will either be tcp or http get requests.

JerMah
  • 693
  • 5
  • 17
  • Thank you for your quick anwser! Http get can ping like that ? – Matthieu Barbe Mar 18 '21 at 10:39
  • You can do http GETs, but if there's no webserver at the other end you won't get a reponse. A router has a webserver on the LAN side usually so that should work. But a lot of other devices such as phones or desktops, usually won't have a webserver running, so you will always get a negative response. – JerMah Mar 18 '21 at 10:53