Android shell using busybox commands as default - android

I'm running a script on android and in effort to make it as portable as possible, all commands use busybox. How I've got it set up currently, is every command has a function named the same, so it converts those commands to use busybox, like so:
echo () {
busybox echo $1 $2 $3 $4 $5
}
echo "hai"
As this has to be done for every command, it takes a lot of space inside the script. That's why I'm trying to figure out a way to force the shell to default to using busybox rather than /system/bin or /system/xbin.
Could this be achieved by modifying the PATH variable? Or is there an environment variable build into shell I could use?
Or should I do something like this?:
bs () {
busybox $#
}
bs echo "Some text"
(I'd like to avoid this if possible as it decreases readability)
EDIT
Could I start a background process that loops and when it detects a command being passed for the shell to processes, it stops this and passes it to busybox? Somehow read from stdin before shell processes it?
EDIT 2
So I thought about redirecting commands into busybox like this:
busybox <<EOF
echo "hai";
EOF
Could this be used somehow?
UPDATE
I've moved to using busyboxes ash shell and it does everything I want it to do. Appareantly there's no way of intercepting commands before they're passed to the shell.

Use the shell's built-in 'alias' (Alias Substitution, Shell Command Language, IEEE Standard 1003.1 (2013) ) method.
For example:
alias vi="busybox vi"
vi readme.txt
will substitute the word 'vi' with 'busybox vi' and the shell will then process
busybox vi readme.txt
Because it is a simple substitution, all parameters are preserved and passed on to the substituted command.
You can also add default parameters to the substitution like so:
alias fbset="busybox fbset -fb /dev/graphics/fb0"
after which one no longer needs to specify the frame buffer - it is added for you. And typically programs will take the last specified parameter as the active one, so if you do need to specify another frame buffer (in the above example) it will ignore the one provided in the alias.

Related

How can I run additional commands after an alias in Powershell?

In my job, I work a lot with different android devices and interface with them using adb. Rather than remembering or needing to copy their serial numbers or IP addresses to refer to them, I've been trying to use aliases, but I'm running into issues. I'm currently using a combination of functions and profiles to refer to the devices. This is my current Profile.ps1 file:
function googletv_func ($params){
if ($params -eq "connect"){
adb connect 192.168.0.14
}
else{
adb -s 192.168.0.14 $params
}
}
function firehd_func ($params){
adb -s G0W1EW0 $params
}
Set-Alias googletv "googletv_func"
Set-Alias firehd "firehd_func"
If I enter the command "googletv connect", it will successfully connect to the google tv. But if I try something with more than one parameter following the alias (e.g. "googletv install example.apk" or "firehd shell pm list packages") it only executes the single parameter following the alias. Wondering what I'm missing and I appreciate anyone's help.

Is there a way to programatically check for used ports without looping?

I'm familiar with couple of methods to check for used ports on Android.
Loop over all possible ports, try creating a socket on each port, if socket creation fails port is used.
Use netstat command from shell which will list all open connections and used ports can be parsed from there.
Use cat /proc/net/tcp command from shell which is similar as netstat command.
Problem with 1st option is that looping over all possible ports is taking too much time, it's not efficient enough if I want to make sure that I'm getting all open ports.
Problem with 2nd and 3rd option is that (on non-rooted device) although shell command can be executed in shell (adb shell) and output is clearly seen, while trying to execute command from Java code in Android application, command output is empty string for cat /proc/net/tcp and only header is outputted from netstat command. I'm guessing that the problem are application permissions which are insufficient to run above commands.
Is there any other way of checking for used ports or am I doing something wrong by using commands from option 2 or 3?
EDIT:
To clarify, while using any command that should list connection info from adb shell this will work fine. However, if I'm trying to invoke any of the commands (same syntax as in adb shell) from my application's Java code, output of the command is empty string.
To get the process I tried using:
Runtime.getRuntime().exec(command);
new ProcessBuilder().command("sh", "-c", command).start();
E.g. output of netstat command (with any arguments) is as following:
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node Path
How about:
netstat -ln | awk '/^tcp/ { split($4,arr,":");prts[arr[2]]="" } END { for (i in prts) { print i } }'
Take the netstat output and then using awk, concentrate on all lines beginning with tcp. Then split the 4th delimited field into a array called arr based on ":" as the delimiter. Put the second index of the array (port number) in another array prts as the index. At the end, loop through the prts array and print the indexes (ports)

Get device information (such as product, model) from adb command

One way to achieve this is as follows:
adb devices -l
example output:
123abc12 device product:<id> model:<id> device:<id>
456abc45 device product:<id> model:<id> device:<id>
But this list's out all devices connected, but I want to get the information for a specific device.I want information only about "123abc12". The output should be:
123abc12 device product:<id> model:<id> device:<id>
The second device should not be shown.
I have the device name i.e 123abc12, and it can be used to get the required information, but I don't know how.
Thanks.
The correct way to do it would be:
adb -s 123abc12 shell getprop
Which will give you a list of all available properties and their values. Once you know which property you want, you can give the name as an argument to getprop to access its value directly, like this:
adb -s 123abc12 shell getprop ro.product.model
The details in adb devices -l consist of the following three properties: ro.product.name, ro.product.model and ro.product.device.
Note that ADB shell ends lines with \r\n, which depending on your platform might or might not make it more difficult to access the exact value (e.g. instead of Nexus 7 you might get Nexus 7\r).
Why don't you try to grep the return of your command ?
Something like :
adb devices -l | grep 123abc12
It should return only the line you want to.

ADB shell script to send AT commands to a modem-cannot return control to a shell and capture output

I already posted similar question, but still could not get my job done, so this a a second attempt, where
I would like to more clearly state my stumbling block.
So basically I am in Android phone's adb shell, communicating with the GPRS modem by sending AT commands.
I am able to do it by redirecting at command to the device file representing the modem; and I can read back
the response using cat utility running on the background (started earlier). I implemented it in a script
which can send a single AT command and read back the response. For example, here is the script to
send at+cops? to get the name of the operator the mobile is camping on:
#SendATCommand script
cat /dev/pts/7 &
echo -e at+cops?\\r > /dev/pts/7
The output looks as follows:
# ./sendATCommand
./sendATCommand
#
+COPS: 0,0,"AT&T",6
OK
/dev/pts/7: invalid length
Now here are two problems which I cannot resolve:
I still need to manually press ENTER button to get back adb shell prompt "#". Is there a way to return
to "#" prompt programmatically? Again, I am in adb shell.
The displayed response cannot be captured, neither in a variable, nor in file, (such as(#./sendATCommand > output.txt) Output.txt file will be empty. I tried various redirections, but still did not get it to work.
Can anyone please help me resolve those two problems (if ever possible)? Ultimately I want this little script to be
called from a "super" script (e.g. Perl or Powershell) running on PC to which my Android device is
connected, but there is no way to do it until those two problems resolved. Thanks a lot in advance!
I suggest that you try out my atinout program which should be exactly what you are asking for: a program to send AT commands from the command line and capture the output.
In your case the result should be like
$ echo 'at+cops?' | atinout - /dev/pts/7 -
+COPS: 0,0,"AT&T",6
OK
$
and to capture the output just put a file name instead of the last -.
I had similar problems with redirecting output to file. I resolved my problem by adding CMD /c in front of the echo command. I.e. if I understand correctly you need to tell the system that it needs to wait until command finishes executing and only then redirect output to a file. I was doing it in DOS.
Since you are running on ANDROID try adding sh -c in front of your command. Hope it helps.

how do i change timezone using adb

I want to change the timezone using adb shell command.
I went through some posts which allow changing it in linux, but they are not valid for android.
For the list of tzdata values I go to http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
To set the timezone use setprop persist.sys.timezone <TZ> command:
setprop persist.sys.timezone "America/Chicago"
adb shell service call alarm 3 s16 America/Phoenix, which will be effective immediately.
"setprop with persist.sys.timezone" only works after reboot.
adb shell service call alarm 4 s16 America/Chicago
4 - stands for the fourth function in alarm service implementation.
s16 - stands for string argument type.
https://android.stackexchange.com/questions/34625/where-to-find-description-of-all-system-bin-service-calls
In my (very) limited experience I have been able to edit the /system/build.prop file. I change the line in there that says persist.sys.timezone=America/Sao_Pao or some such drival to persist.sys.timezone=America/Chicago for my timezone CDT. I also changed the lines persist.sys.language=bt and persist.sys.country=AR to persist.sys.language=en and persist.sys.country=US respectively.
There are a few other things you can tweak in the file that will persist after a system restore. You might want to make sure that the build.prop file in a flash image or directory has the correct lines in it.
The /system/property directory seems to hold several text files with a single value. These seem to show up after using setprop but the OS doesn't seem to reflect the changes.
Editing these files usually does require root access either with a term program or the ADB.exe shell. (ADB = Android Device Bridge available in the Android SDK) I found ADB.exe while looking through the firmware downloads at JXD.HK for the S18 MiniPad. In the files.rar dl there is also the SuperUser.apk and a rooted version of busybox and su.
Best of Luck !!!
Please see this https://gist.github.com/jpkrause/6b7e576894a800d451bf for answer to your question.
So in your case it would be:
adb shell setprop persist.sys.timezone America/Chicago
This works with ADB
Get current global time zone:
adb shell settings get global time_zone
Set a time zone:
adb shell settings put global time_zone Europe/Madrid
Time zones list:
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
I changed 2 files to change my timezone.
the one that i don't entire know if it's necessary, but it keeps things in sync:
/etc/timezone
the more one you for sure need to change:
/etc/localtime
from http://www.cyberciti.biz/faq/linux-unix-set-tz-environment-variable/ you can determine the name of the timezone you want with tzselect.
I took inspiration from http://www.cyberciti.biz/faq/howto-set-date-and-time-timezone-in-freebsd/
and copied my timezone file from /usr/share/zoneinfo/ to /etc/localtime.
/etc/timezone is a text file with the name of the timezone. so i just updated it to match the value provided by tzselect.

Categories

Resources