简体   繁体   中英

running function(s) on the remote host over SSH

I've a number functions in my scripts:

function chk_host() { ...... }
function run_test() { ...... }

which I want to run on the remote machine over ssh:

ADDR=${ADDR:-"10.192.11.22"}
ssh -T user@${ADDR} << EOF
  chk_host
  run_test
}

Obviously that doesn't work as chk_host or run_test is not a command and not available on the remote host. I suppose, I can do something like: ssh -T user@${ADDR} "$( declare -f run_test ); run_test << EOF but I have a number of those and eventually will be running as command-line parameters. What's the best way of doing it?

I guess, the safest way to reach the goal would be to upload a shell script with all necessary functions to a target host, and then running a command via shell, something like ssh user@${HOST} "source /path/to/uploaded_functions.sh; afunc <args>"

The prior uploading is useful (although probably not necessary) because evaluating all required functions right in command line may lead to hard problems with proper escaping all special characters and sequences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM