Take the 2-minute tour ×
Android Enthusiasts Stack Exchange is a question and answer site for enthusiasts and power users of the Android operating system. It's 100% free, no registration required.

I would like my phone to execute certain shell scripts via Tasker if my phone is on the same wifi network as my laptop. My initial idea is to edit the /etc/hosts file, but this wouldn't work since neither device is constrained to be on only one wifi network.

A much more costly idea would be to just ping every possible address on the network from my phone, then attempt to send a command via ssh (say, echo $ANDROID) and listen for the response.

The best way to do this is to just be able to see if a device named "myLaptop" exists on the network.. (or, actually, I could use MAC address too.) but I have no idea if this is possible in bash. Is it?

(I'm also open to implementations in Perl, since I can use SL4A to implement Perl scripts.)

share|improve this question

migrated from stackoverflow.com Aug 4 '11 at 13:22

This question came from our site for professional and enthusiast programmers.

    
Is doing the host lookup on the console required? –  Dan Aug 4 '11 at 0:00
    
not sure what you mean there. if by console you mean terminal, then yes - the idea is on my phone, Tasker can run a shell (or Perl, or Python, or some other language supported by SL4A) script that queries if my laptop is on the same network; if it is, execute particular tasks within the Tasker environment. –  strictlyrude27 Aug 4 '11 at 0:07
    
Do you want this to work across multiple networks? That is, not just on your home network, but also if both laptop & phone are connected to any random network? –  David Yaw Aug 4 '11 at 0:27
    
Yes. It's a pretty simple matter if it was on one network, I think..? –  strictlyrude27 Aug 4 '11 at 0:30
add comment

4 Answers 4

up vote 2 down vote accepted

Dynamic DNS service would work. Get your laptop to update your dynamic domain with it's local ip, and then whenever your phone connects to a new wifi network, resolve that domain, if you get a hit, you're either good or happened to find someone with the same local ip!

Similar to that, you could get your laptop to post it's ip to a server somewhere (web, email, w/e) that you can retrieve from a script on your phone.

Short of setting up a DNS server, I'm not sure there is any means for resolving host names that aren't named in your local configs.

share|improve this answer
    
I like the idea of posting to a server, and in fact I have a server machine at home I could use for this purpose. It seems a little bit inelegant, but it might be the best option. –  strictlyrude27 Aug 4 '11 at 0:26
3  
if you don't own a server, you can use dropbox. –  Lie Ryan Aug 4 '11 at 0:30
1  
If you've got ssh running, it should be easy to find a hook for wifi connecting, grep your ip and ssh echo or scp the ip back home. Then all your phone has to do is scp it back. –  Eric Coutu Aug 4 '11 at 0:32
    
I'll accept for now - I'll toy with all the other ideas though! –  Dang Khoa Aug 6 '11 at 17:38
add comment

If you don't want a server-based solution, your laptop could listen to the network for a specific broadcast packet. A broadcast packet will be rerouted to every device in the same network, but devices that aren't listening to it should ignore it.

share|improve this answer
    
This sounds like an ideal option! Not sure how to accomplish this in bash, if you could provide an example that would be very helpful; especially the listener side. –  strictlyrude27 Aug 4 '11 at 1:25
    
I'm not sure if you can do this from bash, but it's not difficult from either C or Java. –  David Yaw Aug 4 '11 at 1:45
    
It's a pretty simple thing in C and/or Java, I agree. But it has to be a shell script. I can only run shell or interpretable scripts (running the script from Tasker on my phone.) –  strictlyrude27 Aug 4 '11 at 6:19
    
If you write it in C, and compile to an executable (like the other shell execs (chmod, chown, etc.)), it can be invoked by a shell script if you drop the exec into a dir in your 1PATH`, namely /system/bin. –  KevinOrr Apr 18 '13 at 2:51
add comment

You can use avahi (wiki) (or a similar piece of Zeroconf software) to discover computers on the network and publish the list for your phone to retreive.

See Are there any other Java libraries for bonjour/zeroconf apart from JMDNS? for Java Zeroconf implementations.

share|improve this answer
add comment

There's an application called Network Discovery (available on F-Droid, may also be on the Android Market) that attempts to identify devices on the network your phone is connected to.

As it's Free Software, you could try to see if you can modify it or use portions of it for your purposes.

Not a "here's an APK" answer, but if you have to write something, it's a good start.

Edit: App is here: http://f-droid.org/repository/browse/?fdfilter=network%20discovery&fdid=info.lamatricexiste.network&fdpage=1

share|improve this answer
    
It's helpful generally speaking, but it won't suit my needs if I can't get this information into Tasker. That's why I wanted to go the shell script route - Tasker can execute shell scripts and interpreted scripts. I would upvote if I could, but this question got migrated from Stack Overflow and I don't have any rep here yet :/ –  Dang Khoa Aug 6 '11 at 17:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.