Ping list of ip addresses using Javascript - javascript

I have a list of ip addresses to ping and would like to use Java script to do that.
I tried as the following but there is no output when I clicked the "Test" button.
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script>
function myFunction()
{
var myStringArray = [ "10.100.200.133", "10.10.22.0" ];
len=myStringArray.length
for (var i=0; len=myStringArray.length; i<len; i++) {
check=ping(i)
if $check Then
document.write("ip"+ i + "is up")
else
document.write("ip"+ i + "is down")
}
}
</script>
</head>
<body>
<button onclick="myFunction()">Test</button>
</body>
</html>

You can't directly "ping" in JavaScript. There may be a few other ways:
Ajax
Using a Java applet with isReachable
Writing a server-side script which pings, and using AJAX to communicate to your server-side script
You might also be able to ping in Flash (using ActionScript)
There are also some methods for pinging in JavaScript described in this question: Is it possible to ping a server from Javascript?

ping has a very specific meaning: https://www.rfc-editor.org/rfc/rfc1122#section-3.2.2.6, which cannot be replicated using a browser API.
If you want to just test if a web page hosted at a particular IP is available, you can use XMLHttpRequest

Related

"ScriptError: Authorisation is required to perform that action." when running google.script.run from Library

Regards,
I have found several questions regarding this error :
"ScriptError: Authorisation is required to perform that action." but I can't find one that is about my issue.
What I am trying to do is call a function .gs file from .html file using google.script.run where both of the file is in Library. Referring to this answer, this answer and this bug report, I have created the "wrapper function" in the script that is using the library, but still failed to finish the execution.
Here's what I did :
.html in Library :
<html>
<head>
<script>
function onFailure(error) {
console.log("ERROR: " + error);
}
function myfunc() {
console.log("HERE");
google.script.run.withFailureHandler(onFailure).callLibraryFunction('LibraryName.test', ['test123']);
}
</script>
</head>
<body>
<button onclick="myfunc()">CLICK</button>
</body>
</html>
.gs in Library
function test(x){
Logger.log(x);
}
.gs in script that is using the Library:
function callLibraryFunction(func, args) {
var arr = func.split(".");
var libName = arr[0];
var libFunc = arr[1];
args = args || [];
return this[libName][libFunc].apply(this, args);
}
The console logs HERE but then it logs ERROR: ScriptError: Authorisation is required to perform that action. instead of the expected output, test123.
NOTE: The HTML is for custom modeless dialog box in Sheets and not for Web App.
I really hope someone can help me in this. Thank you in advance.
You need to grant access of the library "LibraryName" as an authorized app in your Gmail account the same way how you grant access to the calling script. I guess you have called the method HtmlService.createHtmlOutputFromFile(...) in your library. This requires more authorization. You need to grant this. What you can do is create a temporary function in the library that has HtmlService.createHtmlOutputFromFile(..). Run it from the script editor and the authorization requirement window appears. Proceed in granting access...

How to run an EXE file through URL on .Net shared hosting

I try to run a simple ETL process on a schedule to populate a SQL Server database table on a .Net shared hosting. The EXE file will be hosted with the website and when it runs it will make some API calls and get data to update the website's SQL table.
My hosting company allows such thing (to call an exe file on schedule) with an extra fee, but they require me to have it wrapped and be called using URL. They don't mind any technology to use as long as I provide a URL. I did few attempt to get this setup working with no luck. For example I tried two ways below: Note, I just started to learn JavaScript, I use C# but this is my first time attempt to do something like that and I might be completely off. Any help will be appreciated.
<html>
<head>
<title>Open PMETL</title>
<script type="text/javascript">
function runProgram()
{
try {
var shell = new ActiveXObject("WScript.Shell");
var myPMETL="http://trudat.live/RefreshData.exe";
shell.Run(myPMETL);
}
catch (e) {
alert(e.message);
}
}
function runProgram02() {
if (window.ActiveXObject) {
try {
var excelApp = new ActiveXObject ("Excel.Application");
excelApp.Visible = true;
}
catch (e) {
alert (e.message);
}
}
else {
alert ("Your browser does not support this example.");
}
}
</script>
</head>
<body>
Run program
Run program02
</body>
</html>
I was able to achieve that by adding a new page to my ASP.NET application and port the console application into the code-behind C# and invoke the code through the Page_Load() method. This allowed my to give the hosting company a URL like http://mydomain/ExePage.aspx without impacting my original application since this new page is not reachable from the application's menu. it was a convenient way to give me what I needed.
I assume this is not a typical solution since I had the advantage to have the source code of the EXE program, but nevertheless it is a very effective, and this might help someone in the future.

Web Worker is not working

I have written a very basic web worker, but is not working. Any help would be appreciated. Thanks.
Here's my HTML code:
<!DOCTYPE html>
<html>
<head>
<title>Basic Demo of Web Workers</title>
</head>
<body>
<button type="button" onclick="start()">Start!</button>
<button type="button" onclick="stop()">Stop!</button>
<output id="counterShow"></output>
</body>
<script>
var myWorker;
function start() {
if(window.Worker) {
myWorker = new Worker("http://yourjavascript.com/8257018521/basic-demo.js");
myWorker.onmessage = function(event) {
document.getElementById('counterShow').innerHTML = event.data;
};
myWorker.onerror = function(event) {
alert(event.message, event);
}
} else {
document.getElementsByTagName('BODY')[0].innerHTML = 'Sorry! Web workers are not supported.';
}
}
function stop() {
myWorker.terminate();
}
</script>
Here's the JS file that is hosted on a CDN (yourjavascript.com)
for(var i=0; i<100000; i++) {
postMessage(i);
}
The web worker is silently failing. Please help.
Uncaught DOMException: Failed to construct 'Worker': Script at 'http://yourjavascript.com/8257018521/basic-demo.js' cannot be accessed from origin 'null'
I believe this is due to security reason.
More info: Cross Domain Web Workers
100% a CORS issue. You cannot load web workers in the same way as normal scripts in <script> tags. Only if remote server allows alien host origins to load their files via ajax. Not sure why, but that's the way it is. See this workaround:
https://stackoverflow.com/a/33432215/607407
Well, I fixed it myself. I started a new project on CodePen and ran the code there, and it worked out pretty sweetly. I think the key requirement for Web Workers to run is to run them online, not locally.

Cookies not working when page accessed via file://

My code works in firefox and when i visit w3schools using chrome to test my code in their editor it works fine too but when i launch my code in chrome from notepad++ it doesn't work.It seems that body onload is not working because i don't get the alert.My chrome is up to date.Help would be appreciated.
<!DOCTYPE html>
<html>
<head>
<script>
function setCookie(cname,cvalue,exdays){
var d=new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires="expires="+d.toUTCString();
document.cookie=cname +"="+cvalue+"; "+expires;
}
function f(){
var user=prompt("What is your name?","");
if(user!="" && user!=null){
setCookie("username",user,30);}
}
function getC(cname){
var name=cname+"=";
var ca=document.cookie.split(";");
for(var i=0;i<ca.length;i++){
var c=ca[i];
while(c.charAt(0)==" ")c=c.substring(1);
if(c.indexOf(name)==0) return c.substring(name.length,c.length);
}
return "";
}
function checkcooki(){
var user=getC("username");
if(user!=""){
alert("Welcome back "+user);
}
}
</script>
</head>
<body onLoad="checkcooki()">
<input type="button" onclick="f()" value="klick">
</body>
</html>
For a fact: Using the file:// protocol does NOT guarantee the proper workings with cookies. Since cookies need 3 things:
A name-value pair containing the actual data
An expiry date after which it is no longer valid
The domain and path of the server it should be sent to
The domain tells the browser to which domain the cookie should be sent. If you don't specify it, it becomes the domain of the page that sets the cookie.
On a file:// protocol you don't have a domain.
Now some browsers might have found work-arounds for this, like FireFox and IE. You can test your code on these browsers but they will not use cookies in the same way as on a webserver.
Proper x-browser testing in your case requires the http:// protocol.
I suggest you start a jsfiddle or setup a webserver(IIS, apache).
Proper read on cookies: quircksmode
If you are still persistent to get it working on chrome through the file:// protocol you might have a small chance if you get the path correctly.
path: properly escaped path => encodeURIComponent(document.domain) or "c:\/my%20folder\/index.html" (along these lines but again, very untrustworthy information here)
domain: "/" (no idea what else you can try here)
Your user variable must be a blank string.
Put an alert at the very top of your checkcooki() function to verify that body onload works.

jQuery ajax get response not parsed or not seen

The problem is that jQuery does not receive the response by the server, and I can't figure out why. This is my setup, Windows 7 64bit:
ext.js:
$('#button').click(function(){
var string= $('#string').val();
$.get('http://localhost:3000',
{"input":string},
function(data){
alert(data);
$('#feedback').text(data);
});
})
099.html:
<doctype html>
<html lang="en">
<head> <!-- charset title style -->
<meta charset="uft-8"/>
<title>jQuery 099</title>
</head>
<body><!-- tables, div's bad, html 5 is better: -->
<input type="text" id="string" value=""/>
<input type="button" id="button" value="ajax"/>
<br/>
<div id="feedback"></div>
</body>
<script type="text/javascript" src="./js/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="./js/ext.js"></script>
</html>
server.js:
var express = require('express');
var app = express();
app.get('/', function(req, res){
console.log("got");
console.log(req.query.input);
var content = req.query.input;
res.send(content);
});
app.listen(3000);
I run node 0.10 from command line using
C:\dev\nodejs\0.10\servers\stackoverflow>node server.js
In my FF browser i type
http://localhost:3000/?input=hi
and i get a blank screen containing hi,
which is good. Also node.js prints got and then hi on the command line
I run 099.html from notepad++ > run > chrome > so it runs on a completely other drive but surely it doesn't need to be in a server, right? When i type something XYZ the textfield and click ajax button, node responds on the console XYZ, which is good: the request is discovered by node, so it would send a response, but i don't see the response in my html.
The expected behavior was an alert and my div gets filled in the html and displays XYZ.
What obvious point am i missing?
I'm stuck for 2 hours now and couldnt find a similar question perhaps because of my not knowing jquery.
ps the 099 is from the newboston youtube tutorial and jquery is from the jquery site. i don't know the express version, it's a fairly new one.
ps2: the jquery $.get() api is too vague:http://api.jquery.com/jquery.get/ states: "A callback function that is executed if the request succeeds." well, can i conclude that the request succeeded because the nodejs console reacted to it, and if so, why did the callback function not execute.
ps3: the last argument is dataType, perhaps node responds in a way $.get did not expect? any datatype suggestions?
EDIT: yesterday i dusted off my tomcat and put the above files into it and jquery runs like a charm.
How stupid of me, assuming that a file on a disk can communicate over http to a server, what was i thinking.
the essence of ajax is "listening for the asynchronous response" (for example http://msdn.microsoft.com/en-us/magazine/cc163479.aspx), so the file needs to reside in something that establishes an IP address of some kind obviously. Sorry for polluting the Internet, case closed.
You cannot alter port number when issuing ajax request - this is Same origin policy restriction. See what you can do with SocketIO instead.

Categories

Resources