linux 如何通过ssh执行带参数的远程命令?

tv6aics1  于 2022-11-28  发布在  Linux
关注(0)|答案(6)|浏览(363)

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;./test.sh "$1"
}

When using this command, just the cd command is executed on the remote host; the test.sh command is executed on the local host. This is because the semicolon separates two different commands: the ssh command and the test.sh command.
I tried defining the function as follows (note the single quotes):

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

I tried to keep the cd command and the test.sh 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

./test.sh $1

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

vmpqdwk3

vmpqdwk31#

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.

avwztpqn

avwztpqn2#

我正在使用以下命令从本地计算机在远程计算机上执行命令:

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

4xrmg8kj3#

这是一个在AWS云上运行的示例。场景是从自动缩放启动的某台机器需要在另一台服务器上执行某个操作,通过SSH传递新生成的示例DNS

# Get the public DNS of the current machine (AWS specific)
MY_DNS=`curl -s http://169.254.169.254/latest/meta-data/public-hostname`

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

EOF
kuhbmx9i

kuhbmx9i4#

恢复一个旧线程,但这个相当干净的方法没有列出。

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

eblbsuwk5#

给我一个小技巧,使用"bash-s",他们说他们允许位置参数,但显然0美元已经保留了无论什么原因...然后使用两次相同的参数岩石如下:

ssh user@host "bash -s" < ./start_app.sh -e test -e test -f docker-compose.services.yml
jm2pwxwz

jm2pwxwz6#

Solution: you want to be able connect to machine remotely using ssh protocol and trigger/run some actions outside.

on ssh use a -t flag, from documentation:
-t Force pseudo-terminal allocation.
This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g.when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

formula:

ssh -i <key-path> <user>@<remote-machine> -t '<action>'

Example: as administrator I want to be able to connect remotely into ec2 machines and trigger a revert process for a bad deployment on a several machines in a raw, moreover you better implement this action as an automation script that use ips as an arguments and running on different machines in parallel.

ssh -i /home/admin/.ssh/key admin@10.20.30.40 -t 'cd /home/application && make revert'

相关问题