ssh user@host <<'ENDSSH'#commands to run on remote hostENDSSH
这也可以与需要用户输入的su和命令一起使用。(注意'转义的heldoc)
由于这个答案不断获得流量,我想为这一美妙的使用增加更多信息:
您可以使用此语法嵌套命令,这是嵌套似乎有效的唯一方法(以理智的方式)
ssh user@host <<'ENDSSH'#commands to run on remote hostssh user@host2 <<'END2'# Another bunch of commands on another hostwall <<'ENDWALL'Error: Out of cheeseENDWALLftp ftp.example.com <<'ENDFTP'testtestlsENDFTPEND2ENDSSH
ssh user@host <<-'ENDSSH'#commands to run on remote hostssh user@host2 <<-'END2'# Another bunch of commands on another hostwall <<-'ENDWALL'Error: Out of cheeseENDWALLftp ftp.example.com <<-'ENDFTP'testtestlsENDFTPEND2ENDSSH
rem Open tunnel in the backgroundstart plink.exe -ssh [username]@[hostname] -L 3307:127.0.0.1:3306 -i "[SSHkey]" -N
rem Wait a second to let Plink establish the tunneltimeout /t 1
rem Run the task using the tunnel"C:\Program Files\R\R-3.2.1\bin\x64\R.exe" CMD BATCH qidash.R
rem Kill the tunneltaskkill /im plink.exe
somevar="spaces or other special characters"somevar2="!@#$%^"another_func() {mkdir -p "$1"}work() {another_func "$somevar"touch "$somevar"/"$somevar2"}ssh user@server 'bash -s' <<EOT$(declare -p somevar somevar2) # transfer variables values$(declare -f work another_func) # transfer function definitionswork # call the functionEOT