How to Run a Bash Function() in a Remote Host? in Ubuntu

How to run a bash function() in a remote host? in Ubuntu

Resone for the error:

When you do

ssh ppuser@10.101.5.91 " keyConfig $1 $2 $4 "

You are actually trying to execute a command keyConfig on remote machine 10.101.5.91
Which is certainly not there:

2 Solution for the problem

1) Make a script on remotehost which contains keyConfig code with same name

OR

2) Execute following instead of function

ssh ppuser@10.101.5.91 "sed -i.bak -r "/^$1/s/([^']+')([^']+)('.*)/\1$2\3/" $3"

Please note you may have to add few escape depending on the sed syntax you are using

Shell script: Run function from script over ssh

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.

Call function from function into remote host through bash script is not working

Without seeing exactly what the generated script looks like, it's not really possible to troubleshoot this.

But I would instead break up your logic into a script which gets copied to the destination and executed there, and a simple script which does the copying and evaluation.

#!/bin/bash

script=$(cat <<\____HERE
install_prelibrary () {
# Notice also refactoring; comments below
if wget https://github.com/EOSIO/eos/releases/download/v2.0.0/eosio_2.0.0-1-ubuntu-18.04_amd64.deb --no-check-certificate > /dev/null 2>&1; then
: pass
else
rc=$?
# Write errors to standard error, exit with an actual failure code
printf "Error downloading Ubuntu Binary file\n" >&2
exit $rc
fi
install_cdt
create_wallet_and_keys
}

install_cdt(){
#some commands
}
create_wallet_and_keys(){
#some commands
}
____HERE
)
SCRIPT="$(cat ~/shell/config.sh); $script; install_prelibrary"
for i in ${!genesishost[*]} ; do
printf "\t=========== node ${genesishost[i]} ===========\n\n"
SCR=${SCRIPT/PASSWORD/"$password"}
sshpass -p "$password" ssh -l "$username" "${genesishost[i]}" "${SCR}"
done

If you need to evaluate the functions locally, too, it's not too hard to have the script read itself; or simply store the code in an external file and source that as well as reading it into a variable.

Bash: Running a function as sudo on remote host?

The suggestion from @Will is helpful in this instance, using sudo bash -c, then declaring and running the function:

sudo bash -c "$(declare -f runOnRemoteHostAsRoot); runOnRemoteHostAsRoot"

We'll use that line after passing the password through sshpass for passwordless login, like this:

echo '${SSHPASS}' | sudo --stdin bash -c '$(declare -f runOnRemoteHostAsRoot); runOnRemoteHostAsRoot'`

So using this in the above example:

#!/bin/bash

read SSHPASS
export SSHPASS

runOnRemoteHost() {
# ...
whoami
# ...
}
# ...
sshpass -e ssh -o PasswordAuthentication=yes "user@remotehost" "$(declare -f runOnRemoteHost); runOnRemoteHost" 2>&1
# ...

runOnRemoteHostAsRoot() {
# ...
whoami
# ...
}
# ...
sshpass -e ssh -o PasswordAuthentication=yes "user@remotehost" "echo '${SSHPASS}' | sudo --stdin bash -c '$(declare -f runOnRemoteHostAsRoot); runOnRemoteHostAsRoot'" 2>&1

Output:

user
root

executing method on remote machine with sshpass shell script

I think this will remove the above error.

sshpass -p "$serverpwd" ssh "$serveruser"@"$serverip" << EOSSH
checkfunctioncall(){
cd "/home/user"
mkdir "remotecalltest"
}
checkfunctioncall
EOSSH

What is the cleanest way to ssh and run multiple commands in Bash?

How about a Bash Here Document:

ssh otherhost << EOF
ls some_folder;
./someaction.sh 'some params'
pwd
./some_other_action 'other params'
EOF

To avoid the problems mentioned by @Globalz in the comments, you may be able to (depending what you're doing on the remote site) get away with replacing the first line with

ssh otherhost /bin/bash << EOF

Note that you can do variable substitution in the Here document, but you may have to deal with quoting issues. For instance, if you quote the "limit string" (ie. EOF in the above), then you can't do variable substitutions. But without quoting the limit string, variables are substituted. For example, if you have defined $NAME above in your shell script, you could do

ssh otherhost /bin/bash << EOF
touch "/tmp/${NAME}"
EOF

and it would create a file on the destination otherhost with the name of whatever you'd assigned to $NAME. Other rules about shell script quoting also apply, but are too complicated to go into here.

Executing a local shell function on a remote host over ssh using Python

I ended up using this.

import subprocess
import sys
import re

HOST = "user@" + box
COMMAND = 'my long command with many many flags in single quotes'

ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = ssh.stdout.readlines()


Related Topics



Leave a reply



Submit