Cannot create variables via ssh on remote machine


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cannot create variables via ssh on remote machine
# 8  
Old 11-28-2011
Hello, Please disregard my previous update when I said "it work". It didn;t, so the issue is still present for me. To avoid opening a new post, I will make a short recapitulation of the issue :

Background info :
Local machine : Linux, /bin/bash
Remote machine (for the user used for ssh) : SunOs, /bin/ksh
(so we have different OS, different Shells)

aaa_1=999
ssh -q user@machine "
> i=1
> echo aaa_1 is $aaa_1
> eval value=$`echo aaa_\${i}`
> echo value is \$value
> "
aaa_1 is 999
value is


So the variable $value is not set.
I've also tried the remote eval command this way:
Code:
eval value=\$`echo aaa_\${i}`
eval value=$`echo aaa_${i}`

With the same result.
So I basically want to create (while I'm in a remote ssh session) the variable value. $value should take the content of the variable aaa_1 (which was set in the initial session, before ssh).But I need to call the variable aaa_1 using variable $i (set after ssh and which has value "1" which will be used as part of variable aaa_1 name). I wanna use the value of $i variable to create the name of the $aaa_1 , and this name I wanna use in order to call the variable in the eval command.
The idea it;s a bit complicated but the code shouldn;t be hard to understand.

Any help regarding the syntax I use or different approach to accomplish what I want?

---------- Post updated at 05:38 AM ---------- Previous update was at 05:25 AM ----------

To be more clear, this is what I want but done in only one session , without ssh :
Code:
aaa_1=999
i=1
eval value=$`echo aaa_${i}`
echo "value is $value "
value is 999

So this way it works. The only difference is that I want
Code:
aaa_1=999

------------------------------------> to use ssh in this point
Code:
i=1
eval value=$`echo aaa_${i}`
echo "value is $value "
value is 999

@vbe (moderator) please read carefully my post before answering/suggesting something

THX,
# 9  
Old 11-28-2011
You don't need an escape for i...
Code:
[root@bt] ssh localhost "
i=1
echo aaa_1 is $aaa_1
eval value=`echo \$aaa_${i}`
echo value is $value
"
aaa_1 is 999
value is 999

echo statement is redundant! eval value=\$aaa_${i}; also will work.

HTH
--ahamed
# 10  
Old 11-28-2011
Nope... it didn;t work :
Code:
aaa=999
ssh -q user@remotehost "
> i=1
> echo aaa_1 is $aaa_1
> eval value=`echo \$aaa_${i}`
> echo value is $value
> "
aaa_1 is 999
value is

Nor this way :
Code:
aaa=999
ssh -q user@remotehost "
> i=1
> echo aaa_1 is $aaa_1
> eval value=\$aaa_${i}
> echo value is $value
> "
aaa_1 is 999
value is

Are you sure you got the posted results? could be be a shell discrepancy? as I said in background info I have different shells on local host and remote host. What's the situation on your side? what shells do you use ? I know that command lines are parsed differently by different shells.

---------- Post updated at 08:22 AM ---------- Previous update was at 08:09 AM ----------

btw: make sure you didn;t set the variable $value in the current shell. If you slipped by mistake the line
value=999
somwhere before the ssh , the results are wrong, because the value of $values is carried via ssh .
I've made that mistake before when I thought my problem is solved. :|

Last edited by Franklin52; 01-04-2012 at 04:22 AM.. Reason: Please use code tags for code and data samples, thank you
# 11  
Old 11-28-2011
To get this right: you want to assign the content of a variable that is set on your localhost to another variable on a remote host and want to determine which local variable to use during the ssh-session? Sounds like bad design to me, but if that's your goal you need to transfer your variables to the remote host first.
Under the assumption that the possible variables to choose all start with aaa_ you could do it like this:
Code:
myset=$(set |grep "aaa_")
ssh -q user@remotehost "$myset; i=1; eval value='$'\$(echo aaa_\${i}); echo \$value"

The myset variable does the transfer. set returns the variable in reuseable form so $myset evaluates into all relevant variable-definitions.
Btw, if you like the backquote-notation better than the $()-one you have to escape the backquotes or command subsitution on your local host takes place.

Last edited by cero; 11-28-2011 at 10:30 AM..
This User Gave Thanks to cero For This Post:
# 12  
Old 11-28-2011
Try this...
Code:
root@bt:~# ssh localhost "
i=1
echo aaa_1 is $aaa_1
eval value=\$aaa_$i
echo value is $value
"
aaa_1 is 999
value is 999

The above code was running on solaris... weird the same is not working on Linux...

--ahamed
# 13  
Old 01-04-2012
Quote:
Originally Posted by cero
To get this right: you want to assign the content of a variable that is set on your localhost to another variable on a remote host and want to determine which local variable to use during the ssh-session? Sounds like bad design to me, but if that's your goal you need to transfer your variables to the remote host first.
Under the assumption that the possible variables to choose all start with aaa_ you could do it like this:
Code:
myset=$(set |grep "aaa_")
ssh -q user@remotehost "$myset; i=1; eval value='$'\$(echo aaa_\${i}); echo \$value"

The myset variable does the transfer. set returns the variable in reuseable form so $myset evaluates into all relevant variable-definitions.
Btw, if you like the backquote-notation better than the $()-one you have to escape the backquotes or command subsitution on your local host takes place.
Hello and many thanks. You craked it Smilie . it works perfectly for me with one small adjustment :
instead of
Code:
ssh -q user@remotehost "$myset; i=1; eval value='$'\$(echo aaa_\${i}); echo \$value"

I think it should be
Code:
ssh -q user@remotehost "eval $myset; i=1; eval value='$'\$(echo aaa_\${i}); echo \$value"

Indeed this is also a matter of shifting variables through ssh. I use it like this :
I have a config file on the local machine (with lots of defined variables e.g var1=88 ; var2=77 etc. ) I load the content of this file in a variable and then evaluate the content of this variables immediately after ssh (basically is exactly what you suggested but i don't use set | grep , I use a config file instead ).
I think of it as an elegant way to configure lots of things in one file on a single local machine. then to do lots of actions on many remote machines using the parts of config that you need pending by the conditions you find on each remote machine / situation.
do you suggest a general idea of a different approach?
Basically what I'm trying to do is a script to perform lots of network checks on and between many machines from one flow.

Last edited by Franklin52; 01-04-2012 at 04:23 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Input to while loop in remote machine using ssh

Hello I am writing a script in a local machine, i am using ssh, here i am not able to using back ticks & input file to while loop. here is my script ssh -q server1 a=`ps -ef | grep ccms | awk {print $1}` b=`ps -ef | grep mss | awk {print $1}` # above lines are not working, so i redirected... (12 Replies)
Discussion started by: nanz143
12 Replies

2. Linux

Executing a script in remote machine through ssh

How to execute a script in remote machine through ssh I have a script test.sh which does some backup activity in remote machine. Wanted to keep backup also in remote machine. ssh -l username <remote machine> "commands to be exceuted as ; separted" but how to put the script in the place of... (5 Replies)
Discussion started by: sanvel
5 Replies

3. Red Hat

iptables applied in local machine, can't ssh remote machine after chain changed to DROP

I want to SSH to 192.168.1.15 Server from my machine, my ip was 192.168.1.99 Source Destination was UP, with IP 192.168.1.15. This is LAN Network there are 30 Machine's Connected to the network and working fine, I'm Playing around the local machine's because I need to apply the same rules in... (2 Replies)
Discussion started by: babinlonston
2 Replies

4. UNIX for Dummies Questions & Answers

how to use ssh to run shell script on a remote machine?

how to use ssh to run shell script on a remote machine? ssh user@remote sh ./script.unx i ran the above command ./script.unx HAS NOHUP COMMAND IN ITS BODY, I AM GETTING ERROR AS NOHUP NOT FOUND... i tried to run that script from remote server, its working fine do ineed to set... (6 Replies)
Discussion started by: only4satish
6 Replies

5. Linux

Create VNC Session on remote machine on which ssh access is denied

Hi Folks, I want to create VNC session on the Remote RHEL machine on which ssh access is denied. Is there any way so that I can create VNC session without ssh access. Let me know all possible ways! (1 Reply)
Discussion started by: gydave
1 Replies

6. Shell Programming and Scripting

executing command in a remote machine through ssh - shell script

Hi All, i have two machines like x and y . my requirement is i should connect to machine Y from x through ssh connection . and do some operation such as copy and move and delete files in Y machine . i tried with this code but it is doing in machine x only . and i need to exit from Y when... (1 Reply)
Discussion started by: rateeshkumar
1 Replies

7. UNIX for Dummies Questions & Answers

ssh to remote machine with 2nd user and password

Hi all, Really hope someone can help me, i have been trying lots of things and just cant seem to nail it - and for something that seems straight forward.... Anyway, scenario is I need to log onto a second machine (remote server) from main workstation. Once logged in I need to run a batch... (2 Replies)
Discussion started by: Hopper_no1
2 Replies

8. Red Hat

Unable to SSH into machine - ssh_exchange_identification: Connection closed by remote host

For a few days now I have been experiencing issues when trying to SSH into 1 of my machine. I get the following output when running 'ssh -vvv': server1:/home/mymadq> ssh -l root -vvv server2 OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003 debug1: Reading configuration data /etc/ssh/ssh_config... (3 Replies)
Discussion started by: jaapar
3 Replies

9. Shell Programming and Scripting

ssh connection from remote machine in solaris

Hi! I have two solaris 10 machines(say 10.1.1.1,10.1.1.2). i have installed rsync on 10.1.1.2, 10.1.1.1::: Sun Microsystems Inc. SunOS 5.10 Generic January 2005 -bash-3.00$ ssh 10.1.1.2 "echo $PATH" Password:... (4 Replies)
Discussion started by: dddkiran
4 Replies

10. Shell Programming and Scripting

executng program on remote machine using ssh

I am trying to search and remove files from a list of servers. I want to find every occurence of this file on each machine and then remove it. If I execute the find command on the remote machine I would like to be able to pipe the output to xargs and remove the file. Does anyone know hat would be... (1 Reply)
Discussion started by: sewood
1 Replies
Login or Register to Ask a Question