Created
March 5, 2018 17:19
-
-
Save niraj-shah/e4c33d57312d9ea46251fb04dfe22b1c to your computer and use it in GitHub Desktop.
AWS Lambda IP Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// include the libraries we need | |
var request = require('request'); | |
var cheerio = require('cheerio'); | |
// set some defaults | |
req = request.defaults({ | |
jar: true, // save cookies to jar | |
rejectUnauthorized: false, | |
followAllRedirects: true // allow redirections | |
}); | |
exports.getIP = function( event, context ) { | |
// scrape the page | |
req.get({ | |
url: "http://www.whatsmyipaddress.net/", | |
headers: { | |
'User-Agent': 'Super Cool Browser' // optional headers | |
} | |
}, function(err, resp, body){ | |
var $ = cheerio.load(body); | |
// get the data and output to console | |
var data = {}; | |
data.ip = $('.inner_cntent:nth-child(1) span').text(); | |
data.host = $('.inner_cntent:nth-child(2) span').text(); | |
data.ua = $('.browser span').text(); | |
context.done( null, JSON.stringify( data ) ); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment