Exemple #1
0
 /// <summary>
 /// 执行shell的指令,自动判断指令方式。若root,则使用root指令。
 /// </summary>
 /// <param name="command">指令</param>
 /// <param name="device">目标设备</param>
 /// <param name="maxtimeout">最大超时时间</param>
 internal void ExecuteRemoteAutoCommand(string command, Device device, AbstractOutputReceiver receiver,
                                        int buffersize = 16 * 1024,
                                        int maxtimeout = 9000)
 {
     if (device.IsRoot && device.RealStatus != EnumDeviceStatus.Recovery)
     {
         ExecuteRemoteCommand(string.Format("su -c \"{0}\" ", command), device, receiver, buffersize, maxtimeout);
     }
     else
     {
         ExecuteRemoteCommand(command, device, receiver, buffersize, maxtimeout);
     }
 }
Exemple #2
0
        /// <summary>
        /// 执行shell指令,不产生异常
        /// </summary>
        /// <param name="command">指令</param>
        /// <param name="device">目标设备</param>
        /// <param name="maxtimeout">最大超时时间</param>
        public ExecuteRemoteCommandResult ExecuteRemoteCommandNoException(string command, Device device,
                                                                          AbstractOutputReceiver receiver,
                                                                          int buffersize = 16 * 1024,
                                                                          int maxtimeout = 3000)
        {
            if (String.IsNullOrWhiteSpace(command) || device == null)
            {
                return(new ExecuteRemoteCommandResult(new ArgumentException("The command or device is invalid")));
            }

            string[] cmd   = command.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            var      tdata = this.ExecuteRemoteCommandString(command, device, receiver, buffersize, maxtimeout);

            if (tdata.Contains(String.Format("{0}: not found", cmd[0])))
            {
                return(new ExecuteRemoteCommandResult(new FileNotFoundException(string.Format("The remote execution returned: '{0}: not found'", cmd[0]))));
            }
            if (tdata.Contains("No such file or directory"))
            {
                return(new ExecuteRemoteCommandResult(new FileNotFoundException(String.Format("The remote execution returned: {0}", tdata))));
            }
            if (tdata.Contains("Operation not permitted"))
            {
                return(new ExecuteRemoteCommandResult(new ApplicationException(tdata)));
            }
            if (tdata.Contains("Unknown option"))
            {
                return(new ExecuteRemoteCommandResult(new ApplicationException(tdata)));
            }
            if (tdata.Contains("Starting service: ") &&
                tdata.Contains("java.lang.SecurityException:") &&
                tdata.Contains("is not privileged to communicate"))
            {
                return(new ExecuteRemoteCommandResult(new ApplicationException(tdata)));
            }
            if (IsMatch(tdata, "Aborting.$"))
            {
                return(new ExecuteRemoteCommandResult(new ApplicationException(tdata)));
            }
            if (IsMatch(tdata, "applet not found$") && cmd.Length > 1)
            {
                return(new ExecuteRemoteCommandResult(new FileNotFoundException(string.Format("The remote execution returned: '{0}'", tdata))));
            }
            if (IsMatch(tdata, "(permission|access) denied$"))
            {
                return(new ExecuteRemoteCommandResult(new PermissionDeniedException()));
            }

            return(new ExecuteRemoteCommandResult(true));
        }
Exemple #3
0
        /// <summary>
        /// 执行shell命令,返回string
        /// </summary>
        /// <param name="command"></param>
        /// <param name="device"></param>
        /// <param name="receiver"></param>
        /// <param name="buffersize"></param>
        /// <param name="maxtimeout"></param>
        /// <returns></returns>
        ///
        private string ExecuteRemoteCommandString(string command, Device device,
                                                  AbstractOutputReceiver receiver,
                                                  int buffersize = 16 * 1024,
                                                  int maxtimeout = 3000)
        {
            if (String.IsNullOrWhiteSpace(command) || device == null)
            {
                throw new ArgumentException("The command or device is invalid");
            }
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                socket.Connect(this.SocketAddress);
                socket.ReceiveTimeout    = maxtimeout;
                socket.SendTimeout       = maxtimeout;
                socket.ReceiveBufferSize = buffersize;
                this.SetDevice(socket, device);
                //reqest
                var request = AdbSocketHelper.FormAdbRequest("shell:" + command);
                AdbSocketHelper.Write(socket, request);
                //response
                var res = AdbSocketHelper.ReadResponse(socket);
                if (!res.IsOkay)
                {
                    throw new ApplicationException(string.Format("device[{0}] response error:{1}", device.SerialNumber, res.Data));
                }
                //read
                AdbSocketHelper.Read(socket, receiver, buffersize);
                //验证输出是否合法
                //determines weahter the output is valid
                var tdata = receiver.Data.ToString().Trim();

                return(tdata);
            }
            catch (DoneWithReadException de)
            {
            }
            catch (SocketException ex)
            {
                throw new ApplicationException(string.Format("The shell command\"{0} \" has become unresponsive! ,max timeout:{1}", command, maxtimeout));
            }
            finally
            {
                this.DisposeSocket(socket);
            }

            return(string.Empty);
        }
Exemple #4
0
        /// <summary>
        /// 执行ADB指令,可以指定端口,device可以为null,若为null则不指定设备。
        /// 结果接收器receiver可以为null,为null则不处理结果数据
        /// </summary>
        public void ExecuteAdbSocketCommand(Device device, string command, int port = 5037,
                                            AbstractOutputReceiver receiver         = null,
                                            int bufSize = 16 * 1024, int timeOut = 9000)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                socket.Connect(IPAddress.Loopback, port);
                socket.Blocking          = true;
                socket.ReceiveBufferSize = bufSize;
                socket.SendBufferSize    = bufSize;
                socket.ReceiveTimeout    = timeOut;
                socket.SendTimeout       = timeOut;
                if (device != null)
                {
                    this.SetDevice(socket, device);
                }
                byte[] request = AdbSocketHelper.FormAdbRequest(command);
                AdbSocketHelper.Write(socket, request);
                var res = AdbSocketHelper.ReadResponse(socket);
                if (!res.IsOkay)
                {
                    throw new Exception(string.Format("Device rejected command:{0}. Error:{1} ", command, res.Data));
                }

                if (receiver != null)
                {
                    AdbSocketHelper.Read(socket, receiver, bufSize);
                }
            }
            finally
            {
                this.DisposeSocket(socket);
            }
        }
Exemple #5
0
 /// <summary>
 /// 执行shell的指令,自动判断指令方式。若root,则使用root指令。不产生异常
 /// </summary>
 /// <param name="command">指令</param>
 /// <param name="device">目标设备</param>
 /// <param name="maxtimeout">最大超时时间</param>
 internal ExecuteRemoteCommandResult ExecuteRemoteAutoCommandNoException(string command,
                                                                         Device device, AbstractOutputReceiver receiver,
                                                                         int buffersize = 16 * 1024,
                                                                         int maxtimeout = 3000)
 {
     if (device.IsRoot && device.RealStatus != EnumDeviceStatus.Recovery)
     {
         return(this.ExecuteRemoteCommandNoException(string.Format("su -c \"{0}\" ", command), device, receiver, buffersize, maxtimeout));
     }
     else
     {
         return(this.ExecuteRemoteCommandNoException(command, device, receiver, buffersize, maxtimeout));
     }
 }