How to execute a remote command over ssh with arguments?

ssh remote command example
ssh execute remote command with password
jenkins run command on remote machine linux
jenkins execute shell script on remote host using ssh parameter
python script to ssh and run multiple commands
execute script on remote machine linux
execute windows command from linux ssh
ssh execute multiple commands

In my .bashrc I define a function which I can use on the command line later:

function mycommand() {
    ssh user@123.456.789.0 cd testdir;./ "$1"

When using this command, just the cd command is executed on the remote host; the command is executed on the local host. This is because the semicolon separates two different commands: the ssh command and the command.

I tried defining the function as follows (note the single quotes):

function mycommand() {
    ssh user@123.456.789.0 'cd testdir;./ "$1"'

I tried to keep the cd command and the command together, but the argument $1 is not resolved, independent of what I give to the function. It is always tried to execute a command

./ $1

on the remote host.

How do I properly define mycommand, so the script is executed on the remote host after changing into the directory testdir, with the ability to pass on the argument given to mycommand to

A few ways to execute commands remotely using SSH ·, sudo requires interactive shell, it can be enabled with -t parameter. ssh -t $HOST sudo ls /root [sudo] password for zaiste: Simple multi-line command. VAR1  Execute script. Remote execution is not only limited to the commands; we can even execute script over SSH. We just have to provide absolute path of local script to SSH command. Let us create a simple shell script with following contents and name it as #!/bin/sh uname hostname. Make script executable and run it on remote server as follows:

Reviving an old thread, but this pretty clean approach was not listed.

function mycommand() {
    ssh user@123.456.789.0 <<+
    cd testdir;./ "$1"

Execute remote script with local parameter value, myparameter=foobar ssh user@server "myscript $myparameter". This will execute myscript foobar on the server. For instance, you would like to add one user’s pubkey to a list of authorized keys on the servers. But the idea of connecting to all of those servers manually and executing the command over and over again makes you wanna throw up. Worry no more. There is a tool called PSSH which allows you to execute commands on multiple remote servers over SSH.

This is an example that works on the AWS Cloud. The scenario is that some machine that booted from autoscaling needs to perform some action on another server, passing the newly spawned instance DNS via SSH

# Get the public DNS of the current machine (AWS specific)
MY_DNS=`curl -s`

ssh \
    -o StrictHostKeyChecking=no \
    -i ~/.ssh/id_rsa \ \
<< EOF
cd ~/
echo "Hey I was just SSHed by ${MY_DNS}"
# Newline is important before final EOF!


running remote command with arguments and shell using ssh , sshpass can accept password – as an argument, read it from file or via environment variable. Let us discuss all these approaches. 1) Password  Cool Tip: Connect to a remote SSH server without typing a password! Configure a passwordless authentication! Only 3 easy steps! Read more → SSH: Execute Remote Command. Execute a remote command on a host over SSH: $ ssh USER@HOST 'COMMAND' Examples. Get the uptime of the remote server: $ ssh root@ 'uptime' Reboot the remote server:

I'm using the following to execute commands on the remote from my local computer:

ssh -i ~/.ssh/$GIT_PRIVKEY user@$IP "bash -s" < localpath/ $arg1 $arg2

How to Execute Linux Commands on Remote System over SSH, The problem is that with ssh, the supplied command string is (re)evaluated by the remote shell, and that turns what is meant to be a single  Using heredoc is probably the most convenient way to execute multi-line commands on a remote machine. Also, variables expansion works out-of-the-box. VAR1="boo" ssh -T $HOST << EOSSH ls pwd if true; then echo $VAR1 else echo "False" fi EOSSH If you need to assign variables within the heredoc block, put the opening heredoc in single quotes.

Running local script remotely (with arguments) « \1, If needed you can run SSH commands followed by immediately logging out of the remote system. The problem is that with ssh, the supplied command string is (re)evaluated by the remote shell, and that turns what is meant to be a single argument "arg2 with spaces" into three separate arguments. For the same reason, there may also be problems with other characters that are special to the shell like globbing characters, escapes and quotes.

SSH tip: Send commands remotely, Now i am trying to call it from local laptop using ssh command, my command is gets executed, but the remote system is complaining about the argument part. When command is specified, it is executed on the remote host/server instead of a login shell. The syntax is as follows for executing commands over ssh: ADVERTISEMENTS. [donotprint] [/donotprint] ssh user1@server1 command1. ssh user1@server1 'command2'. ssh user1@server1 'command1 | command2'. The ssh client will login to a server called server1, using user name called user1 and run a command call command1.

passing arguments in remote ssh command?, How to execute remote command, multiple commands or shell (Bash) script over SSH (Secure Shell). Examples of Execute a remote command on a host over SSH: ssh root@ 'bash -s' < " --argument". passing arguments in remote ssh command? i have a bash script on serverA, when i run the script on the server, it runs fine. the way to run the script is like this ./script "option1" Now i am trying to call it from local laptop using ssh command, my command is as the following

  • This is unrelated to the main point, but you might want to use && instead of the semi-colon to join the commands executed on the remote host: cd testdir && ./ "$1". With that form (because evaluation of && short-circuits in bash), if the cd fails the second command won't be executed, and you won't inadvertently run a different in user's homedir.
  • @JoSo Yes that's the basic of it so depending on usage the user can opt to sanitize the argument he needs it. With respect to basic ssh there's probably no better way - unless you do file transfers first, etc.
  • @JoSo Yes, a better approach would be to define a quote function like quote() { printf "'%q'" "$1"; }, and then do ssh user@host "cd testdir; ./ $(quote "$1")"
  • @augurar, almost, but %q results are self-quoting; surrounding them with literal quotes isn't necessary or correct. printf -v arg_str '%q ' "$1"; ssh user@host "cd testdir; ./ $arg_str" suffices.
  • @CharlesDuffy Why do you need to add space though?
  • @konsolebox, for just the immediate case, don't really, but that way the idiom scales if more arguments are added rather than smushing them all together.
  • This would fail if $1 would expand to a string having " and expandable characters like $ and `.
  • It doesn't "fail". It has the same effect as executing any non-ssh command with arguments. The OP wasn't asking for a lesson in double escaping arguments. Just how to get his second command to execute what [whatever] argument he was already intending.
  • If the user asks how to execute something over ssh, then it's reasonable to expect any answer to cover caveats specific to ssh (ie. which don't exist when running code locally). Double expansion (and shell injection vulnerabilities caused by same) are among such caveats which wouldn't exist with cd testdir; ./ "$1" run directly in a local shell, but do exist with the code as-given.
  • I don't imagine you'd upvote an answer to a database question that showcased a practice with SQL injection vulnerabilities, but the potential of someone running arbitrary remote code via mycommand '$(rm -rf ~)' is every bit as severe.
  • (...and the thing is, the changes needed to make your code safe are fairly small: printf -v argv_str '%q ' "$@"; ssh user@host "bash -s $argv_str" <<'+' and the rest can stay as-is; quoting the + sigil for the heredoc prevents its expansion).