run command with ssh[solved]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting run command with ssh[solved]
# 1  
Old 02-17-2012
run command with ssh[solved]

Hi all,

Is it possible to make this possible ?

Code:
[smily@xyz ~]$ echo $SKY_HOME
/var/sink/SKY

[smily@abc ~]$ echo $SKY_HOME
/home/smily/SKY

[smily@abc ~]$ ssh root@xyz "echo $SKY_HOME"
root@xyz 's password: ******
/home/smily/SKY wrong output [for me , it's wrong ]

I was expecting the output as
/var/sink/SKY

.

---------- Post updated at 12:37 PM ---------- Previous update was at 12:04 PM ----------

Yep. I got it.

Code:
In xyz
=====
I made a shell script : /var/scripts/printEnv.sh
#!/bin/bash
source /etc/profile
echo $SKY_HOME

In abc
=====
[smily@abc ~]$ ssh root@xyz "sh /var/scripts/printEnv.sh"
root@xyz 's password: ******
/var/sink/SKY correct output [ As expected. ]


Last edited by linuxadmin; 02-20-2012 at 01:40 AM.. Reason: Wrongly given the answer. sorry. Now corrected.
# 2  
Old 02-17-2012
Single quotes are also a good way to delay $ expansion. In fact, using single quotes a lot and first prevents lots of surprises. In severe cases of quote and $ confusion, you can go to:
Code:
echo '. . . .' | ssh who@where bash

# 3  
Old 02-17-2012
if you need complete and precise control of input and substitution you can do this:

Code:
ssh username@host exec /bin/sh -s "arg1" "arg2" <<"EOF"
# Nothing will substitute inside the here-document.  If you want anything
# carried inside, pass them along like the arg1, arg2, arg3 up there.
# arg1 becomes $1, arg2 becomes $2, etc.
echo "My hostname is $HOSTNAME"
EOF

# 4  
Old 02-22-2012
I find echo '...'| does even less substitution that a <<here document. I never got a speed difference but suspect the here overhead is more than the echo. Also, the echo is directly viewable in final form. However, the $# can be handy, like having a remote script but not having a second file to maintain and distribute.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Run awk command inside ssh

I am trying to run an awk command inside of ssh and it is not working. These are AIX servers. for i in `cat servers`; do ssh $i "/bin/hostname; df -g | awk '/dev/ && $4+0 > 70'"; done server1 server2 server3 server4 I also tried these two methods and they did not work. It just seemed... (5 Replies)
Discussion started by: cokedude
5 Replies

2. Shell Programming and Scripting

[solved] Process ssh command in while loop

I have a script that reads a file containing a list of server names. It's suppose to loop through the list of names and execute a command on the remote server using ssh. It processes the ssh command for the first server in the list and then exits. Here's the code: #!/bin/bash ... (2 Replies)
Discussion started by: westmoreland
2 Replies

3. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

4. Shell Programming and Scripting

Unable to run command after ssh

Hello, I am trying to create a ksh script to login to server and collect gather output of some command to troubleshoot some issue. DATE=`date +%b.%d.%Y.%M.%H` echo " Enter emp id to login to server" read Eid Eid=$Eid echo " Enter hostname of the system" read HOST HOST=$HOST... (2 Replies)
Discussion started by: saurabh84g
2 Replies

5. Shell Programming and Scripting

[SOLVED] put dd | ssh command in backgound

Greetings, I have an issue that has baffled me. I have done many searches, read the man documentation, and I have yet to find a solution. I am trying to run the following command within a script to copy a file across servers: $(dd if="$FDIR" bs=1024 2> /dev/null | ssh "$(whoami)@$SERVER"... (5 Replies)
Discussion started by: unaligned
5 Replies

6. Shell Programming and Scripting

Run perl command in script[solved]

Hi all, When I put the Perl command in a script, I got error. system("perl -pi -e 's@words@words@g' myFile"); The error is: Unrecognized character \x8A; marked by <-- HERE after دت مد�<-- HERE near column 15 at -e line 1. Thanks in advance. ---------- Post updated at 06:30 AM... (0 Replies)
Discussion started by: Lham
0 Replies

7. Shell Programming and Scripting

[solved] Killing 3rd command in ssh chain

Hi All, Noob question here... How do I kill the 3rd command in this ssh chain effectively? # ssh -t -t 10.80.0.5 'ssh 10.80.0.6 | /var/tmp/some_script' The "/var/tmp/some_script" contains: ssh 10.80.0.81 'echo "Hello World!!!!" >> /tmp/sample.txt'The problem is that once the sample.txt... (2 Replies)
Discussion started by: NYG71
2 Replies

8. Solaris

Run command on sc via ssh

when i run a command on ALOM via ssh i get following error ssh root@10.23.12.51 showhosts Password: Waiting for daemons to initialize... Daemons ready shell: Invalid credentials how can i run commands without actually loging to the sc (3 Replies)
Discussion started by: fugitive
3 Replies

9. Shell Programming and Scripting

how to run a command in different machine using SSH

how to run a command in different machie in my case script will runs in solaries machine.. in one instance it has to run a command in different machine with different operating system ( linux ) using SSH command i tried ssh -l (login_name) (machine name/host ) " command " but it is... (3 Replies)
Discussion started by: mail2sant
3 Replies

10. Shell Programming and Scripting

How do I get ssh to run a command in one line?

How would I combine something like: localserver# ssh remoteserver remoteserver# find blah blah blah into a one liner that would ssh to the remote server and run the find command, so I could put it in a script to automatically go out and run things on remote servers with out needed user... (2 Replies)
Discussion started by: LordJezo
2 Replies
Login or Register to Ask a Question