使用adb的无线调试功能十分的方便。 但是实际工作中,总是要手动输入,十分麻烦。所以就把它写成脚本文件。
windows
双击运行bat
文件,mac
的话terminal
运行)bat
脚本adb_wifi.bat
@echo off adb disconnect REM 待处理的字符串 adb shell "ip address | grep inet | grep -v inet6 | grep -v 127" >ip.txt set /P IP=<ip.txt del ip.txt echo %IP% set str="%IP%" REM set str="inet 192.168.1.137/24 brd 192.168.1.255 scope global wlan0" REM FOR语句是按行进行遍历,也就是一行一次循环,对于我们来说,只有一行字符串, REM 这个FOR循环肯定只进入一次。而我们是要对字符串进行空格分割后再处理。因此这 REM 里用GOTO语句来实现循环。而FOR语句只需看成是一条语句,这条语句实现了这样的 REM 功能:将字符串分割成两一部分,一部分是第一个空格前的字串,另一部分是剩余的 REM 字串(tokens=1,*)。第一部分保存在 a 变量中,第二部分保存在 b 变量中,这 REM 个 b 是自动的。 for /f "tokens=2,*" %%a in (%str%) do ( REM 这里可以替换成自己的处理程序,现在只是简单地显示值 REM set a = "%%a" REM echo %a% REM 将剩余字符串存在b里面 set str="%%a" ) echo str = %str% for /f "delims=/ tokens=1,*" %%a in (%str%) do ( REM 这里可以替换成自己的处理程序,现在只是简单地显示值 REM set a = "%%a" REM echo %a% REM 将剩余字符串赋值给str变量 set str=%%a ) echo str = %str% adb tcpip 6666 set _ip=%str%:6666 adb connect %_ip% >result.txt set /P RESULT=<result.txt del result.txt echo %RESULT% |findstr "^connected" >nul if %errorlevel% equ 0 ( echo "成功连接到%_ip%" ) else ( echo "连接到%_ip%失败!!" pause )
bat
脚本运行于windows
,双击就可以运行。adb_wifi.sh
#!/bin/bash _port=":6666" port=${_port:1-start} # 将adb先断开 adb disconnect #取到ip地址. 这里包括了3钟获取IP的方法,ip address应该是通用的方法 ip=`adb shell "ip address | grep inet | grep -v inet6 | grep -v 127"` len=${#ip} if [[ $len != 0 ]]; _ip=`echo $ip |cut -f 2 -d ' ' |cut -f 1 -d '/'`${_port} then ip=`adb shell "ifconfig | grep inet | grep -v inet6 | grep -v 127"` len=${#ip} # 在魅族的手机上,可能获取不到数据,通过另外的方法来取 if [[ $len == 0 ]]; then ip=`adb shell "getprop dhcp.wlan0.ipaddress" | head -n 1` len=${#ip} val=`expr $len - 1` _ip=${ip:0:val}${_port} else ip=`echo ${ip} | cut -f 2 -d ' '` _ip=`echo ${ip} | cut -f 2 -d ':'`${_port} fi fi result=`adb tcpip ${port}` result=`adb connect ${_ip}` echo $result
shell
脚本不需要文件做中间变量就可以。shell
脚本的运行
必须先给对应的权限.然后运行。chmod +x adb_wifi.sh ./adb_wifi.sh
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句