execute adb commands in cmd prompt from python script











up vote
0
down vote

favorite
1












I am trying to execute adb commands in an automated way from python script. Please note that I am using python 2.7 in Windows.
If I manually do it, the sequence is like this:



C:Projectpython>adb shell
login:<enter login id e.g. root>
Password: <enter password e.g. test>
Last login: Thu Feb 9 12:29:46 UTC 2017 on pts/0
~ # date
date
Thu Feb 9 12:55:06 UTC 2017


I am trying to handle this sequence from python script. I have tried using subprocess.call("adb shell date") but it fails saying that cannot execute commands without logging in. I am not sure how to to pass the login id and password. Sorry for the noob question, as I am very new to Python.



Appreciate your help guys !!



Cheers










share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    I am trying to execute adb commands in an automated way from python script. Please note that I am using python 2.7 in Windows.
    If I manually do it, the sequence is like this:



    C:Projectpython>adb shell
    login:<enter login id e.g. root>
    Password: <enter password e.g. test>
    Last login: Thu Feb 9 12:29:46 UTC 2017 on pts/0
    ~ # date
    date
    Thu Feb 9 12:55:06 UTC 2017


    I am trying to handle this sequence from python script. I have tried using subprocess.call("adb shell date") but it fails saying that cannot execute commands without logging in. I am not sure how to to pass the login id and password. Sorry for the noob question, as I am very new to Python.



    Appreciate your help guys !!



    Cheers










    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I am trying to execute adb commands in an automated way from python script. Please note that I am using python 2.7 in Windows.
      If I manually do it, the sequence is like this:



      C:Projectpython>adb shell
      login:<enter login id e.g. root>
      Password: <enter password e.g. test>
      Last login: Thu Feb 9 12:29:46 UTC 2017 on pts/0
      ~ # date
      date
      Thu Feb 9 12:55:06 UTC 2017


      I am trying to handle this sequence from python script. I have tried using subprocess.call("adb shell date") but it fails saying that cannot execute commands without logging in. I am not sure how to to pass the login id and password. Sorry for the noob question, as I am very new to Python.



      Appreciate your help guys !!



      Cheers










      share|improve this question













      I am trying to execute adb commands in an automated way from python script. Please note that I am using python 2.7 in Windows.
      If I manually do it, the sequence is like this:



      C:Projectpython>adb shell
      login:<enter login id e.g. root>
      Password: <enter password e.g. test>
      Last login: Thu Feb 9 12:29:46 UTC 2017 on pts/0
      ~ # date
      date
      Thu Feb 9 12:55:06 UTC 2017


      I am trying to handle this sequence from python script. I have tried using subprocess.call("adb shell date") but it fails saying that cannot execute commands without logging in. I am not sure how to to pass the login id and password. Sorry for the noob question, as I am very new to Python.



      Appreciate your help guys !!



      Cheers







      python-2.7 adb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 9 '17 at 7:35









      wicked_snail

      412




      412
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          Try subprocess.Popen:





          import subprocess

          cmd_input = """<enter login id e.g. root>
          <enter password e.g. test>
          date"""

          process = subprocess.Popen(
          "adb shell",
          shell=True,
          stdin=subprocess.PIPE,
          stdout=subprocess.PIPE
          )
          process.communicate()
          for i in cmd_input.split("n"):
          process.communicate(i + "n")


          Or:



          process = subprocess.Popen(
          "adb shell",
          shell=True,
          stdin=subprocess.PIPE,
          stdout=subprocess.PIPE
          )
          process.communicate(user + "n")
          process.communicate(pwd + "n")
          process.communicate(cmd + "n")


          Another option is to use google/python-adb or adb via pip






          share|improve this answer























          • Just to clarify, comments in <..> are only for representation of what I do manually. I tried cmd_input = "root test date" and then subprocess.call("adb shell", shell=True, input=cmd_input). But I am getting this error Traceback (most recent call last): File "subprocess_login.py", line 4, in <module> subprocess.call("adb shell", shell=True, input=cmd_input) File "C:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
            – wicked_snail
            Feb 9 '17 at 8:09










          • @wicked_snail I know, I just copied them over. input is my error, I looked at the Python3 documentation.
            – CodenameLambda
            Feb 9 '17 at 8:43










          • I tried user = "root" pwd = "test" cmd = "date" . On executing subprocess.Popen("adb shell", shell=True).communicate("{}n{}n{}n".format(user,pwd,cmd)), I get C:ProjectMMCpython>python subprocess_login.py mdm9607 login: So it is asking to enter the credentials manually. But I want it to login and execute the command from the script.
            – wicked_snail
            Feb 9 '17 at 9:17










          • I tried the second option, but it hangs after execution. After 60 seconds, it exits from execution. Probably because the login timeout is 60 seconds. If I quit in middle , it gives: File "C:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
            – wicked_snail
            Feb 9 '17 at 9:43










          • I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
            – wicked_snail
            Feb 9 '17 at 10:56


















          up vote
          0
          down vote













          I was getting below error with process.communicate('command_to_sendn')



          TypeError: a bytes-like object is required, not 'str'


          Using the subprocess communicate command in below way resolved the TypeError:



          process.communicate(b'input keyevent KEYCODE_CALLn')





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42130761%2fexecute-adb-commands-in-cmd-prompt-from-python-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            Try subprocess.Popen:





            import subprocess

            cmd_input = """<enter login id e.g. root>
            <enter password e.g. test>
            date"""

            process = subprocess.Popen(
            "adb shell",
            shell=True,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE
            )
            process.communicate()
            for i in cmd_input.split("n"):
            process.communicate(i + "n")


            Or:



            process = subprocess.Popen(
            "adb shell",
            shell=True,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE
            )
            process.communicate(user + "n")
            process.communicate(pwd + "n")
            process.communicate(cmd + "n")


            Another option is to use google/python-adb or adb via pip






            share|improve this answer























            • Just to clarify, comments in <..> are only for representation of what I do manually. I tried cmd_input = "root test date" and then subprocess.call("adb shell", shell=True, input=cmd_input). But I am getting this error Traceback (most recent call last): File "subprocess_login.py", line 4, in <module> subprocess.call("adb shell", shell=True, input=cmd_input) File "C:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
              – wicked_snail
              Feb 9 '17 at 8:09










            • @wicked_snail I know, I just copied them over. input is my error, I looked at the Python3 documentation.
              – CodenameLambda
              Feb 9 '17 at 8:43










            • I tried user = "root" pwd = "test" cmd = "date" . On executing subprocess.Popen("adb shell", shell=True).communicate("{}n{}n{}n".format(user,pwd,cmd)), I get C:ProjectMMCpython>python subprocess_login.py mdm9607 login: So it is asking to enter the credentials manually. But I want it to login and execute the command from the script.
              – wicked_snail
              Feb 9 '17 at 9:17










            • I tried the second option, but it hangs after execution. After 60 seconds, it exits from execution. Probably because the login timeout is 60 seconds. If I quit in middle , it gives: File "C:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
              – wicked_snail
              Feb 9 '17 at 9:43










            • I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
              – wicked_snail
              Feb 9 '17 at 10:56















            up vote
            1
            down vote













            Try subprocess.Popen:





            import subprocess

            cmd_input = """<enter login id e.g. root>
            <enter password e.g. test>
            date"""

            process = subprocess.Popen(
            "adb shell",
            shell=True,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE
            )
            process.communicate()
            for i in cmd_input.split("n"):
            process.communicate(i + "n")


            Or:



            process = subprocess.Popen(
            "adb shell",
            shell=True,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE
            )
            process.communicate(user + "n")
            process.communicate(pwd + "n")
            process.communicate(cmd + "n")


            Another option is to use google/python-adb or adb via pip






            share|improve this answer























            • Just to clarify, comments in <..> are only for representation of what I do manually. I tried cmd_input = "root test date" and then subprocess.call("adb shell", shell=True, input=cmd_input). But I am getting this error Traceback (most recent call last): File "subprocess_login.py", line 4, in <module> subprocess.call("adb shell", shell=True, input=cmd_input) File "C:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
              – wicked_snail
              Feb 9 '17 at 8:09










            • @wicked_snail I know, I just copied them over. input is my error, I looked at the Python3 documentation.
              – CodenameLambda
              Feb 9 '17 at 8:43










            • I tried user = "root" pwd = "test" cmd = "date" . On executing subprocess.Popen("adb shell", shell=True).communicate("{}n{}n{}n".format(user,pwd,cmd)), I get C:ProjectMMCpython>python subprocess_login.py mdm9607 login: So it is asking to enter the credentials manually. But I want it to login and execute the command from the script.
              – wicked_snail
              Feb 9 '17 at 9:17










            • I tried the second option, but it hangs after execution. After 60 seconds, it exits from execution. Probably because the login timeout is 60 seconds. If I quit in middle , it gives: File "C:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
              – wicked_snail
              Feb 9 '17 at 9:43










            • I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
              – wicked_snail
              Feb 9 '17 at 10:56













            up vote
            1
            down vote










            up vote
            1
            down vote









            Try subprocess.Popen:





            import subprocess

            cmd_input = """<enter login id e.g. root>
            <enter password e.g. test>
            date"""

            process = subprocess.Popen(
            "adb shell",
            shell=True,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE
            )
            process.communicate()
            for i in cmd_input.split("n"):
            process.communicate(i + "n")


            Or:



            process = subprocess.Popen(
            "adb shell",
            shell=True,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE
            )
            process.communicate(user + "n")
            process.communicate(pwd + "n")
            process.communicate(cmd + "n")


            Another option is to use google/python-adb or adb via pip






            share|improve this answer














            Try subprocess.Popen:





            import subprocess

            cmd_input = """<enter login id e.g. root>
            <enter password e.g. test>
            date"""

            process = subprocess.Popen(
            "adb shell",
            shell=True,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE
            )
            process.communicate()
            for i in cmd_input.split("n"):
            process.communicate(i + "n")


            Or:



            process = subprocess.Popen(
            "adb shell",
            shell=True,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE
            )
            process.communicate(user + "n")
            process.communicate(pwd + "n")
            process.communicate(cmd + "n")


            Another option is to use google/python-adb or adb via pip







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 9 '17 at 11:23

























            answered Feb 9 '17 at 7:55









            CodenameLambda

            1,116520




            1,116520












            • Just to clarify, comments in <..> are only for representation of what I do manually. I tried cmd_input = "root test date" and then subprocess.call("adb shell", shell=True, input=cmd_input). But I am getting this error Traceback (most recent call last): File "subprocess_login.py", line 4, in <module> subprocess.call("adb shell", shell=True, input=cmd_input) File "C:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
              – wicked_snail
              Feb 9 '17 at 8:09










            • @wicked_snail I know, I just copied them over. input is my error, I looked at the Python3 documentation.
              – CodenameLambda
              Feb 9 '17 at 8:43










            • I tried user = "root" pwd = "test" cmd = "date" . On executing subprocess.Popen("adb shell", shell=True).communicate("{}n{}n{}n".format(user,pwd,cmd)), I get C:ProjectMMCpython>python subprocess_login.py mdm9607 login: So it is asking to enter the credentials manually. But I want it to login and execute the command from the script.
              – wicked_snail
              Feb 9 '17 at 9:17










            • I tried the second option, but it hangs after execution. After 60 seconds, it exits from execution. Probably because the login timeout is 60 seconds. If I quit in middle , it gives: File "C:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
              – wicked_snail
              Feb 9 '17 at 9:43










            • I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
              – wicked_snail
              Feb 9 '17 at 10:56


















            • Just to clarify, comments in <..> are only for representation of what I do manually. I tried cmd_input = "root test date" and then subprocess.call("adb shell", shell=True, input=cmd_input). But I am getting this error Traceback (most recent call last): File "subprocess_login.py", line 4, in <module> subprocess.call("adb shell", shell=True, input=cmd_input) File "C:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
              – wicked_snail
              Feb 9 '17 at 8:09










            • @wicked_snail I know, I just copied them over. input is my error, I looked at the Python3 documentation.
              – CodenameLambda
              Feb 9 '17 at 8:43










            • I tried user = "root" pwd = "test" cmd = "date" . On executing subprocess.Popen("adb shell", shell=True).communicate("{}n{}n{}n".format(user,pwd,cmd)), I get C:ProjectMMCpython>python subprocess_login.py mdm9607 login: So it is asking to enter the credentials manually. But I want it to login and execute the command from the script.
              – wicked_snail
              Feb 9 '17 at 9:17










            • I tried the second option, but it hangs after execution. After 60 seconds, it exits from execution. Probably because the login timeout is 60 seconds. If I quit in middle , it gives: File "C:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
              – wicked_snail
              Feb 9 '17 at 9:43










            • I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
              – wicked_snail
              Feb 9 '17 at 10:56
















            Just to clarify, comments in <..> are only for representation of what I do manually. I tried cmd_input = "root test date" and then subprocess.call("adb shell", shell=True, input=cmd_input). But I am getting this error Traceback (most recent call last): File "subprocess_login.py", line 4, in <module> subprocess.call("adb shell", shell=True, input=cmd_input) File "C:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
            – wicked_snail
            Feb 9 '17 at 8:09




            Just to clarify, comments in <..> are only for representation of what I do manually. I tried cmd_input = "root test date" and then subprocess.call("adb shell", shell=True, input=cmd_input). But I am getting this error Traceback (most recent call last): File "subprocess_login.py", line 4, in <module> subprocess.call("adb shell", shell=True, input=cmd_input) File "C:Python27libsubprocess.py", line 524, in call return Popen(*popenargs, **kwargs).wait() TypeError: __init__() got an unexpected keyword argument 'input'
            – wicked_snail
            Feb 9 '17 at 8:09












            @wicked_snail I know, I just copied them over. input is my error, I looked at the Python3 documentation.
            – CodenameLambda
            Feb 9 '17 at 8:43




            @wicked_snail I know, I just copied them over. input is my error, I looked at the Python3 documentation.
            – CodenameLambda
            Feb 9 '17 at 8:43












            I tried user = "root" pwd = "test" cmd = "date" . On executing subprocess.Popen("adb shell", shell=True).communicate("{}n{}n{}n".format(user,pwd,cmd)), I get C:ProjectMMCpython>python subprocess_login.py mdm9607 login: So it is asking to enter the credentials manually. But I want it to login and execute the command from the script.
            – wicked_snail
            Feb 9 '17 at 9:17




            I tried user = "root" pwd = "test" cmd = "date" . On executing subprocess.Popen("adb shell", shell=True).communicate("{}n{}n{}n".format(user,pwd,cmd)), I get C:ProjectMMCpython>python subprocess_login.py mdm9607 login: So it is asking to enter the credentials manually. But I want it to login and execute the command from the script.
            – wicked_snail
            Feb 9 '17 at 9:17












            I tried the second option, but it hangs after execution. After 60 seconds, it exits from execution. Probably because the login timeout is 60 seconds. If I quit in middle , it gives: File "C:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
            – wicked_snail
            Feb 9 '17 at 9:43




            I tried the second option, but it hangs after execution. After 60 seconds, it exits from execution. Probably because the login timeout is 60 seconds. If I quit in middle , it gives: File "C:Python27libsubprocess.py", line 1033, in _communicate stdout_thread.join() File "C:Python27libthreading.py", line 947, in join self.__block.wait() File "C:Python27libthreading.py", line 339, in wait waiter.acquire() KeyboardInterrupt
            – wicked_snail
            Feb 9 '17 at 9:43












            I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
            – wicked_snail
            Feb 9 '17 at 10:56




            I printed the stdout : 'rootrrntestrrndaterrnmdm9607 login: rrnLogin timed out after 60 seconds.rrn'
            – wicked_snail
            Feb 9 '17 at 10:56












            up vote
            0
            down vote













            I was getting below error with process.communicate('command_to_sendn')



            TypeError: a bytes-like object is required, not 'str'


            Using the subprocess communicate command in below way resolved the TypeError:



            process.communicate(b'input keyevent KEYCODE_CALLn')





            share|improve this answer

























              up vote
              0
              down vote













              I was getting below error with process.communicate('command_to_sendn')



              TypeError: a bytes-like object is required, not 'str'


              Using the subprocess communicate command in below way resolved the TypeError:



              process.communicate(b'input keyevent KEYCODE_CALLn')





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I was getting below error with process.communicate('command_to_sendn')



                TypeError: a bytes-like object is required, not 'str'


                Using the subprocess communicate command in below way resolved the TypeError:



                process.communicate(b'input keyevent KEYCODE_CALLn')





                share|improve this answer












                I was getting below error with process.communicate('command_to_sendn')



                TypeError: a bytes-like object is required, not 'str'


                Using the subprocess communicate command in below way resolved the TypeError:



                process.communicate(b'input keyevent KEYCODE_CALLn')






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 7 at 11:13









                nAQ

                36549




                36549






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42130761%2fexecute-adb-commands-in-cmd-prompt-from-python-script%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()