Make the ssh connection and enter the sudo password fully automatically












2















I want to make ssh connection automatically and install a packet to the connected machine. I'm able to process the SSH connection automatically. I can even run commands that do not require sudo authorization. But I didn't find a way to automatically enter the password in the commands that require sudo authorization. How do you think I can automatically enter the sudo password?



asd.sh



/usr/bin/expect -c 'spawn ssh -t usr@ip bash "pwd; sudo apt-get update"; expect "password:"; send "12345r"; interact;'


asd.sh output



spawn ssh -t usr@ip bash pwd; sudo apt-get update
usr@ip's password:
/bin/pwd: /bin/pwd: cannot execute binary file
[sudo] password for usr:









share|improve this question


















  • 1





    "cannot execute binary file" is unrelated to your question. The command runs just fine, but fails for external reasons.

    – tripleee
    Nov 16 '18 at 7:16











  • I know, I'm getting the print I want after I enter the sudo password. But I want to enter the sudo password automatically with the script and the result can be seen directly in the terminal. @tripleee

    – Ali.Turkkan
    Nov 16 '18 at 7:27








  • 1





    take a look at sexpect with which you can write Expect scripts with shell code only.

    – pynexj
    Nov 16 '18 at 7:51
















2















I want to make ssh connection automatically and install a packet to the connected machine. I'm able to process the SSH connection automatically. I can even run commands that do not require sudo authorization. But I didn't find a way to automatically enter the password in the commands that require sudo authorization. How do you think I can automatically enter the sudo password?



asd.sh



/usr/bin/expect -c 'spawn ssh -t usr@ip bash "pwd; sudo apt-get update"; expect "password:"; send "12345r"; interact;'


asd.sh output



spawn ssh -t usr@ip bash pwd; sudo apt-get update
usr@ip's password:
/bin/pwd: /bin/pwd: cannot execute binary file
[sudo] password for usr:









share|improve this question


















  • 1





    "cannot execute binary file" is unrelated to your question. The command runs just fine, but fails for external reasons.

    – tripleee
    Nov 16 '18 at 7:16











  • I know, I'm getting the print I want after I enter the sudo password. But I want to enter the sudo password automatically with the script and the result can be seen directly in the terminal. @tripleee

    – Ali.Turkkan
    Nov 16 '18 at 7:27








  • 1





    take a look at sexpect with which you can write Expect scripts with shell code only.

    – pynexj
    Nov 16 '18 at 7:51














2












2








2








I want to make ssh connection automatically and install a packet to the connected machine. I'm able to process the SSH connection automatically. I can even run commands that do not require sudo authorization. But I didn't find a way to automatically enter the password in the commands that require sudo authorization. How do you think I can automatically enter the sudo password?



asd.sh



/usr/bin/expect -c 'spawn ssh -t usr@ip bash "pwd; sudo apt-get update"; expect "password:"; send "12345r"; interact;'


asd.sh output



spawn ssh -t usr@ip bash pwd; sudo apt-get update
usr@ip's password:
/bin/pwd: /bin/pwd: cannot execute binary file
[sudo] password for usr:









share|improve this question














I want to make ssh connection automatically and install a packet to the connected machine. I'm able to process the SSH connection automatically. I can even run commands that do not require sudo authorization. But I didn't find a way to automatically enter the password in the commands that require sudo authorization. How do you think I can automatically enter the sudo password?



asd.sh



/usr/bin/expect -c 'spawn ssh -t usr@ip bash "pwd; sudo apt-get update"; expect "password:"; send "12345r"; interact;'


asd.sh output



spawn ssh -t usr@ip bash pwd; sudo apt-get update
usr@ip's password:
/bin/pwd: /bin/pwd: cannot execute binary file
[sudo] password for usr:






bash expect openssh pwd






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 7:07









Ali.TurkkanAli.Turkkan

1147




1147








  • 1





    "cannot execute binary file" is unrelated to your question. The command runs just fine, but fails for external reasons.

    – tripleee
    Nov 16 '18 at 7:16











  • I know, I'm getting the print I want after I enter the sudo password. But I want to enter the sudo password automatically with the script and the result can be seen directly in the terminal. @tripleee

    – Ali.Turkkan
    Nov 16 '18 at 7:27








  • 1





    take a look at sexpect with which you can write Expect scripts with shell code only.

    – pynexj
    Nov 16 '18 at 7:51














  • 1





    "cannot execute binary file" is unrelated to your question. The command runs just fine, but fails for external reasons.

    – tripleee
    Nov 16 '18 at 7:16











  • I know, I'm getting the print I want after I enter the sudo password. But I want to enter the sudo password automatically with the script and the result can be seen directly in the terminal. @tripleee

    – Ali.Turkkan
    Nov 16 '18 at 7:27








  • 1





    take a look at sexpect with which you can write Expect scripts with shell code only.

    – pynexj
    Nov 16 '18 at 7:51








1




1





"cannot execute binary file" is unrelated to your question. The command runs just fine, but fails for external reasons.

– tripleee
Nov 16 '18 at 7:16





"cannot execute binary file" is unrelated to your question. The command runs just fine, but fails for external reasons.

– tripleee
Nov 16 '18 at 7:16













I know, I'm getting the print I want after I enter the sudo password. But I want to enter the sudo password automatically with the script and the result can be seen directly in the terminal. @tripleee

– Ali.Turkkan
Nov 16 '18 at 7:27







I know, I'm getting the print I want after I enter the sudo password. But I want to enter the sudo password automatically with the script and the result can be seen directly in the terminal. @tripleee

– Ali.Turkkan
Nov 16 '18 at 7:27






1




1





take a look at sexpect with which you can write Expect scripts with shell code only.

– pynexj
Nov 16 '18 at 7:51





take a look at sexpect with which you can write Expect scripts with shell code only.

– pynexj
Nov 16 '18 at 7:51












1 Answer
1






active

oldest

votes


















3














You need the -c argument to pass a command string to Bash. Also, try to have the pattern match the full line. Try with:



/usr/bin/expect -c 'spawn ssh -t usr@ip bash -c "pwd; sudo apt-get update"; expect "*password:"; send "12345r"; interact;'
^^ ^


Note that for this kind of task, Ansible can be very helpful as it will take care of all the boilerplate related to SSH and SUDO, and offers high-level modules to carry on any task easily.



The Ansible script ('playbook') would look like this (untested):



- hosts: ip
tasks:
- name: Update and upgrade apt packages
become: true
apt:
upgrade: yes


You can store the SUDO password in a file, and that file can be encrypted.






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',
    autoActivateHeartbeat: false,
    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%2f53333012%2fmake-the-ssh-connection-and-enter-the-sudo-password-fully-automatically%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You need the -c argument to pass a command string to Bash. Also, try to have the pattern match the full line. Try with:



    /usr/bin/expect -c 'spawn ssh -t usr@ip bash -c "pwd; sudo apt-get update"; expect "*password:"; send "12345r"; interact;'
    ^^ ^


    Note that for this kind of task, Ansible can be very helpful as it will take care of all the boilerplate related to SSH and SUDO, and offers high-level modules to carry on any task easily.



    The Ansible script ('playbook') would look like this (untested):



    - hosts: ip
    tasks:
    - name: Update and upgrade apt packages
    become: true
    apt:
    upgrade: yes


    You can store the SUDO password in a file, and that file can be encrypted.






    share|improve this answer






























      3














      You need the -c argument to pass a command string to Bash. Also, try to have the pattern match the full line. Try with:



      /usr/bin/expect -c 'spawn ssh -t usr@ip bash -c "pwd; sudo apt-get update"; expect "*password:"; send "12345r"; interact;'
      ^^ ^


      Note that for this kind of task, Ansible can be very helpful as it will take care of all the boilerplate related to SSH and SUDO, and offers high-level modules to carry on any task easily.



      The Ansible script ('playbook') would look like this (untested):



      - hosts: ip
      tasks:
      - name: Update and upgrade apt packages
      become: true
      apt:
      upgrade: yes


      You can store the SUDO password in a file, and that file can be encrypted.






      share|improve this answer




























        3












        3








        3







        You need the -c argument to pass a command string to Bash. Also, try to have the pattern match the full line. Try with:



        /usr/bin/expect -c 'spawn ssh -t usr@ip bash -c "pwd; sudo apt-get update"; expect "*password:"; send "12345r"; interact;'
        ^^ ^


        Note that for this kind of task, Ansible can be very helpful as it will take care of all the boilerplate related to SSH and SUDO, and offers high-level modules to carry on any task easily.



        The Ansible script ('playbook') would look like this (untested):



        - hosts: ip
        tasks:
        - name: Update and upgrade apt packages
        become: true
        apt:
        upgrade: yes


        You can store the SUDO password in a file, and that file can be encrypted.






        share|improve this answer















        You need the -c argument to pass a command string to Bash. Also, try to have the pattern match the full line. Try with:



        /usr/bin/expect -c 'spawn ssh -t usr@ip bash -c "pwd; sudo apt-get update"; expect "*password:"; send "12345r"; interact;'
        ^^ ^


        Note that for this kind of task, Ansible can be very helpful as it will take care of all the boilerplate related to SSH and SUDO, and offers high-level modules to carry on any task easily.



        The Ansible script ('playbook') would look like this (untested):



        - hosts: ip
        tasks:
        - name: Update and upgrade apt packages
        become: true
        apt:
        upgrade: yes


        You can store the SUDO password in a file, and that file can be encrypted.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 7:41

























        answered Nov 16 '18 at 7:27









        damienfrancoisdamienfrancois

        26.4k54864




        26.4k54864
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333012%2fmake-the-ssh-connection-and-enter-the-sudo-password-fully-automatically%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







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python