LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-28-2009, 12:38 PM   #1
columb
LQ Newbie
 
Registered: Oct 2003
Location: UK
Distribution: Redhat/Fedora/Centos/SuSE
Posts: 6

Rep: Reputation: 0
Run function from script over ssh (BASH)


Hi,

(I hope it's right forum)
Is there any cleaver way to run a 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 impossible to do that, but then one never know!

Many thanks in advance!
Cheers
Chris
 
Old 05-28-2009, 12:53 PM   #2
esoukenka
LQ Newbie
 
Registered: Feb 2009
Posts: 19

Rep: Reputation: 2
A solutions

I have a script which works you can review and alter to what you need

#!/bin/sh
if [ "$1" = "" ];
then
echo "syntax: ussh remote_username remote_server local_username local_server"
exit 1
fi
ssh "$1"@"$2" "echo `ssh "$3"@"$4" cat .ssh/id_rsa.pub` >> .ssh/authorized_keys"
 
Old 05-28-2009, 01:18 PM   #3
columb
LQ Newbie
 
Registered: Oct 2003
Location: UK
Distribution: Redhat/Fedora/Centos/SuSE
Posts: 6

Original Poster
Rep: Reputation: 0
Sorry, but you have missunderstood me. I know how to run command over the ssh but was wondering if there is a clever way to run function from the script where ssh is called from.

Cheers
Chris
 
Old 05-28-2009, 02:55 PM   #4
nuwen52
Member
 
Registered: Feb 2009
Distribution: Debian, CentOS 5, Gentoo, FreeBSD, Fedora, Mint, Slackware64
Posts: 208

Rep: Reputation: 46
From bash? I don't know without doing something like transferring the commands to a script on the remote machine and running that.

But, you could use expect to open an SSH session and send the commands that way.
 
Old 05-29-2009, 01:33 AM   #5
esoukenka
LQ Newbie
 
Registered: Feb 2009
Posts: 19

Rep: Reputation: 2
Put a script on remote machine and call it with your function. Then output that to a file and scp it back to your script and parse as needed. Only way I can think of.
 
Old 05-29-2009, 02:49 AM   #6
columb
LQ Newbie
 
Registered: Oct 2003
Location: UK
Distribution: Redhat/Fedora/Centos/SuSE
Posts: 6

Original Poster
Rep: Reputation: 0
Well, pretty much that's what going to happen. I will scp script first an then run it. That's easy. I was thinking if there is a way to define function on remote system from the script which calls ssh

Good that's impossible!

Cheers
Chris
 
Old 05-29-2009, 03:48 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Indeed you have to define the function on the remote machine, since you cannot export a function through ssh. However you can add the function definition at the beginning of the ssh commands so that you define it for that ssh session only. For example:
Code:
#!/bin/bash
FUNCTION1='function FN1 { ls -l; }'
ssh user@host "$FUNCTION1; FN1"
If you need the same function in the local script, just add:
Code:
eval $FUNCTION1
export -f FN1
the statement "export -f" make the function available to each subshell and child process created by the script.

Last edited by colucix; 05-29-2009 at 03:50 AM.
 
Old 11-07-2011, 01:22 PM   #8
azdc
LQ Newbie
 
Registered: Nov 2011
Posts: 1

Rep: Reputation: Disabled
One way you can do it is to write the function in the ssh command. Building on the example above to add the public key to the remote server, here is a script I wrote that will do it for an array of servers (all assuming they have the same username you use on the local machine). This does a few checks to prevent copying the public key more than once, and sets permissions appropriately for the .ssh directory and the authorized_keys file.

Code:
SERVER=`uname -n`
KEY=`cat ~/.ssh/id_dsa.pub`

servers=( 192.168.24.180 \
          192.168.24.181 \
          192.168.24.182 )

for server in ${servers[@]}; do
  ssh `whoami`@$server "
  function copy_key {
      [ ! -e ~/.ssh ] && mkdir ~/.ssh
      chmod 755 ~/.ssh
      [ ! -e ~/.ssh/authorized_keys ] && echo $KEY >> ~/.ssh/authorized_keys

      grep $SERVER ~/.ssh/authorized_keys > /dev/null
      [ $? -ne 0 ] && echo $KEY >> ~/.ssh/authorized_keys

      chmod 600 ~/.ssh/authorized_keys
  }; copy_key"
done

Last edited by azdc; 11-07-2011 at 01:23 PM. Reason: typo
 
Old 07-22-2016, 12:54 PM   #9
Shinto
LQ Newbie
 
Registered: Jul 2016
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
Indeed you have to define the function on the remote machine, since you cannot export a function through ssh. However you can add the function definition at the beginning of the ssh commands so that you define it for that ssh session only. For example:
Code:
#!/bin/bash
FUNCTION1='function FN1 { ls -l; }'
ssh user@host "$FUNCTION1; FN1"
If you need the same function in the local script, just add:
Code:
eval $FUNCTION1
export -f FN1
the statement "export -f" make the function available to each subshell and child process created by the script.


Thanks for your info....
I have some query , how to execute a function which has many command on it.
Below fine work good but only one command.
For example I need to add more command on function and call it.for example as below.

Code:
#!/bin/bash
for I in server list
Do
FUNCTION1='function FN1 {
a=`hostname`
b=`df -h`
c=`ls -l`
echo "server=$a list=$bc fs=$b
 }'
ssh $i "$FUNCTION1; FN1"
But above one is wrong it won't work.
How do is make it correct.
 
Old 07-23-2016, 09:16 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
Code:
for i in server list
do
ssh $i 'echo "server=$(hostname) list=$(ls -l) fs=$(df -h)"'
done
Edit: used $() expression assuming bash was default remote shell for current user, if not stay with back ticks

Last edited by keefaz; 07-23-2016 at 09:19 AM.
 
Old 07-23-2016, 11:51 AM   #11
Shinto
LQ Newbie
 
Registered: Jul 2016
Posts: 2

Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
Code:
for i in server list
do
ssh $i 'echo "server=$(hostname) list=$(ls -l) fs=$(df -h)"'
done
Edit: used $() expression assuming bash was default remote shell for current user, if not stay with back ticks
Thanks for the reply..
But My query was How do I apply my function in remote shell.
I need to Declare the function globally and call the function even in ssh
 
Old 07-23-2016, 01:10 PM   #12
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
Why do you absolutely need a function?

You could also do it like
Code:
#!/bin/bash
for i in server list
do
cat <<'END' | ssh $i bash
a=`hostname`
b=`df -h`
c=`ls -l`
echo "server=$a list=$c fs=$b"
END
done
Or with a function (don't see the need though)
Code:
#!/bin/bash
for i in server list
do
cat <<'END' | ssh $i bash
FN1() {
  a=`hostname`
  b=`df -h`
  c=`ls -l`
  echo "server=$a list=$c fs=$b"
}
FN1
END
done

Last edited by keefaz; 07-23-2016 at 01:13 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Split function (bash script) NiM Programming 11 03-10-2012 07:53 AM
Bash: Problem to run a function in Case mitramcc Linux - Newbie 1 02-12-2009 03:07 PM
bash script with ftp in a function -- please help! kpj104 Linux - General 1 05-19-2008 11:24 AM
Run intractive ssh session in a bash script MasterC Programming 2 10-15-2007 06:08 PM
search function (bash script) LYK Programming 2 05-27-2004 11:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration