Shell Script: Run Function from Script Over Ssh

Shell script: Run function from script over ssh

You can use the typeset command to make your functions available on a remote machine via ssh. There are several options depending on how you want to run your remote script.

#!/bin/bash
# Define your function
myfn () { ls -l; }

To use the function on the remote hosts:

typeset -f myfn | ssh user@host "$(cat); myfn"
typeset -f myfn | ssh user@host2 "$(cat); myfn"

Better yet, why bother with pipe:

ssh user@host "$(typeset -f myfn); myfn"

Or you can use a HEREDOC:

ssh user@host << EOF
$(typeset -f myfn)
myfn
EOF

If you want to send all the functions defined within the script, not just myfn, just use typeset -f like so:

ssh user@host "$(typeset -f); myfn"

Explanation

typeset -f myfn will display the definition of myfn.

cat will receive the definition of the function as a text and $() will execute it in the current shell which will become a defined function in the remote shell. Finally the function can be executed.

The last code will put the definition of the functions inline before ssh execution.

How to run functions and local resources over SSH in a shell script

An easy approach (if your local side is Linux) is to use set -a to enable automatic export before your source command; copy /proc/self/environ on stdin; and parse it into a set of variables on the remote side.

Because BASHOPTS, EUID, etc. aren't environment variables, this avoids trying to modify them. (If you were complying with POSIX recommendations and using lowercase names for your own variables, you could even go as far as to ignore all-caps variables entirely).

set -a # enable export of all variables defined, **before** the source operation
source /tmp/settings.conf

import_env() {
while IFS= read -r -d '' item; do
printf -v "${item%%=*}" "%s" "${item#*=}" && export "$item"
done
}

cat /proc/self/environ | ssh user@host "$(declare -f); import_env; display_os_name"

Even easier is to just copy the file you want to source over the wire.

ssh user@host "$(declare -f); $(</tmp/settings.conf); display_os_name"

Call function from function into remote host through bash script is not working

Without seeing exactly what the generated script looks like, it's not really possible to troubleshoot this.

But I would instead break up your logic into a script which gets copied to the destination and executed there, and a simple script which does the copying and evaluation.

#!/bin/bash

script=$(cat <<\____HERE
install_prelibrary () {
# Notice also refactoring; comments below
if wget https://github.com/EOSIO/eos/releases/download/v2.0.0/eosio_2.0.0-1-ubuntu-18.04_amd64.deb --no-check-certificate > /dev/null 2>&1; then
: pass
else
rc=$?
# Write errors to standard error, exit with an actual failure code
printf "Error downloading Ubuntu Binary file\n" >&2
exit $rc
fi
install_cdt
create_wallet_and_keys
}

install_cdt(){
#some commands
}
create_wallet_and_keys(){
#some commands
}
____HERE
)
SCRIPT="$(cat ~/shell/config.sh); $script; install_prelibrary"
for i in ${!genesishost[*]} ; do
printf "\t=========== node ${genesishost[i]} ===========\n\n"
SCR=${SCRIPT/PASSWORD/"$password"}
sshpass -p "$password" ssh -l "$username" "${genesishost[i]}" "${SCR}"
done

If you need to evaluate the functions locally, too, it's not too hard to have the script read itself; or simply store the code in an external file and source that as well as reading it into a variable.

How to run a bash function() in a remote host? in Ubuntu

Resone for the error:

When you do

ssh ppuser@10.101.5.91 " keyConfig $1 $2 $4 "

You are actually trying to execute a command keyConfig on remote machine 10.101.5.91
Which is certainly not there:

2 Solution for the problem

1) Make a script on remotehost which contains keyConfig code with same name

OR

2) Execute following instead of function

ssh ppuser@10.101.5.91 "sed -i.bak -r "/^$1/s/([^']+')([^']+)('.*)/\1$2\3/" $3"

Please note you may have to add few escape depending on the sed syntax you are using



Related Topics



Leave a reply



Submit