I want to create an android activity for setting up an SSH Session with a remote device (through Wifi) and executing some linux commands on the remote device. Anyone got a quick, short example for connecting, authenticating and sending remote commands using Trilead libraries in Android ? Connectbot source (the only place to find the source for the unmaintained library) is quite vast and time consuming to go through if one is just trying to do a quick SSH Connection/ Command execution. I had found the sshJ library earlier, which had nicely documented examples and tips but unfortunately Android lacks some Java.Util classes required for sshJ.
I am looking for something in Trilead like (this is from the sshJ example I found earlier) :
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost");
try {
ssh.authPublickey(System.getProperty("user.name"));
final Session session = ssh.startSession();
try {
final Command cmd = session.exec("ping -c 1 google.com");
System.out.print(cmd.getOutputAsString());
System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally {
session.close();
}
} finally {
ssh.disconnect();
}
If you are okay with restricting your app to Android 2.3+ (Gingerbread), then you can use sshj. You will have to create the SSHClient object with this
Config.
Related
I would like to set my Android app as device owner on a device running Android Things.
(I know that Android Things is getting shut down, but still I would need to make it work)
I tried the usual command: adb shell dpm set-device-owner <package>/<component>
This command works well on a standard Android device, but on Android Things I get the following response:
java.lang.RuntimeException: Can't set package ComponentInfo{<package>/<component>} as device owner.
at com.android.commands.dpm.Dpm.runSetDeviceOwner(Dpm.java:149)
at com.android.commands.dpm.Dpm.onRun(Dpm.java:96)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:54)
at com.android.commands.dpm.Dpm.main(Dpm.java:41)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:285)
I tried running the command as root (after adb shell & su), but still get the same error.
I also tried to create manually device_owner.xml, device_policies.xml,... files in /system/data, as mentionned in topics related to AOSP, but it has no effect.
Is it somehow possible to set device owner on an Android Things device?
Android Things are already deprecated now, you can use smartphone OS or Android. That is based on this quote:
Android Things is a now-deprecated Android-based embedded operating system platform by Google, announced at Google I/O 2015, and launched in 2018. In 2019, Android Things dropped support for low-power hardware and refocused on smartphone-class devices.
Owner in android can be used to set in this document explanation, sample code :
val provisioningActivity = getActivity()
// You'll need the package name for the DPC app.
val myDPCPackageName = "com.example.myDPCApp"
// Set up the provisioning intent
val provisioningIntent = Intent("android.app.action.PROVISION_MANAGED_PROFILE")
provisioningIntent.putExtra(myDPCPackageName,
provisioningActivity.applicationContext.packageName)
if (provisioningIntent.resolveActivity(provisioningActivity.packageManager) == null) {
// No handler for intent! Can't provision this device.
// Show an error message and cancel.
} else {
// REQUEST_PROVISION_MANAGED_PROFILE is defined
// to be a suitable request code
startActivityForResult(provisioningIntent,
REQUEST_PROVISION_MANAGED_PROFILE)
provisioningActivity.finish()
}
I am using a Raspberry Pi Zero W. I have succeeded in connecting the Pi to my Android Device on startup of Pi. Then I turn on Internet Sharing to make sure my Pi has an internet connection. I want to make an application which can receive data from Android Device and run preexisting scripts based on it without using ssh, if possible.
I normally use Juice SSH on my android phone to run scripts on the Pi but that involves manual work like finding and executing the script which I do not want my user to do.
The script I want to run is a Google Directions Python Script. I have the script ready, it just takes input of Origin and Destination from the user. After that it fetches the Direction Response and starts showing instructions on a screen connected to the Pi.
TLDR: I would like to know a way to initiate a python script on a Raspberry Pi from an Android Device connected via Bluetooth. Do I need to make a server? Is it possible using Firebase?
I actually mounted something very similar not too long ago. You may be able to get around various ways but I think some sort of server is going to be needed any way.
Take a look at my public repository in github!
git clone https://github.com/NanoSpicer/XpressShutdown
You could then modify my index.js file like so:
#!/usr/bin/node
const command = 'python yourscript.py';
const proc = require('child_process');
const express = require('express');
const app = new express();
const router = express.Router();
router.get('/customComand', (request, response) => {
// you could even catch parameters with this, edit your command string and pass them into the script
proc.exec(command, (err, stdout, stderr) => {
response.json({output: stdout});
});
});
app.use('/raspi', router);
app.listen(80);
console.log('Server is running');
Get that server up and running as a background process with:
chmod +x index.js
./index.js & # you can do this because of the shebang
Make the HTTP request like
http://{your-raspi-IP-address}/raspi/customComand
And now you could run your command wherever in the world if you can perform an http request to your raspi!
I solved this problem by using the Jsch Library for Android. It is quite simple and well documented. It allows me to start a SSH connection with a set command that I want to execute on the Server.
I'm hitting this error:
java.lang.SecurityException: adb clearing user data is forbidden.'; Code: '1'
while running the below mentioned code on appium server in android 8.1.0 (oreo) but the same code is running fine in android 5.1 (lollipop). USB debugging is already on in mobile. Anyone having any idea why I'm hitting this error?`
package appium;
#BeforeTest
public void setcapbilities() throws Exception
{
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName","Vipul");
cap.setCapability(CapabilityType.VERSION,"8.1");
cap.setCapability("platformName","Android");
cap.setCapability("appPackage","com.android.calculator2");
cap.setCapability("appActivity","com.android.calculator2.Calculator");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),cap);
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
}
#Test
public void firstt()
{
driver.findElement(By.xpath("//android.widget.Button")).click();
driver.findElement(By.xpath("7")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
#AfterTest
public void kill()
{
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.quit();
}
There few pointers which you can try to solve the issue you're facing.
First, use latest stable Appium server and Java client available.
Second, use AndroidDriver <MobileElement> instead of RemoteWebDriver. Since, AndroidDriver extends RemoteWebDriver via AppiumDriver, it must have overiden the quit method functionality.
Third, you don't need to call implicit wait everywhere. You must call it once in your set up method.
Forth, update your Android SDK and tools to latest.
It is not neccessary that if a something works in andorid v5 , then it should also work on android v8
Reason
To make the devices more secure , latest android versions are imposing restrictions on some already working APIs.
e.g. You can enable airplane mode using Appium API in mobile devices till Android V6, from Android V7 onwards this API has been blocked.
Solution
looking at your code, it seems ,the session should get created successfully , the only thing u need to take care is how to make your test successfull. May be you need to change your Xpath as per the UI for Android V8.
did you try checking this Xpath in APPIUM Desktop ?
https://automationlab0000.wordpress.com/2018/12/31/appium-desktop-for-locating-elements/
I am able to create the multiple user profile on Jellybean using "pm create-user Name" terminal command on Emulator, but I want to know is there any way so I can run the same command programmatically. I don’t want to open the terminal.
When the Multiple User feature was first found in 4.1, I wrote an app to do just this. I've open sourced it here. You can find the code for running that command programmatically in TerminalUtils, but I'll put it in the answer as well.
public static void createUser(String name)
{
Process p;
try {
p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("pm create-user \"" + name + "\"\n");
os.writeBytes("exit\n");
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
TerminalUtils also contains methods to remove and rename users.
Yes you can by :
Using Stericson RootTools :
RootTools provides rooted developers a standardized set of tools for
use in the development of rooted applications. In the end, we will
accomplish this by providing developers with robust, easy-to-use
libraries that will drastically improve development times as well as
promote code reuse. This project is open to any proven developer that
feels they have something to contribute. By pitching in together we
can streamline our own processes, improve the effectiveness of our
apps, learn new techniques, and provide a better experience for our
users.
Using Android Runtime :
Allows Java applications to interface with the environment in which
they are running. Applications can not create an instance of this
class, but they can get a singleton instance by invoking getRuntime().
I want to build an android application in which the android device work as airplay server (receiver) and iOS device as a client(sender). I have followed this link . But here you have to first register to the port to appear as a airplay option on iOS device from the command line using :
mDNS -R MyAirplayService _airplay._tcp local 22555
When i run this java code I can see airplay icon on my iOS device . But how it can be don in android device ? Is there any open source code or library to do this ?
That code is basically registering an airplay tcp service in the local network, so that any other iOS device on the same local network can discover this airplay service, and therefore display the airplay icon as an option.
In iOS, this can be done using the Bonjour/NSNetService. Please refer to the Apple's official tutorial.
NSNetService *service;
service = [[NSNetService alloc] initWithDomain:#""// 1
type:#"_airplay._tcp"
//this will show up as the airplay name
name:#"myiOSAirplayServer"
port:port];
if(service)
{
[service setDelegate:delegateObject];// 2
[service publish];// 3
}
else
{
NSLog(#"An error occurred initializing the NSNetService object.");
}
In Android, this can be done using Network Service Discovery, and the official example is here:
public void registerService(int port) {
NsdServiceInfo serviceInfo = new NsdServiceInfo();
//this will show up as the airplay name
serviceInfo.setServiceName("myAndroidAirplayServer");
serviceInfo.setServiceType("_airplay._tcp.");
serviceInfo.setPort(port);
mNsdManager = Context.getSystemService(Context.NSD_SERVICE);
mNsdManager.registerService(
serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}
However, doing so just registers the service in local network, and gives you an icon in the iOS device. To do the real airplay server/mirroring service, you need to do a lot more. If you want to know that, please check my iOS app that works as an Airplay mirroring server, https://www.youtube.com/watch?v=0d6ggJMypIk. There is also an open source project that is written in python, called PyOpenAirMirror.
If I'm not mistaken, Airplay is an Apple-only API. I have tried getting it to be recognizable on android and I was largely unsuccessful. You may want to consider another mode of transmission for the streaming audio.
I would look at Erica Sadun's utilities. I may be mistaken, but I think they are open source. She has written a server, player/transmitter etc. for AirPlay.
http://ericasadun.com/category/airplayer/