SSH with su and remote command using -c and running multiple commands with parameters

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












Here is my command with IP's commented out with semanticIP's



ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP 
su -c "export HISTCONTROL=ignorespace;
iptables -t nat -A PREROUTING -p tcp --dport 3389 -j DNAT --to-destination localRDP_IP:3389;
iptables -t nat -A POSTROUTING -p tcp -d localRDP_IP --dport 3389 -j SNAT --to-source jumpIP";


basically, I'm trying to run some remote routing, which is not the question. The question is how do I run such a command?



The best test I've been able to do is:



ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP -t "su -c nano; nano"


but I don't know how to do the spaces. If I have spaces in my commands in the -c "quoted area" other than a single command, I get an error.



Note: I realize that with ssh port forwarding, iptables commands may be unnecessary.










share|improve this question



























    up vote
    1
    down vote

    favorite












    Here is my command with IP's commented out with semanticIP's



    ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP 
    su -c "export HISTCONTROL=ignorespace;
    iptables -t nat -A PREROUTING -p tcp --dport 3389 -j DNAT --to-destination localRDP_IP:3389;
    iptables -t nat -A POSTROUTING -p tcp -d localRDP_IP --dport 3389 -j SNAT --to-source jumpIP";


    basically, I'm trying to run some remote routing, which is not the question. The question is how do I run such a command?



    The best test I've been able to do is:



    ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP -t "su -c nano; nano"


    but I don't know how to do the spaces. If I have spaces in my commands in the -c "quoted area" other than a single command, I get an error.



    Note: I realize that with ssh port forwarding, iptables commands may be unnecessary.










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Here is my command with IP's commented out with semanticIP's



      ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP 
      su -c "export HISTCONTROL=ignorespace;
      iptables -t nat -A PREROUTING -p tcp --dport 3389 -j DNAT --to-destination localRDP_IP:3389;
      iptables -t nat -A POSTROUTING -p tcp -d localRDP_IP --dport 3389 -j SNAT --to-source jumpIP";


      basically, I'm trying to run some remote routing, which is not the question. The question is how do I run such a command?



      The best test I've been able to do is:



      ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP -t "su -c nano; nano"


      but I don't know how to do the spaces. If I have spaces in my commands in the -c "quoted area" other than a single command, I get an error.



      Note: I realize that with ssh port forwarding, iptables commands may be unnecessary.










      share|improve this question















      Here is my command with IP's commented out with semanticIP's



      ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP 
      su -c "export HISTCONTROL=ignorespace;
      iptables -t nat -A PREROUTING -p tcp --dport 3389 -j DNAT --to-destination localRDP_IP:3389;
      iptables -t nat -A POSTROUTING -p tcp -d localRDP_IP --dport 3389 -j SNAT --to-source jumpIP";


      basically, I'm trying to run some remote routing, which is not the question. The question is how do I run such a command?



      The best test I've been able to do is:



      ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP -t "su -c nano; nano"


      but I don't know how to do the spaces. If I have spaces in my commands in the -c "quoted area" other than a single command, I get an error.



      Note: I realize that with ssh port forwarding, iptables commands may be unnecessary.







      ssh su






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 8 '17 at 17:35

























      asked Oct 8 '17 at 14:41









      thistleknot

      12815




      12815




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          As shown in the man page for su (man su) the -c option takes a single shell command, passed to your current shell for execution by its -c command:




          -c, --command COMMAND Specify a command that will be invoked by the shell using its -c.




          And then from bash:




          If the -c option is present, then commands are read from the first non-option argument _command_string_. If there are arguments after the _command_string_, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters.




          The upshot of this is that you cannot use -c with multiple commands.



          However, there is nothing stopping you providing a shell as the command to be run by -c and using that to handle the multiple commands:



          ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP su -c bash -c '
          export HISTCONTROL=ignorespace;
          iptables -t nat -A PREROUTING -p tcp --dport 3389 -j DNAT --to-destination localRDP_IP:3389;
          iptables -t nat -A POSTROUTING -p tcp -d localRDP_IP --dport 3389 -j SNAT --to-source jumpIP
          '


          I'm not entirely sure what you want to achieve with your second command, though. At the moment it's set up to run nano as the root user, and then to run it again as your own account. Is this what you wanted? Or did you want to run nano twice as the root user, like this:



          ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP -t su -c bash -c 'nano; nano'





          share|improve this answer




















          • The second was supplied as an example of what I could accomplish w multiple commands (no options). Your bash example might be the answer I was looking for! Thank you!
            – thistleknot
            Oct 9 '17 at 1:41

















          up vote
          0
          down vote













          psql (and by extension mysql) had a similar limitation. While reading documentation on psql, I came across this



          -c command
          --command=command
          Specifies that psql is to execute one command string, command, and then exit. This is useful in shell scripts. Start-up files (psqlrc and ~/.psqlrc) are ignored with this option.



          command must be either a command string that is completely parsable by the server (i.e., it contains no psql-specific features), or a single backslash command. Thus you cannot mix SQL and psql meta-commands with this option. To achieve that, you could pipe the string into psql, for example: echo 'x SELECT * FROM foo;' | psql. ( is the separator meta-command.)



          If the command string contains multiple SQL commands, they are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands included in the string to divide it into multiple transactions. This is different from the behavior when the same string is fed to psql's standard input. Also, only the result of the last SQL command is returned.



          Because of these legacy behaviors, putting more than one command in the -c string often has unexpected results. It's better to feed multiple commands to psql's standard input, either using echo as illustrated above, or via a shell here-document, for example:



          psql <<EOF
          x
          SELECT * FROM foo;
          EOF


          for my case, I merely modified my echo statement



          echo drop database if exists somedb; create database somedb;drop table if exists ur_table; CREATE TABLE ur_table (timestamp date, open real, high real,low real,close real,adjusted_close real,volume real,dividend_amount real,split_coefficient real,CONSTRAINT timestamp_pkey PRIMARY KEY (timestamp)); COPY ur_table(timestamp,open,high,low,close,adjusted_close,volume,dividend_amount,split_coefficient) FROM 'c:testtemp.csv' DELIMITER ',' CSV HEADER;| psql -U postgres





          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            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: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f396843%2fssh-with-su-and-remote-command-using-c-and-running-multiple-commands-with-param%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            As shown in the man page for su (man su) the -c option takes a single shell command, passed to your current shell for execution by its -c command:




            -c, --command COMMAND Specify a command that will be invoked by the shell using its -c.




            And then from bash:




            If the -c option is present, then commands are read from the first non-option argument _command_string_. If there are arguments after the _command_string_, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters.




            The upshot of this is that you cannot use -c with multiple commands.



            However, there is nothing stopping you providing a shell as the command to be run by -c and using that to handle the multiple commands:



            ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP su -c bash -c '
            export HISTCONTROL=ignorespace;
            iptables -t nat -A PREROUTING -p tcp --dport 3389 -j DNAT --to-destination localRDP_IP:3389;
            iptables -t nat -A POSTROUTING -p tcp -d localRDP_IP --dport 3389 -j SNAT --to-source jumpIP
            '


            I'm not entirely sure what you want to achieve with your second command, though. At the moment it's set up to run nano as the root user, and then to run it again as your own account. Is this what you wanted? Or did you want to run nano twice as the root user, like this:



            ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP -t su -c bash -c 'nano; nano'





            share|improve this answer




















            • The second was supplied as an example of what I could accomplish w multiple commands (no options). Your bash example might be the answer I was looking for! Thank you!
              – thistleknot
              Oct 9 '17 at 1:41














            up vote
            1
            down vote













            As shown in the man page for su (man su) the -c option takes a single shell command, passed to your current shell for execution by its -c command:




            -c, --command COMMAND Specify a command that will be invoked by the shell using its -c.




            And then from bash:




            If the -c option is present, then commands are read from the first non-option argument _command_string_. If there are arguments after the _command_string_, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters.




            The upshot of this is that you cannot use -c with multiple commands.



            However, there is nothing stopping you providing a shell as the command to be run by -c and using that to handle the multiple commands:



            ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP su -c bash -c '
            export HISTCONTROL=ignorespace;
            iptables -t nat -A PREROUTING -p tcp --dport 3389 -j DNAT --to-destination localRDP_IP:3389;
            iptables -t nat -A POSTROUTING -p tcp -d localRDP_IP --dport 3389 -j SNAT --to-source jumpIP
            '


            I'm not entirely sure what you want to achieve with your second command, though. At the moment it's set up to run nano as the root user, and then to run it again as your own account. Is this what you wanted? Or did you want to run nano twice as the root user, like this:



            ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP -t su -c bash -c 'nano; nano'





            share|improve this answer




















            • The second was supplied as an example of what I could accomplish w multiple commands (no options). Your bash example might be the answer I was looking for! Thank you!
              – thistleknot
              Oct 9 '17 at 1:41












            up vote
            1
            down vote










            up vote
            1
            down vote









            As shown in the man page for su (man su) the -c option takes a single shell command, passed to your current shell for execution by its -c command:




            -c, --command COMMAND Specify a command that will be invoked by the shell using its -c.




            And then from bash:




            If the -c option is present, then commands are read from the first non-option argument _command_string_. If there are arguments after the _command_string_, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters.




            The upshot of this is that you cannot use -c with multiple commands.



            However, there is nothing stopping you providing a shell as the command to be run by -c and using that to handle the multiple commands:



            ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP su -c bash -c '
            export HISTCONTROL=ignorespace;
            iptables -t nat -A PREROUTING -p tcp --dport 3389 -j DNAT --to-destination localRDP_IP:3389;
            iptables -t nat -A POSTROUTING -p tcp -d localRDP_IP --dport 3389 -j SNAT --to-source jumpIP
            '


            I'm not entirely sure what you want to achieve with your second command, though. At the moment it's set up to run nano as the root user, and then to run it again as your own account. Is this what you wanted? Or did you want to run nano twice as the root user, like this:



            ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP -t su -c bash -c 'nano; nano'





            share|improve this answer












            As shown in the man page for su (man su) the -c option takes a single shell command, passed to your current shell for execution by its -c command:




            -c, --command COMMAND Specify a command that will be invoked by the shell using its -c.




            And then from bash:




            If the -c option is present, then commands are read from the first non-option argument _command_string_. If there are arguments after the _command_string_, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters.




            The upshot of this is that you cannot use -c with multiple commands.



            However, there is nothing stopping you providing a shell as the command to be run by -c and using that to handle the multiple commands:



            ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP su -c bash -c '
            export HISTCONTROL=ignorespace;
            iptables -t nat -A PREROUTING -p tcp --dport 3389 -j DNAT --to-destination localRDP_IP:3389;
            iptables -t nat -A POSTROUTING -p tcp -d localRDP_IP --dport 3389 -j SNAT --to-source jumpIP
            '


            I'm not entirely sure what you want to achieve with your second command, though. At the moment it's set up to run nano as the root user, and then to run it again as your own account. Is this what you wanted? Or did you want to run nano twice as the root user, like this:



            ssh -p 2022 -L 9389:localRDPIP:3389 user@publicIP -t su -c bash -c 'nano; nano'






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 8 '17 at 18:17









            roaima

            40.1k547110




            40.1k547110











            • The second was supplied as an example of what I could accomplish w multiple commands (no options). Your bash example might be the answer I was looking for! Thank you!
              – thistleknot
              Oct 9 '17 at 1:41
















            • The second was supplied as an example of what I could accomplish w multiple commands (no options). Your bash example might be the answer I was looking for! Thank you!
              – thistleknot
              Oct 9 '17 at 1:41















            The second was supplied as an example of what I could accomplish w multiple commands (no options). Your bash example might be the answer I was looking for! Thank you!
            – thistleknot
            Oct 9 '17 at 1:41




            The second was supplied as an example of what I could accomplish w multiple commands (no options). Your bash example might be the answer I was looking for! Thank you!
            – thistleknot
            Oct 9 '17 at 1:41












            up vote
            0
            down vote













            psql (and by extension mysql) had a similar limitation. While reading documentation on psql, I came across this



            -c command
            --command=command
            Specifies that psql is to execute one command string, command, and then exit. This is useful in shell scripts. Start-up files (psqlrc and ~/.psqlrc) are ignored with this option.



            command must be either a command string that is completely parsable by the server (i.e., it contains no psql-specific features), or a single backslash command. Thus you cannot mix SQL and psql meta-commands with this option. To achieve that, you could pipe the string into psql, for example: echo 'x SELECT * FROM foo;' | psql. ( is the separator meta-command.)



            If the command string contains multiple SQL commands, they are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands included in the string to divide it into multiple transactions. This is different from the behavior when the same string is fed to psql's standard input. Also, only the result of the last SQL command is returned.



            Because of these legacy behaviors, putting more than one command in the -c string often has unexpected results. It's better to feed multiple commands to psql's standard input, either using echo as illustrated above, or via a shell here-document, for example:



            psql <<EOF
            x
            SELECT * FROM foo;
            EOF


            for my case, I merely modified my echo statement



            echo drop database if exists somedb; create database somedb;drop table if exists ur_table; CREATE TABLE ur_table (timestamp date, open real, high real,low real,close real,adjusted_close real,volume real,dividend_amount real,split_coefficient real,CONSTRAINT timestamp_pkey PRIMARY KEY (timestamp)); COPY ur_table(timestamp,open,high,low,close,adjusted_close,volume,dividend_amount,split_coefficient) FROM 'c:testtemp.csv' DELIMITER ',' CSV HEADER;| psql -U postgres





            share|improve this answer
























              up vote
              0
              down vote













              psql (and by extension mysql) had a similar limitation. While reading documentation on psql, I came across this



              -c command
              --command=command
              Specifies that psql is to execute one command string, command, and then exit. This is useful in shell scripts. Start-up files (psqlrc and ~/.psqlrc) are ignored with this option.



              command must be either a command string that is completely parsable by the server (i.e., it contains no psql-specific features), or a single backslash command. Thus you cannot mix SQL and psql meta-commands with this option. To achieve that, you could pipe the string into psql, for example: echo 'x SELECT * FROM foo;' | psql. ( is the separator meta-command.)



              If the command string contains multiple SQL commands, they are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands included in the string to divide it into multiple transactions. This is different from the behavior when the same string is fed to psql's standard input. Also, only the result of the last SQL command is returned.



              Because of these legacy behaviors, putting more than one command in the -c string often has unexpected results. It's better to feed multiple commands to psql's standard input, either using echo as illustrated above, or via a shell here-document, for example:



              psql <<EOF
              x
              SELECT * FROM foo;
              EOF


              for my case, I merely modified my echo statement



              echo drop database if exists somedb; create database somedb;drop table if exists ur_table; CREATE TABLE ur_table (timestamp date, open real, high real,low real,close real,adjusted_close real,volume real,dividend_amount real,split_coefficient real,CONSTRAINT timestamp_pkey PRIMARY KEY (timestamp)); COPY ur_table(timestamp,open,high,low,close,adjusted_close,volume,dividend_amount,split_coefficient) FROM 'c:testtemp.csv' DELIMITER ',' CSV HEADER;| psql -U postgres





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                psql (and by extension mysql) had a similar limitation. While reading documentation on psql, I came across this



                -c command
                --command=command
                Specifies that psql is to execute one command string, command, and then exit. This is useful in shell scripts. Start-up files (psqlrc and ~/.psqlrc) are ignored with this option.



                command must be either a command string that is completely parsable by the server (i.e., it contains no psql-specific features), or a single backslash command. Thus you cannot mix SQL and psql meta-commands with this option. To achieve that, you could pipe the string into psql, for example: echo 'x SELECT * FROM foo;' | psql. ( is the separator meta-command.)



                If the command string contains multiple SQL commands, they are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands included in the string to divide it into multiple transactions. This is different from the behavior when the same string is fed to psql's standard input. Also, only the result of the last SQL command is returned.



                Because of these legacy behaviors, putting more than one command in the -c string often has unexpected results. It's better to feed multiple commands to psql's standard input, either using echo as illustrated above, or via a shell here-document, for example:



                psql <<EOF
                x
                SELECT * FROM foo;
                EOF


                for my case, I merely modified my echo statement



                echo drop database if exists somedb; create database somedb;drop table if exists ur_table; CREATE TABLE ur_table (timestamp date, open real, high real,low real,close real,adjusted_close real,volume real,dividend_amount real,split_coefficient real,CONSTRAINT timestamp_pkey PRIMARY KEY (timestamp)); COPY ur_table(timestamp,open,high,low,close,adjusted_close,volume,dividend_amount,split_coefficient) FROM 'c:testtemp.csv' DELIMITER ',' CSV HEADER;| psql -U postgres





                share|improve this answer












                psql (and by extension mysql) had a similar limitation. While reading documentation on psql, I came across this



                -c command
                --command=command
                Specifies that psql is to execute one command string, command, and then exit. This is useful in shell scripts. Start-up files (psqlrc and ~/.psqlrc) are ignored with this option.



                command must be either a command string that is completely parsable by the server (i.e., it contains no psql-specific features), or a single backslash command. Thus you cannot mix SQL and psql meta-commands with this option. To achieve that, you could pipe the string into psql, for example: echo 'x SELECT * FROM foo;' | psql. ( is the separator meta-command.)



                If the command string contains multiple SQL commands, they are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands included in the string to divide it into multiple transactions. This is different from the behavior when the same string is fed to psql's standard input. Also, only the result of the last SQL command is returned.



                Because of these legacy behaviors, putting more than one command in the -c string often has unexpected results. It's better to feed multiple commands to psql's standard input, either using echo as illustrated above, or via a shell here-document, for example:



                psql <<EOF
                x
                SELECT * FROM foo;
                EOF


                for my case, I merely modified my echo statement



                echo drop database if exists somedb; create database somedb;drop table if exists ur_table; CREATE TABLE ur_table (timestamp date, open real, high real,low real,close real,adjusted_close real,volume real,dividend_amount real,split_coefficient real,CONSTRAINT timestamp_pkey PRIMARY KEY (timestamp)); COPY ur_table(timestamp,open,high,low,close,adjusted_close,volume,dividend_amount,split_coefficient) FROM 'c:testtemp.csv' DELIMITER ',' CSV HEADER;| psql -U postgres






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 17 at 7:57









                thistleknot

                12815




                12815



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f396843%2fssh-with-su-and-remote-command-using-c-and-running-multiple-commands-with-param%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay