execute which command over ssh in python script











up vote
2
down vote

favorite












I'm trying to run the command which solsql over SSH in a Python script.



I think the problem is in the ssh command and not the Python part, but maybe it's both.



I tried



subprocess.check_output("ssh root@IP which solsql",
stderr=subprocess.STDOUT, shell=True)


but I get an error.



I tried to run the command manually:



ssh root@{server_IP}" which solsql"


and I get a different output.



On the server I get the real path (/opt/solidDB/soliddb-6.5/bin/solsql)
but over SSH I get this:



which: no solsql in
(/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin)









share|improve this question




















  • 1




    What error do you get?
    – MihanEntalpo
    Nov 8 at 10:51






  • 2




    Where is /opt/solidDB/soliddb-6.5/bin added to the path? Probably in .bash_profile (which is sourced for login shells), not in .bashrc, which is sourced for non-login-shells (such as the one started if you start ssh with a command).
    – L3viathan
    Nov 8 at 10:57










  • @MihanEntalpo the error is: subprocess.CalledProcessError: Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1
    – Ortal Turgeman
    Nov 8 at 11:54










  • Hey Ortal, if my answer helped and worked could accept the answer so others can see the answer is the right way to go.
    – Jack Herer
    Nov 8 at 14:47










  • @JackHerer the answers helped me understand the reason to the problem so i chose to go in a different way, your solution seems useful but i didn't try it, i was hoping for something simpler
    – Ortal Turgeman
    Nov 11 at 12:51

















up vote
2
down vote

favorite












I'm trying to run the command which solsql over SSH in a Python script.



I think the problem is in the ssh command and not the Python part, but maybe it's both.



I tried



subprocess.check_output("ssh root@IP which solsql",
stderr=subprocess.STDOUT, shell=True)


but I get an error.



I tried to run the command manually:



ssh root@{server_IP}" which solsql"


and I get a different output.



On the server I get the real path (/opt/solidDB/soliddb-6.5/bin/solsql)
but over SSH I get this:



which: no solsql in
(/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin)









share|improve this question




















  • 1




    What error do you get?
    – MihanEntalpo
    Nov 8 at 10:51






  • 2




    Where is /opt/solidDB/soliddb-6.5/bin added to the path? Probably in .bash_profile (which is sourced for login shells), not in .bashrc, which is sourced for non-login-shells (such as the one started if you start ssh with a command).
    – L3viathan
    Nov 8 at 10:57










  • @MihanEntalpo the error is: subprocess.CalledProcessError: Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1
    – Ortal Turgeman
    Nov 8 at 11:54










  • Hey Ortal, if my answer helped and worked could accept the answer so others can see the answer is the right way to go.
    – Jack Herer
    Nov 8 at 14:47










  • @JackHerer the answers helped me understand the reason to the problem so i chose to go in a different way, your solution seems useful but i didn't try it, i was hoping for something simpler
    – Ortal Turgeman
    Nov 11 at 12:51















up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm trying to run the command which solsql over SSH in a Python script.



I think the problem is in the ssh command and not the Python part, but maybe it's both.



I tried



subprocess.check_output("ssh root@IP which solsql",
stderr=subprocess.STDOUT, shell=True)


but I get an error.



I tried to run the command manually:



ssh root@{server_IP}" which solsql"


and I get a different output.



On the server I get the real path (/opt/solidDB/soliddb-6.5/bin/solsql)
but over SSH I get this:



which: no solsql in
(/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin)









share|improve this question















I'm trying to run the command which solsql over SSH in a Python script.



I think the problem is in the ssh command and not the Python part, but maybe it's both.



I tried



subprocess.check_output("ssh root@IP which solsql",
stderr=subprocess.STDOUT, shell=True)


but I get an error.



I tried to run the command manually:



ssh root@{server_IP}" which solsql"


and I get a different output.



On the server I get the real path (/opt/solidDB/soliddb-6.5/bin/solsql)
but over SSH I get this:



which: no solsql in
(/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin)






python ssh which






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 12:45









tripleee

86.6k12121175




86.6k12121175










asked Nov 8 at 10:48









Ortal Turgeman

6118




6118








  • 1




    What error do you get?
    – MihanEntalpo
    Nov 8 at 10:51






  • 2




    Where is /opt/solidDB/soliddb-6.5/bin added to the path? Probably in .bash_profile (which is sourced for login shells), not in .bashrc, which is sourced for non-login-shells (such as the one started if you start ssh with a command).
    – L3viathan
    Nov 8 at 10:57










  • @MihanEntalpo the error is: subprocess.CalledProcessError: Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1
    – Ortal Turgeman
    Nov 8 at 11:54










  • Hey Ortal, if my answer helped and worked could accept the answer so others can see the answer is the right way to go.
    – Jack Herer
    Nov 8 at 14:47










  • @JackHerer the answers helped me understand the reason to the problem so i chose to go in a different way, your solution seems useful but i didn't try it, i was hoping for something simpler
    – Ortal Turgeman
    Nov 11 at 12:51
















  • 1




    What error do you get?
    – MihanEntalpo
    Nov 8 at 10:51






  • 2




    Where is /opt/solidDB/soliddb-6.5/bin added to the path? Probably in .bash_profile (which is sourced for login shells), not in .bashrc, which is sourced for non-login-shells (such as the one started if you start ssh with a command).
    – L3viathan
    Nov 8 at 10:57










  • @MihanEntalpo the error is: subprocess.CalledProcessError: Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1
    – Ortal Turgeman
    Nov 8 at 11:54










  • Hey Ortal, if my answer helped and worked could accept the answer so others can see the answer is the right way to go.
    – Jack Herer
    Nov 8 at 14:47










  • @JackHerer the answers helped me understand the reason to the problem so i chose to go in a different way, your solution seems useful but i didn't try it, i was hoping for something simpler
    – Ortal Turgeman
    Nov 11 at 12:51










1




1




What error do you get?
– MihanEntalpo
Nov 8 at 10:51




What error do you get?
– MihanEntalpo
Nov 8 at 10:51




2




2




Where is /opt/solidDB/soliddb-6.5/bin added to the path? Probably in .bash_profile (which is sourced for login shells), not in .bashrc, which is sourced for non-login-shells (such as the one started if you start ssh with a command).
– L3viathan
Nov 8 at 10:57




Where is /opt/solidDB/soliddb-6.5/bin added to the path? Probably in .bash_profile (which is sourced for login shells), not in .bashrc, which is sourced for non-login-shells (such as the one started if you start ssh with a command).
– L3viathan
Nov 8 at 10:57












@MihanEntalpo the error is: subprocess.CalledProcessError: Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1
– Ortal Turgeman
Nov 8 at 11:54




@MihanEntalpo the error is: subprocess.CalledProcessError: Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1
– Ortal Turgeman
Nov 8 at 11:54












Hey Ortal, if my answer helped and worked could accept the answer so others can see the answer is the right way to go.
– Jack Herer
Nov 8 at 14:47




Hey Ortal, if my answer helped and worked could accept the answer so others can see the answer is the right way to go.
– Jack Herer
Nov 8 at 14:47












@JackHerer the answers helped me understand the reason to the problem so i chose to go in a different way, your solution seems useful but i didn't try it, i was hoping for something simpler
– Ortal Turgeman
Nov 11 at 12:51






@JackHerer the answers helped me understand the reason to the problem so i chose to go in a different way, your solution seems useful but i didn't try it, i was hoping for something simpler
– Ortal Turgeman
Nov 11 at 12:51














3 Answers
3






active

oldest

votes

















up vote
3
down vote













I think what your looking for is something like paramiko. An example of how to use the library and issue a command to the remote system.



import base64
import paramiko
key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))
client = paramiko.SSHClient()
client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
client.connect('ssh.example.com', username='THE_USER', password='THE_PASSWORD')
stdin, stdout, stderr = client.exec_command('which solsql')
for line in stdout:
print('... ' + line.strip('n'))
client.close()





share|improve this answer




























    up vote
    1
    down vote













    When you run a command over SSH, your shell executes a different set of startup files than when you connect interactively to the server. So the fundamental problem is really that the path where this tool is installed is not in your PATH when you connect via ssh from a script.



    A common but crude workaround is to force the shell to read in the file with the PATH definition you want; but of course that basically requires you to know at least where the correct PATH is set, so you might as well just figure out where exactly the tool is installed in the first place anyway.



    ssh server '. .bashrc; type -all solsql'


    (assuming that the PATH is set up in your .bashrc; and ignoring for the time being the difference between executing stuff as yourself and as root. The dot and space before .bashrc are quite significant. Notice also how we use the POSIX command type rather than the brittle which command which should have died a natural but horrible death decades ago).



    If you have a good idea of where the tool might be installed, perhaps instead do



    subprocess.check_output(['ssh', 'root@' + ip, '''
    for path in /opt/solidDB/*/bin /usr/local/bin /usr/bin; do
    test -x "$path/solsql" || continue
    echo "$path"
    exit 0
    done
    exit 1'''])


    Notice how we also avoid the (here, useless) shell=True. Perhaps see also Actual meaning of 'shell=True' in subprocess






    share|improve this answer























    • i know the meaning of shell=True, i just don't like when it's false because the syntax gets more complicated and more potential for errors
      – Ortal Turgeman
      Nov 8 at 14:02










    • Actually it's arguably exactly the other way around. More levels of quoting means more stuff you have to unroll inside your head or just guess. Anyway, the argument to ssh is effectively being passed through to a (remote) shell, you're just not making Python run a shell to pass that string to the shell run by ssh on the remote host; so the argument of syntactic convenience is moot, if not outright false. (Maybe do read the linked question after all; my answer there expands a bit on this.)
      – tripleee
      Nov 8 at 17:20


















    up vote
    0
    down vote













    First, you need to debug your error.



    Use the code like this:



    command = "ssh root@IP which solsql"

    try:
    retult = subprocess.check_output(command,shell=True,stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
    raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

    print ("Result:", result)


    It will output error message to you, and you'll know what to do, for example, ssh could have asked for a password, or didn't find your key, or something else.






    share|improve this answer

















    • 1




      Catching the exception so you can reraise it seems rather pointless. The real problem is how to avoid the error, i.e. find out where the command is actually installed.
      – tripleee
      Nov 8 at 13:02












    • I've tried to help questioner to detect the cause of a problem. With this new exception he could see output of command, that he didn't seen before, because of not so detailed exception message (Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1). It's better to get the method of solving such problems: debug it the right way, instead of give some one-off solution, and wait while new question arise.
      – MihanEntalpo
      Nov 9 at 6:00










    • The traceback you get reveals exactly this information; you are just rephrasing it.
      – tripleee
      Nov 9 at 6:04










    • The traceback DOES NOT reveals exactly this infromation. If you look closer, you'll see, that in my example, e.output is added to the exception text body and it doesn't get outputted in a traceback by default. Try it yourself. Just call subprocess.check_output("ssh noone@nowhere",shell=True,stderr=subprocess.STDOUT), and tell me, what is the problem with the command except of "returned non-zero exit status"
      – MihanEntalpo
      Nov 9 at 12:06










    • But in my example, exception would be: command 'ssh noone@nowhere' return with error (code 255): b'Pseudo-terminal will not be allocated because stdin is not a terminal.rnssh: Could not resolve hostname nowhere: Name or service not knownrn'. See? "Could not resolve hostname ....". It's much more informative, when "returned non-zero exit status" and could be easily solved.
      – MihanEntalpo
      Nov 9 at 12:07











    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%2f53206146%2fexecute-which-command-over-ssh-in-python-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    I think what your looking for is something like paramiko. An example of how to use the library and issue a command to the remote system.



    import base64
    import paramiko
    key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))
    client = paramiko.SSHClient()
    client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
    client.connect('ssh.example.com', username='THE_USER', password='THE_PASSWORD')
    stdin, stdout, stderr = client.exec_command('which solsql')
    for line in stdout:
    print('... ' + line.strip('n'))
    client.close()





    share|improve this answer

























      up vote
      3
      down vote













      I think what your looking for is something like paramiko. An example of how to use the library and issue a command to the remote system.



      import base64
      import paramiko
      key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))
      client = paramiko.SSHClient()
      client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
      client.connect('ssh.example.com', username='THE_USER', password='THE_PASSWORD')
      stdin, stdout, stderr = client.exec_command('which solsql')
      for line in stdout:
      print('... ' + line.strip('n'))
      client.close()





      share|improve this answer























        up vote
        3
        down vote










        up vote
        3
        down vote









        I think what your looking for is something like paramiko. An example of how to use the library and issue a command to the remote system.



        import base64
        import paramiko
        key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))
        client = paramiko.SSHClient()
        client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
        client.connect('ssh.example.com', username='THE_USER', password='THE_PASSWORD')
        stdin, stdout, stderr = client.exec_command('which solsql')
        for line in stdout:
        print('... ' + line.strip('n'))
        client.close()





        share|improve this answer












        I think what your looking for is something like paramiko. An example of how to use the library and issue a command to the remote system.



        import base64
        import paramiko
        key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))
        client = paramiko.SSHClient()
        client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
        client.connect('ssh.example.com', username='THE_USER', password='THE_PASSWORD')
        stdin, stdout, stderr = client.exec_command('which solsql')
        for line in stdout:
        print('... ' + line.strip('n'))
        client.close()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 11:40









        Jack Herer

        16310




        16310
























            up vote
            1
            down vote













            When you run a command over SSH, your shell executes a different set of startup files than when you connect interactively to the server. So the fundamental problem is really that the path where this tool is installed is not in your PATH when you connect via ssh from a script.



            A common but crude workaround is to force the shell to read in the file with the PATH definition you want; but of course that basically requires you to know at least where the correct PATH is set, so you might as well just figure out where exactly the tool is installed in the first place anyway.



            ssh server '. .bashrc; type -all solsql'


            (assuming that the PATH is set up in your .bashrc; and ignoring for the time being the difference between executing stuff as yourself and as root. The dot and space before .bashrc are quite significant. Notice also how we use the POSIX command type rather than the brittle which command which should have died a natural but horrible death decades ago).



            If you have a good idea of where the tool might be installed, perhaps instead do



            subprocess.check_output(['ssh', 'root@' + ip, '''
            for path in /opt/solidDB/*/bin /usr/local/bin /usr/bin; do
            test -x "$path/solsql" || continue
            echo "$path"
            exit 0
            done
            exit 1'''])


            Notice how we also avoid the (here, useless) shell=True. Perhaps see also Actual meaning of 'shell=True' in subprocess






            share|improve this answer























            • i know the meaning of shell=True, i just don't like when it's false because the syntax gets more complicated and more potential for errors
              – Ortal Turgeman
              Nov 8 at 14:02










            • Actually it's arguably exactly the other way around. More levels of quoting means more stuff you have to unroll inside your head or just guess. Anyway, the argument to ssh is effectively being passed through to a (remote) shell, you're just not making Python run a shell to pass that string to the shell run by ssh on the remote host; so the argument of syntactic convenience is moot, if not outright false. (Maybe do read the linked question after all; my answer there expands a bit on this.)
              – tripleee
              Nov 8 at 17:20















            up vote
            1
            down vote













            When you run a command over SSH, your shell executes a different set of startup files than when you connect interactively to the server. So the fundamental problem is really that the path where this tool is installed is not in your PATH when you connect via ssh from a script.



            A common but crude workaround is to force the shell to read in the file with the PATH definition you want; but of course that basically requires you to know at least where the correct PATH is set, so you might as well just figure out where exactly the tool is installed in the first place anyway.



            ssh server '. .bashrc; type -all solsql'


            (assuming that the PATH is set up in your .bashrc; and ignoring for the time being the difference between executing stuff as yourself and as root. The dot and space before .bashrc are quite significant. Notice also how we use the POSIX command type rather than the brittle which command which should have died a natural but horrible death decades ago).



            If you have a good idea of where the tool might be installed, perhaps instead do



            subprocess.check_output(['ssh', 'root@' + ip, '''
            for path in /opt/solidDB/*/bin /usr/local/bin /usr/bin; do
            test -x "$path/solsql" || continue
            echo "$path"
            exit 0
            done
            exit 1'''])


            Notice how we also avoid the (here, useless) shell=True. Perhaps see also Actual meaning of 'shell=True' in subprocess






            share|improve this answer























            • i know the meaning of shell=True, i just don't like when it's false because the syntax gets more complicated and more potential for errors
              – Ortal Turgeman
              Nov 8 at 14:02










            • Actually it's arguably exactly the other way around. More levels of quoting means more stuff you have to unroll inside your head or just guess. Anyway, the argument to ssh is effectively being passed through to a (remote) shell, you're just not making Python run a shell to pass that string to the shell run by ssh on the remote host; so the argument of syntactic convenience is moot, if not outright false. (Maybe do read the linked question after all; my answer there expands a bit on this.)
              – tripleee
              Nov 8 at 17:20













            up vote
            1
            down vote










            up vote
            1
            down vote









            When you run a command over SSH, your shell executes a different set of startup files than when you connect interactively to the server. So the fundamental problem is really that the path where this tool is installed is not in your PATH when you connect via ssh from a script.



            A common but crude workaround is to force the shell to read in the file with the PATH definition you want; but of course that basically requires you to know at least where the correct PATH is set, so you might as well just figure out where exactly the tool is installed in the first place anyway.



            ssh server '. .bashrc; type -all solsql'


            (assuming that the PATH is set up in your .bashrc; and ignoring for the time being the difference between executing stuff as yourself and as root. The dot and space before .bashrc are quite significant. Notice also how we use the POSIX command type rather than the brittle which command which should have died a natural but horrible death decades ago).



            If you have a good idea of where the tool might be installed, perhaps instead do



            subprocess.check_output(['ssh', 'root@' + ip, '''
            for path in /opt/solidDB/*/bin /usr/local/bin /usr/bin; do
            test -x "$path/solsql" || continue
            echo "$path"
            exit 0
            done
            exit 1'''])


            Notice how we also avoid the (here, useless) shell=True. Perhaps see also Actual meaning of 'shell=True' in subprocess






            share|improve this answer














            When you run a command over SSH, your shell executes a different set of startup files than when you connect interactively to the server. So the fundamental problem is really that the path where this tool is installed is not in your PATH when you connect via ssh from a script.



            A common but crude workaround is to force the shell to read in the file with the PATH definition you want; but of course that basically requires you to know at least where the correct PATH is set, so you might as well just figure out where exactly the tool is installed in the first place anyway.



            ssh server '. .bashrc; type -all solsql'


            (assuming that the PATH is set up in your .bashrc; and ignoring for the time being the difference between executing stuff as yourself and as root. The dot and space before .bashrc are quite significant. Notice also how we use the POSIX command type rather than the brittle which command which should have died a natural but horrible death decades ago).



            If you have a good idea of where the tool might be installed, perhaps instead do



            subprocess.check_output(['ssh', 'root@' + ip, '''
            for path in /opt/solidDB/*/bin /usr/local/bin /usr/bin; do
            test -x "$path/solsql" || continue
            echo "$path"
            exit 0
            done
            exit 1'''])


            Notice how we also avoid the (here, useless) shell=True. Perhaps see also Actual meaning of 'shell=True' in subprocess







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 8 at 17:24

























            answered Nov 8 at 12:58









            tripleee

            86.6k12121175




            86.6k12121175












            • i know the meaning of shell=True, i just don't like when it's false because the syntax gets more complicated and more potential for errors
              – Ortal Turgeman
              Nov 8 at 14:02










            • Actually it's arguably exactly the other way around. More levels of quoting means more stuff you have to unroll inside your head or just guess. Anyway, the argument to ssh is effectively being passed through to a (remote) shell, you're just not making Python run a shell to pass that string to the shell run by ssh on the remote host; so the argument of syntactic convenience is moot, if not outright false. (Maybe do read the linked question after all; my answer there expands a bit on this.)
              – tripleee
              Nov 8 at 17:20


















            • i know the meaning of shell=True, i just don't like when it's false because the syntax gets more complicated and more potential for errors
              – Ortal Turgeman
              Nov 8 at 14:02










            • Actually it's arguably exactly the other way around. More levels of quoting means more stuff you have to unroll inside your head or just guess. Anyway, the argument to ssh is effectively being passed through to a (remote) shell, you're just not making Python run a shell to pass that string to the shell run by ssh on the remote host; so the argument of syntactic convenience is moot, if not outright false. (Maybe do read the linked question after all; my answer there expands a bit on this.)
              – tripleee
              Nov 8 at 17:20
















            i know the meaning of shell=True, i just don't like when it's false because the syntax gets more complicated and more potential for errors
            – Ortal Turgeman
            Nov 8 at 14:02




            i know the meaning of shell=True, i just don't like when it's false because the syntax gets more complicated and more potential for errors
            – Ortal Turgeman
            Nov 8 at 14:02












            Actually it's arguably exactly the other way around. More levels of quoting means more stuff you have to unroll inside your head or just guess. Anyway, the argument to ssh is effectively being passed through to a (remote) shell, you're just not making Python run a shell to pass that string to the shell run by ssh on the remote host; so the argument of syntactic convenience is moot, if not outright false. (Maybe do read the linked question after all; my answer there expands a bit on this.)
            – tripleee
            Nov 8 at 17:20




            Actually it's arguably exactly the other way around. More levels of quoting means more stuff you have to unroll inside your head or just guess. Anyway, the argument to ssh is effectively being passed through to a (remote) shell, you're just not making Python run a shell to pass that string to the shell run by ssh on the remote host; so the argument of syntactic convenience is moot, if not outright false. (Maybe do read the linked question after all; my answer there expands a bit on this.)
            – tripleee
            Nov 8 at 17:20










            up vote
            0
            down vote













            First, you need to debug your error.



            Use the code like this:



            command = "ssh root@IP which solsql"

            try:
            retult = subprocess.check_output(command,shell=True,stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
            raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

            print ("Result:", result)


            It will output error message to you, and you'll know what to do, for example, ssh could have asked for a password, or didn't find your key, or something else.






            share|improve this answer

















            • 1




              Catching the exception so you can reraise it seems rather pointless. The real problem is how to avoid the error, i.e. find out where the command is actually installed.
              – tripleee
              Nov 8 at 13:02












            • I've tried to help questioner to detect the cause of a problem. With this new exception he could see output of command, that he didn't seen before, because of not so detailed exception message (Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1). It's better to get the method of solving such problems: debug it the right way, instead of give some one-off solution, and wait while new question arise.
              – MihanEntalpo
              Nov 9 at 6:00










            • The traceback you get reveals exactly this information; you are just rephrasing it.
              – tripleee
              Nov 9 at 6:04










            • The traceback DOES NOT reveals exactly this infromation. If you look closer, you'll see, that in my example, e.output is added to the exception text body and it doesn't get outputted in a traceback by default. Try it yourself. Just call subprocess.check_output("ssh noone@nowhere",shell=True,stderr=subprocess.STDOUT), and tell me, what is the problem with the command except of "returned non-zero exit status"
              – MihanEntalpo
              Nov 9 at 12:06










            • But in my example, exception would be: command 'ssh noone@nowhere' return with error (code 255): b'Pseudo-terminal will not be allocated because stdin is not a terminal.rnssh: Could not resolve hostname nowhere: Name or service not knownrn'. See? "Could not resolve hostname ....". It's much more informative, when "returned non-zero exit status" and could be easily solved.
              – MihanEntalpo
              Nov 9 at 12:07















            up vote
            0
            down vote













            First, you need to debug your error.



            Use the code like this:



            command = "ssh root@IP which solsql"

            try:
            retult = subprocess.check_output(command,shell=True,stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
            raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

            print ("Result:", result)


            It will output error message to you, and you'll know what to do, for example, ssh could have asked for a password, or didn't find your key, or something else.






            share|improve this answer

















            • 1




              Catching the exception so you can reraise it seems rather pointless. The real problem is how to avoid the error, i.e. find out where the command is actually installed.
              – tripleee
              Nov 8 at 13:02












            • I've tried to help questioner to detect the cause of a problem. With this new exception he could see output of command, that he didn't seen before, because of not so detailed exception message (Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1). It's better to get the method of solving such problems: debug it the right way, instead of give some one-off solution, and wait while new question arise.
              – MihanEntalpo
              Nov 9 at 6:00










            • The traceback you get reveals exactly this information; you are just rephrasing it.
              – tripleee
              Nov 9 at 6:04










            • The traceback DOES NOT reveals exactly this infromation. If you look closer, you'll see, that in my example, e.output is added to the exception text body and it doesn't get outputted in a traceback by default. Try it yourself. Just call subprocess.check_output("ssh noone@nowhere",shell=True,stderr=subprocess.STDOUT), and tell me, what is the problem with the command except of "returned non-zero exit status"
              – MihanEntalpo
              Nov 9 at 12:06










            • But in my example, exception would be: command 'ssh noone@nowhere' return with error (code 255): b'Pseudo-terminal will not be allocated because stdin is not a terminal.rnssh: Could not resolve hostname nowhere: Name or service not knownrn'. See? "Could not resolve hostname ....". It's much more informative, when "returned non-zero exit status" and could be easily solved.
              – MihanEntalpo
              Nov 9 at 12:07













            up vote
            0
            down vote










            up vote
            0
            down vote









            First, you need to debug your error.



            Use the code like this:



            command = "ssh root@IP which solsql"

            try:
            retult = subprocess.check_output(command,shell=True,stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
            raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

            print ("Result:", result)


            It will output error message to you, and you'll know what to do, for example, ssh could have asked for a password, or didn't find your key, or something else.






            share|improve this answer












            First, you need to debug your error.



            Use the code like this:



            command = "ssh root@IP which solsql"

            try:
            retult = subprocess.check_output(command,shell=True,stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
            raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

            print ("Result:", result)


            It will output error message to you, and you'll know what to do, for example, ssh could have asked for a password, or didn't find your key, or something else.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 8 at 12:13









            MihanEntalpo

            1,051918




            1,051918








            • 1




              Catching the exception so you can reraise it seems rather pointless. The real problem is how to avoid the error, i.e. find out where the command is actually installed.
              – tripleee
              Nov 8 at 13:02












            • I've tried to help questioner to detect the cause of a problem. With this new exception he could see output of command, that he didn't seen before, because of not so detailed exception message (Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1). It's better to get the method of solving such problems: debug it the right way, instead of give some one-off solution, and wait while new question arise.
              – MihanEntalpo
              Nov 9 at 6:00










            • The traceback you get reveals exactly this information; you are just rephrasing it.
              – tripleee
              Nov 9 at 6:04










            • The traceback DOES NOT reveals exactly this infromation. If you look closer, you'll see, that in my example, e.output is added to the exception text body and it doesn't get outputted in a traceback by default. Try it yourself. Just call subprocess.check_output("ssh noone@nowhere",shell=True,stderr=subprocess.STDOUT), and tell me, what is the problem with the command except of "returned non-zero exit status"
              – MihanEntalpo
              Nov 9 at 12:06










            • But in my example, exception would be: command 'ssh noone@nowhere' return with error (code 255): b'Pseudo-terminal will not be allocated because stdin is not a terminal.rnssh: Could not resolve hostname nowhere: Name or service not knownrn'. See? "Could not resolve hostname ....". It's much more informative, when "returned non-zero exit status" and could be easily solved.
              – MihanEntalpo
              Nov 9 at 12:07














            • 1




              Catching the exception so you can reraise it seems rather pointless. The real problem is how to avoid the error, i.e. find out where the command is actually installed.
              – tripleee
              Nov 8 at 13:02












            • I've tried to help questioner to detect the cause of a problem. With this new exception he could see output of command, that he didn't seen before, because of not so detailed exception message (Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1). It's better to get the method of solving such problems: debug it the right way, instead of give some one-off solution, and wait while new question arise.
              – MihanEntalpo
              Nov 9 at 6:00










            • The traceback you get reveals exactly this information; you are just rephrasing it.
              – tripleee
              Nov 9 at 6:04










            • The traceback DOES NOT reveals exactly this infromation. If you look closer, you'll see, that in my example, e.output is added to the exception text body and it doesn't get outputted in a traceback by default. Try it yourself. Just call subprocess.check_output("ssh noone@nowhere",shell=True,stderr=subprocess.STDOUT), and tell me, what is the problem with the command except of "returned non-zero exit status"
              – MihanEntalpo
              Nov 9 at 12:06










            • But in my example, exception would be: command 'ssh noone@nowhere' return with error (code 255): b'Pseudo-terminal will not be allocated because stdin is not a terminal.rnssh: Could not resolve hostname nowhere: Name or service not knownrn'. See? "Could not resolve hostname ....". It's much more informative, when "returned non-zero exit status" and could be easily solved.
              – MihanEntalpo
              Nov 9 at 12:07








            1




            1




            Catching the exception so you can reraise it seems rather pointless. The real problem is how to avoid the error, i.e. find out where the command is actually installed.
            – tripleee
            Nov 8 at 13:02






            Catching the exception so you can reraise it seems rather pointless. The real problem is how to avoid the error, i.e. find out where the command is actually installed.
            – tripleee
            Nov 8 at 13:02














            I've tried to help questioner to detect the cause of a problem. With this new exception he could see output of command, that he didn't seen before, because of not so detailed exception message (Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1). It's better to get the method of solving such problems: debug it the right way, instead of give some one-off solution, and wait while new question arise.
            – MihanEntalpo
            Nov 9 at 6:00




            I've tried to help questioner to detect the cause of a problem. With this new exception he could see output of command, that he didn't seen before, because of not so detailed exception message (Command 'ssh root@10.65.163.24 "which solsql"' returned non-zero exit status 1). It's better to get the method of solving such problems: debug it the right way, instead of give some one-off solution, and wait while new question arise.
            – MihanEntalpo
            Nov 9 at 6:00












            The traceback you get reveals exactly this information; you are just rephrasing it.
            – tripleee
            Nov 9 at 6:04




            The traceback you get reveals exactly this information; you are just rephrasing it.
            – tripleee
            Nov 9 at 6:04












            The traceback DOES NOT reveals exactly this infromation. If you look closer, you'll see, that in my example, e.output is added to the exception text body and it doesn't get outputted in a traceback by default. Try it yourself. Just call subprocess.check_output("ssh noone@nowhere",shell=True,stderr=subprocess.STDOUT), and tell me, what is the problem with the command except of "returned non-zero exit status"
            – MihanEntalpo
            Nov 9 at 12:06




            The traceback DOES NOT reveals exactly this infromation. If you look closer, you'll see, that in my example, e.output is added to the exception text body and it doesn't get outputted in a traceback by default. Try it yourself. Just call subprocess.check_output("ssh noone@nowhere",shell=True,stderr=subprocess.STDOUT), and tell me, what is the problem with the command except of "returned non-zero exit status"
            – MihanEntalpo
            Nov 9 at 12:06












            But in my example, exception would be: command 'ssh noone@nowhere' return with error (code 255): b'Pseudo-terminal will not be allocated because stdin is not a terminal.rnssh: Could not resolve hostname nowhere: Name or service not knownrn'. See? "Could not resolve hostname ....". It's much more informative, when "returned non-zero exit status" and could be easily solved.
            – MihanEntalpo
            Nov 9 at 12:07




            But in my example, exception would be: command 'ssh noone@nowhere' return with error (code 255): b'Pseudo-terminal will not be allocated because stdin is not a terminal.rnssh: Could not resolve hostname nowhere: Name or service not knownrn'. See? "Could not resolve hostname ....". It's much more informative, when "returned non-zero exit status" and could be easily solved.
            – MihanEntalpo
            Nov 9 at 12:07


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206146%2fexecute-which-command-over-ssh-in-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







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff