Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script: Run function from script over ssh

Tags:

linux

bash

shell

sh

Is there any clever way to run a local Bash function on a remote host over ssh?

For example:

#!/bin/bash #Definition of the function f () {  ls -l; }  #I want to use the function locally f  #Execution of the function on the remote machine. ssh user@host f  #Reuse of the same function on another machine. ssh user@host2 f 

Yeah, I know it doesn't work, but is there a way to achieve this?

like image 798
Mr.Eddart Avatar asked Feb 28 '14 23:02

Mr.Eddart


People also ask

Can you run a script through SSH?

Try running ssh user@remote sh ./script. unx . This only works if the script is in the default (home) directory on the remote.

How do I run a script on a remote host?

To run a script on one or many remote computers, use the FilePath parameter of the Invoke-Command cmdlet. The script must be on or accessible to your local computer. The results are returned to your local computer.

What is ssh command in Linux?

SSH: Execute Remote Command or Script – Linux. This is quite a common task for Linux system administrators, when it is needed to execute some command or a local Bash script from a one Linux workstation or a server on another remote Linux machine over SSH.

How do I run SSH commands on a remote server?

SSH: Execute Remote Command. Execute a remote command on a host over SSH: $ ssh USER@HOST 'COMMAND' Examples. Get the uptime of the remote server: $ ssh [email protected] 'uptime' Reboot the remote server: $ ssh [email protected] 'reboot' SSH: Run Multiple Remote Commands

Why can't I run commands over SSH?

The problem with running commands over SSH is that generally you either have to type them yourself or upload a script file. However, with a bit of bash knowledge, you can pass entire scripts over SSH without having the .sh file on the remote machine. Sorry, the video player failed to load. (Error Code: 100013)

How do I run a script on a remote machine?

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. If you want to send all the functions defined within the script, not just myfn, just use typeset -f like so:


2 Answers

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.

like image 76
alvits Avatar answered Sep 22 '22 12:09

alvits


I personally don't know the correct answer to your question, but I have a lot of installation scripts that just copy themselves over using ssh.

Have the command copy the file over, load the file functions, run the file functions, and then delete the file.

ssh user@host "scp user@otherhost:/myFile ; . myFile ; f ; rm Myfile" 
like image 37
user2836202 Avatar answered Sep 19 '22 12:09

user2836202