[bash] running a function remotely using ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash] running a function remotely using ssh
# 1  
Old 02-15-2017
[bash] running a function remotely using ssh

Hi all.

I need a bash script to run a function remotely. I think it should be similar to the following but can't figure out the exact syntax.

Here is the script:
Code:
#!/bin/bash

function nu ()
{
        for var in {0..5}; do printf "$var, "; done; echo
}

ssh host "$(typeset -f); nu"

Here is the output:
Code:
./r.sh 
Badly placed ()'s.
{: Command not found.
for: Command not found.
do: Command not found.
var: Undefined variable.
done: Command not found.

}: Command not found.
nu: Command not found.

Next step is to run it as other user using su command but this is the start.

Thank you in advanced.

Last edited by ziguy; 02-16-2017 at 05:32 AM..
# 2  
Old 02-15-2017
I think you're better off starting with:

Code:
ssh host bash -s << 'EOF'
nu() {
  whatever
}
nu
EOF

This starts a remote shell and reads the script from stdin, which you give with a heredoc
# 3  
Old 02-15-2017
Above seems to work between two systems running bash (or sh) each. What be the shell on the remote system? Do you know if the errors occur on the remote or local host?
# 4  
Old 02-15-2017
Quote:
Originally Posted by ziguy
Hi all.

I need a bash script to run a function remotely. I think it should be similar to the following but can't figure out the exact syntax.

Here is the script:
Code:
#!/bin/bash

function nu ()
{
        for var in {0..5}; do printf "$var, "; done; echo
}

ssh host "$(typeset -f); nu"

Here is the output:
Code:
./r.sh 
Badly placed ()'s.
{: Command not found.
++ null function ++
}: Command not found.
nu: Command not found.

Next step is to run it as other user using su command but this is the start.

Thank you in advanced.
As others have pointed out, setting a function on your system doesn't make the function exist on a remote system.

I'll also point out that $(typeset -f) is run on your local system, before ssh is even run. This is because you've put it inside double quotes, which will evaluate variables, backticks, and $( ) before processing the command.
# 5  
Old 02-15-2017
Thank you for all responses.
Using heredoc is the alternative i'm indeed using right now.

I'm trying to find a better way because I have several functions and need to use them locally AND remotely and don't want to declare the functions more than once.

I'm aware that typeset is evaluated locally, otherwise it won't contain the function and in that case is useless

I'm not sure where the error occurs.
# 6  
Old 02-15-2017
How about:

Code:
. functions.sh

ssh user@host <<EOF
$(cat functions.sh)

remote code
EOF

# 7  
Old 02-15-2017
It works fine for me here, output:

Code:
0, 1, 2, 3, 4, 5,

I expect your remote shell is not a bash shell, if I force csh on remote side:

Code:
#!/bin/bash

function nu ()
{
        for var in {0..5}; do printf "$var, "; done; echo
}

ssh host "csh -c \"$(typeset -f); nu\""

I get a very similar output:

Code:
Badly placed ()'s.
{: Command not found.
for: Command not found.
do: Command not found.


Perhaps you should force bash on remote side with ssh host "/usr/bin/bash -c \"$(typeset -f); nu\""

Last edited by Chubler_XL; 02-15-2017 at 04:31 PM.. Reason: Tidy up grammar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Then error while running script remotely

facing issue with then error while running a local script aginst a remote server. i facing the same issue in multiple scripts. So what i am missing here or what is needed. #!/bin/ksh echo "enter the filename" read file if then echo "file exists" else echo "file does not exists" fi ... (0 Replies)
Discussion started by: NarayanaPrakash
0 Replies

2. Solaris

SSH Remotely

Hello. I am trying to ssh and run a script from a remote computer. These computers will be both Windows and MACs. I am using Solaris 8 and what I have tried is: using putty ssh user@ip_address (remote command) /folder/folder/filename.sh The issue here is that the user profile has not... (3 Replies)
Discussion started by: jkmtm
3 Replies

3. Shell Programming and Scripting

remotely call function from local script

The following code doesn't work properly which means it doesn't displays remote output. #!/bin/ksh #################### Function macAddressFinder ######################## macAddressFinder() { `ifconfig -a > ipInterfaces` `cat ipInterfaces` }... (2 Replies)
Discussion started by: presul
2 Replies

4. Shell Programming and Scripting

howto run remotely call function from within script

Hi I have the following script : #!/bin/ksh #################### Function macAddressFinder ######################## macAddressFinder() { `ifconfig -a > ipInterfaces` `cat ipInterfaces` } ####################################################################### # # print... (2 Replies)
Discussion started by: presul
2 Replies

5. Shell Programming and Scripting

Running a function on a remote server via SSH in a script

I'm working on a script (mostly for practice) to simplify a task I have to do every now and then. I have a cluster with 6 servers on it, each server has a directory with a set of files called *.pid and *.mpid. Each file contains the pid of a process that may or may not be running on that server.... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

6. AIX

Mozilla running remotely

We have a server that has CDE and Mozilla installed on it. I have a develpoer who want to have an application start Mozilla locally on the AIX server (which has no video card) or wants Mozilla running on the server at all times. Can anybody tell me if this is at all possible? If so how and if... (4 Replies)
Discussion started by: daveisme
4 Replies

7. Shell Programming and Scripting

ssh running on function

i have a problem regarding running an ssh command while inside "function" script structure #!/usr/bin/bash func1(){ script=$1 ssh user@server "$script" } cat script.txt | while read line do func1 $line done exit 0 the problem is when ssh ran,... (1 Reply)
Discussion started by: ryandegreat25
1 Replies

8. Shell Programming and Scripting

Run function from script over ssh (BASH)

Hi, Is there any cleaver way to run function from the bash scrip over ssh? For example: #!/bin/bash #Function 1 FN1 () { ls -l } #Main run ssh user@host FN1 exit 0 Yeah, I know it will not work, but I'm asking how to make it to work :) I'm suspecting that it would be... (1 Reply)
Discussion started by: columb
1 Replies

9. Shell Programming and Scripting

Running function or command concurrently in a bash script

I currently run a script over a vpnc tunnel to back-up my data to a remote server. However for a number of reasons the tunnel often collapses. If I manually restore the tunnel then the whole thing can continue, but I want to add into my script a section whereby while the transfer is taking place,... (8 Replies)
Discussion started by: dj_bridges
8 Replies

10. Shell Programming and Scripting

running commands with remotely with Telnet

i have a box here that can only be accessed with telnet. now, i was wondering if anyone know of a way of which i can run a command on that box remotely. (2 Replies)
Discussion started by: Terrible
2 Replies
Login or Register to Ask a Question