1834355 Members
1899 Online
110066 Solutions
New Discussion

function through remsh

 
Donny Jekels
Respected Contributor

function through remsh

not sure if this is possible, but I need to execute a function inside a shell script on a remote node with remsh and then use sudo.

any ideas??
"Vision, is the art of seeing the invisible"
7 REPLIES 7
Ramkumar Devanathan
Honored Contributor

Re: function through remsh

Hi Donny,

Using remsh you would run a command that is present on the target node.

so, create the script and save it on the node with the execute permissions all in order.

Thereafter run it from another node using remsh command.

Although I haven't tried it, using sudo is also fine since it would get passed to the shell on the machine on which sudo is configured - as long as your user had sudo authority bestowed in the /etc/sudoers file.

- ramd.
HPE Software Rocks!
Chris Vail
Honored Contributor

Re: function through remsh

I do this kind of thing all the time, except I use secure shell instead of remsh. The syntax:
remsh $HOST "sudo $SCRIPT"

I have two scripts that interact to analyzed disk usage. The first writes the data out in comma delimited format to standard output. The second parses a HOST file and then does:
ssh $HOST:"$SCRIPT">>FILE
FILE is on my local host. This lets me analyzed 20-30 machines at once, appending the output from that remote host onto a file on my local host.



Chris
Donny Jekels
Respected Contributor

Re: function through remsh

and what about passing variables across the boxes, does a export work?

========
get_data()
{
username=john
loginid=johntheman
export username
export loginid
}
creat_user()
{
userad -u 1000 -c $username $loginid
}

exe_another_node()
{
remsh BOX001 "sudo create_user"
}

exe_another_node
=========

will this work?
"Vision, is the art of seeing the invisible"
Ramkumar Devanathan
Honored Contributor

Re: function through remsh

hi Donny,

This won't work at all. remsh opens a shell on the remote host specified and then runs the command specified within that shell. So, your function and its declaration are not visible to the remsh call, or the remote host.

try this -

remsh hostname

that should return the hostname of the remote host. so, that means that the hostname command is called from within the remsh shell and it has to be present on the target node.

see man remsh for more details.

- ramd.
HPE Software Rocks!
Ramkumar Devanathan
Honored Contributor

Re: function through remsh

Donny,

Try this -

export username=$1
export loginname=$2
export uid=$3

eval "remsh \"useradd -u $uid -c $username $loginname\""

- ramd.
HPE Software Rocks!
Ramkumar Devanathan
Honored Contributor

Re: function through remsh

sorry the earlier post is incomplete.

Donny,

# cat abc.sh
#!/usr/bin/ksh
export username=$1
export loginname=$2
export uid=$3

eval "remsh \"useradd -u $uid -c $username $loginname\""

# EOF

run as follows -
# ./abc.sh ramd "Ramkumar" 1000

- ramd.

HPE Software Rocks!
Donny Jekels
Respected Contributor

Re: function through remsh

hey ramd,

I got it to work, after many hours of sweat, but guess what there is more limitations.

ok, let me 1st explain what I did to make it happen.


I have 2 scripts, one call it master on the main server.
and the other call it client.

now what I wanted to achieve was to allow the master to remsh into the client, then feed the client some variables, the client then executes his script with the variables supplied.

then when the client finishes, it needs to supply me a return code.

here is the trick.

master:

ok I have a list of variables already populated.
say -- db[0] .. db[9] all with different values and textl.

>>
DOIT=`remsh remote "client ${db[*]}`

echo ${DOIT} | read RETURN
echo $RETURN

# I use the value of $RETURN to do other tasks

client:

the client script read the variables supplied from the master script.

I assign inside the script those variables as $1 .. $9

NUM=$1; export $NUM

etc

then in the client script, I create say a useraccount.

the useraccount has a return code, which I assign to a variable
then I use return to send the variable back to the master.

return ${RETURN_CODE}


sounds cumbersome, but it works very well.

also, I never knew there is a limit on the total amount of characters sent back over a remsh = 10 max. by return function. discovered that today.

anyway thanx for your help.

"Vision, is the art of seeing the invisible"