How to export the function in ssh

Hi Gurus,

I have a requirment to use a function created in a script in another server within the script. Below is the sample script for reference

#/bin/bash

logs_gen () {
          XXXXXXXXXXX
          XXXXXXXXXXX
          XXXXXXXXXXX
}

for i in x y z; do # x y z are the server names
ssh -q ${i}<< EOF
logs_gen #Here it is giving errors while executing this function.. like command not found.
exit
EOF
done

Could anyone suggest on this how to export the variable pls?

Regards,
VRN

save the code below to a file such as remote_run.sh

#/bin/bash

logs_gen () {
          XXXXXXXXXXX
          XXXXXXXXXXX
          XXXXXXXXXXX
}

logs_gen 

then just run the commands below

for i in x y z; do # x y z are the server names
      ssh -q ${i} "bash -s" <  remote_run.sh
done

I tried the below , but it did not export it to entire session...

export -f fname

Pls suggest...

Thanks in advance..

---------- Post updated at 11:16 PM ---------- Previous update was at 11:14 PM ----------

what is the remote_run.sh?

Is that the script to execute the function?

Could you elaborate please?

---------- Post updated at 11:22 PM ---------- Previous update was at 11:16 PM ----------

Sorry... got your suggestion...

But can this not be possible within the script instead of creating another script?

It's impossible to invoke a local function over ssh, as the local function just not have been defined at the remote environment!

You have to do a workaround.

In the ssh command, copy your script to tmp & trigger from there.

It should be doable on the fly,but not nice :

ssh host "printf \"testing () {\n cp "'\$'1" "'\$'2" \n}\" > /tmp/func;. /tmp/func; testing /tmp/func /tmp/func.used"

thanks all for all of your updates on this.

Can it be possible to export the variable (not function) to the remoote location...?

For ex: I've defined a variable and exported it... can i use it after I do the ssh to another server also?

Could anyone let me know the way for this pls?

Regards,
VRN...

When you run ssh, you get a brand new, independent shell.

You set variables in it the exact same way you would locally: VARNAME="..."

You create functions in it the exact same way you would locally:

function_name
{
...
}

There's no "exporting" of anthing, it does exactly what you tell it to, no more, no less.

When you want to send big blocks of code over ssh, and make sure your code does exactly what it looks like, it can be useful to do something like this:

ssh username@host exec /bin/bash -s "$ARG1" "$ARG2" <<"EOF"
function_name
{
        echo "inside function"
}

echo "ARG1 was $1"
echo "ARG2 was $2"

function_name
EOF

The -s argument, followed by "$ARG1", "$ARG2", etc. lets you set the $1 $2 ... variables for code running on the remote server. (It works on any shell, not just bash.) Nothing else at all from the local side carries over, giving you precise control.

<<"EOF" instead of <<EOF prevents any expansion whatsoever from happening inside the here-document, guaranteeing that everything in it gets sent over raw, dollar-signs and quotes and whitespace and anything else purely unmangled.

An unfortunate side-effect of this is that your remote shell code won't be able to use read. Standard input has been redirected from the contents of the here-document, you can't read from the terminal.

1 Like