Visit Our UNIX and Linux User Community


Passing Values to remote server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing Values to remote server
# 1  
Old 04-09-2011
Passing Values to remote server

I'm trying to pass there values from the present server to the remote server. here is the below code.

Code:
function abc() {

export a=$1
export b=$2
export c="$3"
export d="$4"

#servers
Servers=$(echo server40{1..3}p.s.com)

for host in ${Servers};
do

#server login
ssh $host /bin/bash <<\EOF

echo $a a
echo $b b
echo $c c
echo $d d

EOF
done
}

#functioncall
abc 34 56 xxxx yyyy;

but i'm getting blank for those variables wen i switch to other server even though i exported the variables. Please help me out what should be done.

---------- Post updated at 05:24 AM ---------- Previous update was at 05:08 AM ----------

i tried to pass like below

Code:
ssh $host "$a $b $c $d" /bin/bash <<\EOF

aa=$1
bb=$2
cc=$3
dd=$4

echo $aa aa
echo $bb bb
echo $cc cc
echo $dd dd

EOF

but i'm getting below error

Code:
bash: 3: command not found
bash: 3: command not found
bash: 3: command not found

is the way i'm passing is right?

Last edited by Yogesh Sawant; 04-09-2011 at 10:00 AM.. Reason: corrected the code tags
# 2  
Old 04-09-2011
You need quotes.
Code:
Servers="$(echo server40{1..3}p.s.com)"

Maybe you could write the commands to a file, scp it to the servers and then execute it.
Code:
echo "..." > cmdfile
for host in $Servers; do
    scp cmdfile $host;
    ssh $host ". cmdfile; rm ~/cmdfile"
done

---------- Post updated at 01:17 PM ---------- Previous update was at 01:13 PM ----------

Actually, you were right. You don't need quotes. Although it's good style to include them.
# 3  
Old 04-10-2011
From 'man ssh:'
Quote:
Additionally, ssh reads ~/.ssh/environment, and adds lines of the format
“VARNAME=value” to the environment if the file exists and users are
allowed to change their environment. For more information, see the
PermitUserEnvironment option in sshd_config(5).
Sooo, just before ssh-ing, you could create a new environment file:
Code:
...
a=$1 ; b=$2; 
varFile=~/.ssh/environment
[[ -f $varFile ]] && cp ${varFile}{,.orig} #backup if exists
cat > $varFile <<EOF
a=$a
b=$b
EOF

ssh host

[[ -f ${varFile}.orig ]] && mv ${varFile}{.orig,} #revert if necessary

but make sure PermitUserEnvironment is enabled in sshd_config on server side.

Last edited by mirni; 04-10-2011 at 07:25 AM..
# 4  
Old 04-10-2011
Quote:
Originally Posted by Amutha
Code:
ssh $host /bin/bash <<\EOF

Are you sure bash is in /bin on the hosts? NetBSD machines put it in /usr/pkg/bin.

I just tried this and it worked. It also worked when I used sh instead of bash.
Code:
TEST=abc
ssh somehost bash <<EOF
> echo $TEST test
> EOF
abc test

# 5  
Old 04-11-2011
Quote:
Originally Posted by KenJackson
You need quotes.
Code:
Servers="$(echo server40{1..3}p.s.com)"

Maybe you could write the commands to a file, scp it to the servers and then execute it.
Code:
echo "..." > cmdfile
for host in $Servers; do
    scp cmdfile $host;
    ssh $host ". cmdfile; rm ~/cmdfile"
done

---------- Post updated at 01:17 PM ---------- Previous update was at 01:13 PM ----------

Actually, you were right. You don't need quotes. Although it's good style to include them.
Actually it needs to execute in 3 servers.. i need the values in the present local server to be passed to all the 3 servers when they are executing

---------- Post updated at 05:42 AM ---------- Previous update was at 05:39 AM ----------

Quote:
Originally Posted by KenJackson
Are you sure bash is in /bin on the hosts? NetBSD machines put it in /usr/pkg/bin.

I just tried this and it worked. It also worked when I used sh instead of bash.
Code:
TEST=abc
ssh somehost bash <<EOF
> echo $TEST test
> EOF
abc test

It will work fine if its just an echo statement to display the value. but in case if its a path.

a=/tmp/user/log/

in this case this path wont be present in the local server but it'll be present in the remote server that i'm logging in.
when i tried like the way you instead it was searching for the path in the same server not in the remote server.

Previous Thread | Next Thread
Test Your Knowledge in Computers #649
Difficulty: Easy
aterm is an rxvt based terminal emulator developed for Afterstep.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script connect to remote server, not find files and exit only from remote server, but not from scrip

I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code TARFILE=${NAME}.tar TARGZFILE=${NAME}.tar.gz ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;... (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Shell Programming and Scripting

Sudo connect to a remote server and execute scripts in remote server

Hello Every one!! I am trying to write a shell script which will connect to a remote server and execute scripts which are at a certain path in the remote server. Before this I am using a sudo command to change the user. The place where I am stuck is, I am able to connect to the... (6 Replies)
Discussion started by: masubram
6 Replies

3. Shell Programming and Scripting

Connect (SSH) to Windows server via Linux server through a script and passing command.. but failing

I am trying to connect to Windows server via Linux server through a script and run two commands " cd and ls " But its giving me error saying " could not start the program" followed by the command name i specify e g : "cd" i am trying in this manner " ssh username@servername "cd... (5 Replies)
Discussion started by: sunil seelam
5 Replies

4. UNIX for Dummies Questions & Answers

Passing of variable values to remote server

Hi, My script will take 3 i/p's from user. Now i need to pass these 3 values to remote server. Please find my code. while do echo " To which server you want to connect ? " echo " 1. server1 \n" echo " 2. server2 \n" read opt_server if then echo "enter the... (2 Replies)
Discussion started by: sree143reddy
2 Replies

5. Programming

Passing multiple values from a function in C

I know multiple values can be returned from a function in C like this: char **read_file ( char * , unsigned long int * );//this is the function prototypeunsigned long int number_of_words = 0;//variable defined in main() and initialized to 0words_from_dictionary = read_file ( "dictionary.dit" ,... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

6. Programming

SFTP from one remote server to another remote server from desktop

Hi, I have 1. lappy 2. server A 3. server B Now, what i need is to run a command from lappy that will sftp a file from server A to server B. Please guide me to achieve this. -akash (1 Reply)
Discussion started by: akash.mahakode
1 Replies

7. Shell Programming and Scripting

passing arguments in remote ssh command?

i have a bash script on serverA, when i run the script on the server, it runs fine. the way to run the script is like this ./script "option1" Now i am trying to call it from local laptop using ssh command, my command is as the following ssh serverA ~/script "option1" and i got error... (7 Replies)
Discussion started by: fedora
7 Replies

8. Shell Programming and Scripting

Passing the variable value to remote server.

Dear All, Can anybody explain me how to pass the variable value to command argument which will execute in remote machine. example.. test="test-123.dbf" how can i pass this value to command ls -l for remote machine? I tried to do like this way ssh root@remote 'ls -l... (2 Replies)
Discussion started by: nmadhuhb
2 Replies

9. Shell Programming and Scripting

passing values into logrotate??

Wondering if this is possible? OK, here is my question. I'm backing up a TON of different logs in different locations. I'm querying a database for the information. What I'd like to do, is pass that information into a logrotate script for the backup.. i.e. I'm trying to do the following: ... (1 Reply)
Discussion started by: imbiea
1 Replies

10. Shell Programming and Scripting

Passing values out awk.

found that passing (input) values to awk, all work well. For example: errpt | awk 'BEGIN { errore=0 } substr($2,1,4) /'ParamData'/ { .... } ' ParamData=`date +"%m%d"` Now I wish to obtain (output) a value. Using this method is it possible to re-write ParamData, for example? Thanks in... (3 Replies)
Discussion started by: gio123bg
3 Replies

Featured Tech Videos