run commands in linux shell using batch file

adb shell as root after device rooted

once device rooted, we must perform "su" before we get root permission in adb shell,this is not convenient in some situations,so there have a method to get permission without perform "su".

adb shell
su
mount -o remount,rw /system
chmod 4755 /system/bin/sh
exit

-rwsr-xr-x root     shell      285016 2015-02-02 20:57 sh

we set /system/bin/sh to SUID permisson,now we can get root permission directly after adb shell.

maybe this method have secure issue, so only for specific suituation used.

BAT脚本自动执行 adb shell 以后的命令

@echo off
echo su > temp.txt
adb shell <temp.txt
echo 其它命令 >> temp.txt
echo 其它命令 >> temp.txt
adb shell <temp.txt
del temp.txt

adb脚本命令之su命令被打断的问题

以抓去设备网络包为例, 将命令行统一放到一个临时文件里面,然后批次执行。

adb start-server

adb push G:\cachelog\tcpdump /data/local/tcpdump

@echo off

set p=G:\cachelog\temp.txt

echo su > %p%

echo chmod 755 /data/local/tcpdump >> %p%

echo rm -r /sdcard/capture.pcap >> %p%

echo /data/local/tcpdump -p -vv -s 0 -w /sdcard/capture.pcap >> %p%

adb shell < %p%

del %p%

pause

run commands in linux shell using batch file

I need to be able to run the following commands on a rooted Android phone connected to my PC:

su
mount -o rw,remount /system
chmod 777 /system/app/
exit
exit

And then I need to execute the following command on my PC:

adb push ..\bin\MyApp_signed.apk /system/app/

And finally, I need to do some clean up and reboot the phone:

su
chmod 755 /system/app/
reboot
exit
exit

How can I create a windows batch file so that those shell commands are passed to the android shell?

adb shell <command> allows you to run a shell command on the device from your computer.
If your su command supports the -c option, you can do this:

adb shell su -c "mount -o rw,remount /system"
adb shell su -c "chmod 777 /system/app/"

adb push ..\bin\MyApp_signed.apk /system/app/

adb shell su -c "chmod 755 /system/app/"
adb shell su -c reboot

If your su command doesn‘t support commands, you could make a script to leave on the device that bundles the device side of it.

It appears that I was using a very simple version of su which did not accept the -c argument.

I copied another su which did work.

AndyD is totally right though, so I am accepting his answer instead of mine :)

Launch a script as root through ADB

I have created a script to mount partitions and do some stuff in my Android system. I saved the script as install.sh in the /bin folder of Android.

I want to call the script from ADB, which is itself called from a batch file on Windows, but it needs to be executed as root.

This works for me:

Create myscript.bat and put into it (note the single quotes around the commands to be executed in superuser mode):

adb shell "su -c ‘command1; command2; command3‘"

hen run myscript.bat from a DOS shell.

Note: it doesn‘t appear that the the DOS line continuation character (^) works in this situation.

In other words, the following doesn‘t work for me: This results in "Syntax error: Unterminated quoted string"

adb shell "su -c ‘^
command1; ^
command2; ^
command3‘"

This works :

adb shell echo command which needs root privileges \| su

If you need redirection:

adb shell echo ‘echo anytext > /data/data/aforbiddenfolder/file‘ \| su

For "copying" a local file to an android path needing root privileges (but alocalfile must not contain ):

cat alocalfile | adb shell echo "echo ‘`cat`‘ > /data/data/aforbiddenfolder/file" \| su

This works for me:

adb shell "su -c ./data/local/tcpdump-arm -s 0 -v -w /data/local/appxpress_dump.pcap"

but it does not work as it gives me a shell access (with root permissions), but nothing is executed.

How do you know that you are given root permissions?

I assume you are attempting to execute the script on a device? Has your device been rooted?

You may need to give execute permissions via chmod to the file.

chmod ugo=rwx /bin/script.sh

Android ADB Commands Using Batch File

This tip will show how to use a batch file for adb commands.

Preface

This tip is for Windows users, but the concept is the same for all platforms.

It will show how to set several adb commands into one batch file.

Introduction

When developing for Android platform, sometimes you find yourself typing the same commands in the adb (Android Debug Bridge),

commands like granting permissions for internal files.

I find it much easier to create a batch file for each purpose and run it when necessary.

Batch files are script files that contain a series of commands that will be executed at the command interpreter (CMD on Windows).

Linux also has the same feature, in fact much more flexible, called shell scripting.

One of the common operations that I run constantly on my Android applications is

to pull out my sqlite database to my PC in order to examine its structure.

So, instead of typing the commands one by one, I created a batch file and named it db_lookup.bat.

This is the content of my batch file, simply type it in Notepad

and change its extension from txt to bat as you save, in order to run it as batch file.

cd %ANDROID_SDK%/platform-tools
cd %ANDROID_SDK%/platform-tools
adb shell su -c "chmod 777 /data"
adb shell su -c "chmod 777 /data/data"
adb shell su -c "chmod 777 /data/data/your.packagename"
adb shell su -c "chmod 777 /data/data/your.packagename/databases"
adb shell su -c "chmod 777 /data/data/your.packagename/databases/MyDB"
adb pull /data/data/your.packagename/databases/MyDB C:\Users\Pavel\Desktop

http://www.faqoverflow.com/android/5884.html

QUESTION

Is there a way for me to run Adb shell as root without typing in su?

I want to be able to have root access without going into the shell.

ANSWER

If I‘m understanding your question correctly, you‘re asking how to get root access automatically when you run adb shell,

so that you don‘t have to go into the shell and type su to execute a command as root on your phone.

This is controlled by a flag in your boot partition, which most custom ROMs have modified to allow root permission by default.

If you get dropped to a $ when you use adb shell then you have

two permanent options and one temporary one (temporary meaning that it doesn‘t stick if you reboot):

  1. Flash a ROM that includes the modification
  2. Create a custom boot.img and flash it to your phone
  3. Restart adbd on your device with root permissions

Number 2 is actually not that difficult, but it can be somewhat dangerous if you‘re careless.

There is a wiki article here which explains the process and includes a couple of Perl scripts to help you.

These instructions assume a Linux/Unix environment.

I have personally used these instructions on Fedora Linux and can tell you that they work, but I do not know about other *nix environments like Macs.

I also do not know of any similar Windows instructions, but I could try to track some down if you are on Windows.

The exact commands will vary somewhat from phone to phone since different devices use different partition tables.

Generally speaking, though, you need to pull your current boot.img from your phone, unpack it, extract the ramdisk, and find the default.prop file.

This is a plaintext file, which you need to open in a text editor and then find the line that contains the value ro.secure.

If the line says ro.secure=1 then you need to change it to ro.secure=0.

After that you can re-pack the ramdisk and boot.img, then flash it to your phone.

Once you reboot, you will be greeted with a # prompt whenever you perform adb shell without having to run su.

Alternatively, if you are using a custom ROM but it doesn‘t have this modification,

you can just unzip the ROM and modify the boot.img that is included with it using the above steps.

Then you can zip up the ROM with the newly modified boot.img and flash the zip file as you normally would.

This probably goes without saying, but be careful when doing this.

Messing with your boot partition can quite easily wreck your phone and force you to recover via HBoot.

I would highly recommend testing Fastboot to makes sure you can use extended Fastboot commands and perform a recovery.

This varies a bit based on your model, but most phones have some sort of desktop software that can be run to reflash the phone as well.

The third option is that in many cases it may be possible to restart adbd on your device with root privileges.

One possibility is to execute adb root from a PC terminal, although this will not work on all ROM configurations
(the ROM must be built as a "userdebug" one).

You can also try Chainfire‘s adbd insecure app.

This will not persist across a reboot, so you would need to use either the app or adb root again any time you restarted your phone.

adbd Insecure v2.00

adbd Insecure lets you run adbd in root mode if your device is rooted.
(Note that if you are running a custom kernel, it is likely that it already implements this functionality)
If you are running a stock (made by the phone manufacturer) kernel on your device,
chances are adbd is running in "secure" mode, even if you are rooted.

This app lets you run adbd in "insecure" mode, which gives you root access in "adb shell",
allows access to system files and directories through "adb push/pull",
and lets you run the "adb remount" command to make your /system partition writable.

Of course, your device must already be rooted for this to work.
It may not work with "S-ON" devices and locked bootloaders.

This app does not make any permanent changes to your device - a reboot will undo any modification.
That‘s why there is also the option to enable this feature at boot.

Note that if you have USB connected when the app activates or deactivates adbd root mode,
you may need to unplug/replug the cable
or run "adb kill-server" on your computer before adb will work again.

时间: 2024-11-06 19:46:37

run commands in linux shell using batch file的相关文章

[Python] 利用commands模块执行Linux shell命令

用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要函数: 1. commands.getoutput('shell command') 执行shell命令,返回结果(string类型) >>> commands.getoutput('pwd') '/home/oracle' 2. commands.getstatus('file') 该函数

LINUX SHELL脚本攻略笔记[速查]

Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述符和重定向 cat 数组和关联数组 alias date 调试脚本 函数和参数 管道 读取命令输出 read 字段分隔符和迭代器 循环 比较和测试 find xargs tr md5sum sha1sum 对目录进行校验 sort uniq tempfile split bash变量匹配切分 exp

linux shell except tcl login ssh Automatic interaction

/*************************************************************************************** * linux shell except tcl login ssh Automatic interaction * 声明: * 本程序是使用except自动登入远程目标机,并且执行commands文件中的命令给定的命令, * 可以对多个目标机进行测试,目标机的IP保存在shell的数组中,目前只支持相同的账户和密码.

Linux Shell Bash 带有特殊含义的退出码

Linux Shell Bash 带有特殊含义的退出码 用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前脚本执行. 常用参数 格式:exit n 退出.设置退出码为n.(Cause the shell to exit with a status of n.) 格式:exit 退出.退出码不变,即为最后一个命令的退出码.(If n is omitted, the exit status is that of the  last  command executed. )

Linux shell脚本基础学习详细介绍(完整版)一

Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Linux 脚本编写基础◆1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序. 当编辑好脚本时,如果要执行该脚本,还必须使其可执行. 要使脚本可执

思维导图学 Linux Shell攻略之小试牛刀篇

曾听一位大神讲过,带着目的去学,知识往往能记得牢,记得稳.借助思维导图这个工具,对一些我感兴趣的知识点进行分类管理.以后方便自己复习. 我会以思维导图+代码段的方式,回滚学习linux shell编程. 转义/色彩 与用户交互的接口 #打印一个普通的字符串 [[email protected] ~]# echo "it's isa dog" it's is a dog   #打印一个带有单引号和换行符的字符串,单引号可正常输出,但换行符没有效果 #没有达到想要的效果 [[email p

Linux Shell 文本处理工具集锦 zz

内容目录: find 文件查找 grep 文本搜索 xargs 命令行参数转换 sort 排序 uniq 消除重复行 用tr进行转换 cut 按列切分文本 paste 按列拼接文本 wc 统计行和字符的工具 sed 文本替换利器 awk 数据流处理工具 迭代文件中的行.单词和字符 本文将介绍Linux下使用Shell处理文本时最常用的工具: find.grep.xargs.sort.uniq.tr.cut.paste.wc.sed.awk: 提供的例子和参数都是最常用和最为实用的: 我对shel

运维经验分享(七)-- Linux Shell之ChatterServer服务控制脚本第三次优化

运维经验分享作为一个专题,目前共7篇文章 <运维经验分享(一)-- Linux Shell之ChatterServer服务控制脚本> <运维经验分享(二)-- Linux Shell之ChatterServer服务控制脚本二次优化> <运维经验分享(三)-- 解决Ubuntu下crontab不能正确执行Shell脚本的问题(一)> <运维经验分享(四)--关于 java进程管理的服务控制脚本编程思路分析> <运维经验分享(五)-- 改进的java进程管

20 Useful Commands for Linux Newbies

英文原文:20 Useful Commands for Linux Newbies 你打算从Windows换到Linux上来,还是你刚好换到Linux上来?哎哟!!!我说什么呢,是什么原因你就出现在我的世界里了.从我以往的经验来说,当我刚使用Linux,命令,终端啊什么的,吓了我一跳.我担心该记住多少命令,来帮助我完成所有任务.毫无疑问,在线文档,书籍,man pages以及社区帮了我一个大忙,但是我还是坚信有那么一篇文章记录了如何简单学习和理解命令的秘籍.这激发了我掌握Linux和使它容易使用