Ping Test?

811 views
Skip to first unread message

Lee Tarry

unread,
May 25, 2017, 3:12:03 AM5/25/17
to DroidScript
A colleague has asked if i can make a Ping Test for his mobile device,

does anyone know where to start?

i need to 'ping' and i.p. address every second or so, to test his connection so he can report any faults to his
internet supplier.

I don't know what information i need to send/receive to (possibly google.com),

it sounds like it would be a fun project and i'm going to start with the (Location sample).

if anyone can steer me in the right direction i would be very grateful,

Thanks,

Lee

Dave Smart

unread,
May 25, 2017, 6:22:51 AM5/25/17
to DroidScript
Hi Lee,

The app.SysExec for app.CreateSysProc methods would probably be a good place to start.

(Since the ping command is a long running command by default it might lock up your app if you use SysExec, unless you pipe the result to a local file instead and/or use option switches to limit the number of pings)

If you are a premium user then check out the "Terminal" example for how to read the responses from long running linux commands.


Calling something like this with app.SysExec and then parsing the results with regex should do it:-

ping -c 1 www.google.com

Regards
David

Lee Tarry

unread,
May 25, 2017, 6:41:54 AM5/25/17
to DroidScript
Thanks Dave,

I'm not a premium user yet as i'm only a hobbyist, 
I might have a look into it, and try the terminal example.

for now ill look into the  app.SysExec & app.CreateSysProc Methods,
Ive got to be honest, i think this maybe a bit above my skill set. haha

thanks again!

Lee Tarry

unread,
May 25, 2017, 8:56:47 AM5/25/17
to DroidScript
Dave, Is this anyware near?


//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
  lay.SetBackColor("Red");

//Create a text control to show data log.
txt = app.CreateText( "...", 0.9, 0.9, "Multiline,Left" );
txt.SetTextColor( "White" );
lay.AddChild( txt );
//Create a text label and add it to layout.
var shell = app.CreateSysProc( "sh" );
  var cmd = "ping -c3 8.8.8.8 > /sdcard/ping.txt";
  shell.Out( cmd + "\n" );

txt.SetText( shell );

  app.AddLayout( lay );
  }

for the moment i just want to get it to show on screen,
id like the text file to send aftrerwards.
i have used the text label from another feed in the forum.

Dave Smart

unread,
May 25, 2017, 9:40:38 AM5/25/17
to DroidScript
I was thinking of something more like this:-


//Called when application is started.
function OnStart()
{
   
//Create a layout with objects vertically centered.
    lay
= app.CreateLayout( "linear", "VCenter,FillXY" );

    lay
.SetBackColor( "#333333" );
   
   
//Create an text edit box for entering commands.
    edt
= app.CreateTextEdit( "www.google.com", 0.8, -1, "SingleLine" );
    edt
.SetMargins( 0,8,0,8, "dip" );
    edt
.SetHint( "address/ip" );
    lay
.AddChild( edt );
   
   
//Create a scroller for log window.
    scroll
= app.CreateScroller( 0.95, 0.7 );
    scroll
.SetBackColor( "black" );
    lay
.AddChild( scroll );
   
     
//Create text control for logging (max 500 lines).
 txt
= app.CreateText( "", 2,-1, "Log,Monospace" );
 txt
.SetBackColor( "black" );
 txt
.SetLog( 500 );
 scroll
.AddChild( txt );
   
   
//Create a button
 btn
= app.CreateButton( "Ping", 0.3, 0.1 );
 btn
.SetMargins( 0,8,0,8, "dip" );
 btn
.SetOnTouch( btn_OnTouch );
 lay
.AddChild( btn );
 
    app
.AddLayout( lay );
}




//Called when user touches our button.
function btn_OnTouch()
{
 
var s = app.SysExec( "ping -c 1 " + edt.GetText() );
 txt
.Log( s );
}



Lee Tarry

unread,
May 25, 2017, 3:52:38 PM5/25/17
to DroidScript
wow, thanks dave, that works great.

the cmd "ping -c 1" obviously get all of the information, is there a way of just getting part of that?

i have set it to ping once per second,
and i only really need a yes/no.

so i can add them up and show uptime + downtime.

Steve Garman

unread,
May 25, 2017, 5:04:33 PM5/25/17
to DroidScript
function btn_OnTouch()
{
var s = app.SysExec( "ping -c 1 " + edt.GetText() );
if(s.indexOf("1 received")== -1) s = "no";
else s = "yes";
txt.Log( s );
}

Lee Tarry

unread,
May 25, 2017, 5:15:35 PM5/25/17
to DroidScript
Awesome, thanks steve.

I will post on here when finnished, i am told it is a useful tool for I.T. folk...

cheers again,

Lee

Lee Tarry

unread,
May 29, 2017, 5:05:23 AM5/29/17
to DroidScript
is there a way of making the scroller move down so the last text is always visable?

Steve Garman

unread,
May 29, 2017, 6:22:28 AM5/29/17
to DroidScript
var biggest = Number.MAX_SAFE_INTEGER

//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );

lay.SetBackColor( "#333333" );

//Create an text edit box for entering commands.
edt = app.CreateTextEdit( "www.google.com", 0.8, -1, "SingleLine" );
edt.SetMargins( 0,8,0,8, "dip" );
edt.SetHint( "address/ip" );
lay.AddChild( edt );

//Create a scroller for log window.
scroll = app.CreateScroller( 0.95, 0.7 );
scroll.SetBackColor( "black" );
lay.AddChild( scroll );

//Create text control for logging (max 500 lines).
txt = app.CreateText( "", 2,-1, "Log,Monospace" );
txt.SetBackColor( "black" );
txt.SetLog( 500 );
scroll.AddChild( txt );


app.AddLayout( lay );
setInterval(btn_OnTouch,1000);
}

scroll.ScrollTo( 0, biggest);
}

Lee Tarry

unread,
May 29, 2017, 7:00:42 AM5/29/17
to DroidScript
Works great, thanks steve!

Lee Tarry

unread,
May 30, 2017, 2:22:36 PM5/30/17
to DroidScript
i have tried to turn this into an apk, and its only showing no connection, is their anything i need to allow or anything like that to do this?

Steve Garman

unread,
May 30, 2017, 2:28:10 PM5/30/17
to DroidScript
DroidScript cannot guess what app.SysExec is doing so it doesn't know what permissions you need.

Try adding this at the top of your code.

_AddPermissions("Network");

Reply all
Reply to author
Forward
0 new messages