How to export the function in ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to export the function in ssh
# 1  
Old 03-01-2012
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

Code:
#/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
# 2  
Old 03-02-2012
save the code below to a file such as remote_run.sh
Code:
#/bin/bash

logs_gen () {
          XXXXXXXXXXX
          XXXXXXXXXXX
          XXXXXXXXXXX
}

logs_gen

then just run the commands below
Code:
for i in x y z; do # x y z are the server names
      ssh -q ${i} "bash -s" <  remote_run.sh
done

# 3  
Old 03-02-2012
I tried the below , but it did not export it to entire session...

Code:
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?
# 4  
Old 03-02-2012
It's impossible to invoke a local function over ssh, as the local function just not have been defined at the remote environment!
# 5  
Old 03-02-2012
You have to do a workaround.

In the ssh command, copy your script to tmp & trigger from there.
# 6  
Old 03-02-2012
It should be doable on the fly,but not nice :
Code:
ssh host "printf \"testing () {\n cp "'\$'1" "'\$'2" \n}\" > /tmp/func;. /tmp/func; testing /tmp/func /tmp/func.used"

# 7  
Old 03-08-2012
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

3. Shell Programming and Scripting

[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: #!/bin/bash function nu () { for var in {0..5}; do printf "$var, "; done; echo } ssh host "$(typeset -f); nu" ... (9 Replies)
Discussion started by: ziguy
9 Replies

4. Shell Programming and Scripting

Export a function to another function

is there a way to export a function to another function? i'm using #!/usr/xpg4/bin/sh in the first line of my script. firstfunc () { echo $1 is nice } secondfunc () { firstfunc Nancy } when i run a similar scenario to the above, i get: /usr/xpg4/bin/sh: firstfunc: not found (4 Replies)
Discussion started by: SkySmart
4 Replies

5. Shell Programming and Scripting

Run recursive function with variables over SSH

Hi, I don't know if you can help or if this is even possible, but I am trying to run the following function over an ssh and (depending on the itteration I choose) keep getting unexpected token or undefined symbol errors. The function is: killtree() { typeset parent=$1 typeset child... (1 Reply)
Discussion started by: RECrerar
1 Replies

6. Shell Programming and Scripting

korn shell - export function??

Hi, I have a question. I know that if I want to access any variables defined in a calling script (script1.ksh) in a called script (script2.ksh) I need to export that variable in the calling script to make it available in the called script. But what about the functions in the calling script? ... (1 Reply)
Discussion started by: dips_ag
1 Replies

7. 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

8. 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

9. 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

10. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question