PublisherName
PublisherName

Reputation: 131

How can I ping a specific IP using Google App Script?

I want to ping a specific ip address using GAS. I tried using UrlFetchApp.fetch() but it's says unable to fetch url as i don;t have any html content hosted on that specific ip. So, how can i check the pings to decide wither the ip is live or down ?

Upvotes: 0

Views: 6045

Answers (1)

Douglas Gaskell
Douglas Gaskell

Reputation: 10030

You cannot ping an IP with Apps Script via ICMP. However, there are a few things you can try with URLFetchApp

Instead of looking for returned HTML content, look at the response code.

var response = UrlFetchApp.fetch(yourIP, { 
  muteHttpExceptions: true,
  validateHttpsCertificates: false,
  followRedirects: true 
});

var siteStatus = response.getResponseCode(); 

If you get a response code that is not 0, there is a server there listening (afaik, correct me if wrong). An example: using 104.16.34.249 (Stackoverflow) will return 403, which is a forbidden status code. There is a site there and you tried to access content you are not authorized to access. If you use 192.0.78.9 (wordpress.com) you recieve a status code of 200 which means the request has succeeded.

List of status codes: http://www.restapitutorial.com/httpstatuscodes.html

Upvotes: 3

Related Questions