[SOLVED] Can I call a local function from ssh?

Issue

I know I can call variables from the local shell in an SSH session, so I assumed I could call a function this way too. But this doesn’t seem to work.

I have a script that checks if my local machine can handle certain operations and if it can I need to call local functions, if the local machine cannot I need to SSH into a remote machine and call the local functions.

canHandle() {
  if [ -d /this_must_exist/ ]
  then
     return 0
  else
     return 1
  fi
}

localFunction() {
  // Predefined tasks
}

canHandle
if [ $? -eq 0 ]
then
   // Process locally using predefined functions
   localFunction
else
   ssh -t [email protected] << ENDSSH
   // Process remotely calling predefined local functions
   localFunction
   ENDSSH
fi 

Solution

Imho this will not work.

Local Variables get expanded on your Local Shell, and are Transferred expanded.

You could write a bash script doing the same as local function, and write a function to copy it via scp to the remote system, execute it there via shh and delete it afterwards.

Answered By – MyChaOS

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *