How to execute a remote command over ssh with arguments?
Do it this way instead:
function mycommand {
ssh user@123.456.789.0 "cd testdir;./test.sh \"$1\""
}
You still have to pass the whole command as a single string, yet in that single string you need to have $1
expanded before it is sent to ssh so you need to use ""
for it.
Update
Another proper way to do this actually is to use printf %q
to properly quote the argument. This would make the argument safe to parse even if it has spaces, single quotes, double quotes, or any other character that may have a special meaning to the shell:
function mycommand {
printf -v __ %q "$1"
ssh user@123.456.789.0 "cd testdir;./test.sh $__"
}
- When declaring a function with
function
,()
is not necessary. - Don't comment back about it just because you're a POSIXist.
Starting Bash version 4.4, it can also be simplified to this:
function mycommand {
ssh user@123.456.789.0 "cd testdir;./test.sh ${1@Q}"
}
See ${parameter@operator}
section in Shell Parameter Expansion.
Remote ssh command with arguments
The thing is the $
is expanded by the shell before it is passed to the ssh
command. You need to deprive it of its special meaning locally by escaping it before passing it to ssh
as
ssh -t login@machine watch "ps aux | awk '{print \$1}' | grep php-fpm | wc -l"
The error you are seeing is because when shell tries to expand $1
it does not find a value for it and leaves an empty string which results in incorrect number of arguments passed to awk
.
Also you could replace your shell pipeline containing awk
and grep
with just a simple logic
ssh -t login@machine watch "ps aux | awk '\$1 == \"php-fpm\"{count++}END{print count}'
How do I pass arguments inside a remote command using sshpass and ssh
Finally tried below syntax to append the variable into the command that I need to execute..
sshpass -p $pswd ssh $username@$hostname "aggr show; aggr create -aggregate agg1_"$cluster" -disklist 1.0.0,1.0.1,1.0.2,1.0.3,1.0.4,1.0.5,1.0.6,1.0.7 -simulate true"
What is the cleanest way to ssh and run multiple commands in Bash?
How about a Bash Here Document:
ssh otherhost << EOF
ls some_folder;
./someaction.sh 'some params'
pwd
./some_other_action 'other params'
EOF
To avoid the problems mentioned by @Globalz in the comments, you may be able to (depending what you're doing on the remote site) get away with replacing the first line with
ssh otherhost /bin/bash << EOF
Note that you can do variable substitution in the Here document, but you may have to deal with quoting issues. For instance, if you quote the "limit string" (ie. EOF
in the above), then you can't do variable substitutions. But without quoting the limit string, variables are substituted. For example, if you have defined $NAME
above in your shell script, you could do
ssh otherhost /bin/bash << EOF
touch "/tmp/${NAME}"
EOF
and it would create a file on the destination otherhost
with the name of whatever you'd assigned to $NAME
. Other rules about shell script quoting also apply, but are too complicated to go into here.
Executing multiple commands with ssh, including evaluation of environment variables on the remote machine
Just escape the $ using \.
ssh my_user@my_host "echo \$USER; echo \$HOSTNAME"
How do I get the result of a command from a remote server using ssh?
Simply assign the output of your script to a variable and later use that variable:
result="$(./yourscript)"
echo "Result from SSH = $result"
Related Topics
Get Ceiling Integer from Number in Linux (Bash)
Randomly Shuffling Lines in Linux/Bash
Centos 64 Bit Bad Elf Interpreter
Git Status Ignore Line Endings/Identical Files/Windows & Linux Environment/Dropbox/Meld
Pack Shared Libraries into the Elf
Secure Way to Run Other People Code (Sandbox) on My Server
Sending Udp Packets from the Linux Kernel
How to Get the Source Code for the Linux Utility Tail
Pthread Mutex Lock Unlock by Different Threads
Prevent Linux Thread from Being Interrupted by Scheduler
Self Modifying Code Always Segmentation Faults on Linux
How to Pass a Wildcard Parameter to a Bash File
How to Automatically Add User Account and Password with a Bash Script
How to Monitor the Thread Count of a Process on Linux
Linux: Command to Open Url in Default Browser
Is There an Invalid Pthread_T Id
What Is the Purpose of Map_Anonymous Flag in Mmap System Call