Remote command in variable using ssh

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remote command in variable using ssh
# 1  
Remote command in variable using ssh

Hi, I've an issue in a shell script:

I'm opening an ssh connection to a remote server, then I want to store the result of a ls command in a variable, but it doesn't work: the ls is done on the local machine.
ssh user@server << EOF
 ls # works as expected (ls is done remotely)
 test=`ls` # the ls is done locally and not remotely

Note: I have to use EOF, because they are several others commands done during the ssh.

any solution for this problem ?

Last edited by Franklin52; 02-19-2013 at 08:41 AM.. Reason: Please use code tags for data and code samples
# 2  

Or try
ssh user@server << 'EOF'

This treats the following text as being in 'ticks'
But often this replaces one problem by another.
The real solution is to have two scripts:
ssh user@server < remotescript

Or, safer, force the correct interpreter in a remote shell:
ssh user@server /bin/sh < remotescript

No restriction on remotescript, and can be interactively tested on a remote system.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #446
Difficulty: Medium
The Large Hadron Collider experiments at CERN run on Scientific Linux.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete line from remote file over ssh passing variable

I have a variable called $a1 which maps to something like "http://servername proxy1 count http" and a lots of entries in a file on remote server. If I have the following in my .sh script: sed -i "\%$a1%d" mylog.txtthe line is deleted from mylog.txt. Great. I'm trying now to remvoe this from a... (3 Replies)
Discussion started by: say170
3 Replies

2. Shell Programming and Scripting

Declare and grep a variable via ssh/remote/loop

If I am running a bash command, and some awk getting the ethernet adapter on the local machine. It works fine. But if I will run it from the remote, it is EMPTY on echo and throwing error in grep. Thank you This work perfectly fine $ f=`/sbin/ip a|grep 127.127 | awk '{print $NF }' ` ; ip... (2 Replies)
Discussion started by: kenshinhimura
2 Replies

3. Shell Programming and Scripting

Execute ssh command with additional terminal command to any remote user not working script

Hello i am having an issue with bash script and this is the code now=$(cat hosts1.txt | awk '{print $2;}') while read n ;do ssh root@$now 'useradd test1; echo -e "test1\ntest1" | passwd test1 && echo "test1 ALL=(ALL:ALL) ALL" >> /etc/sudoers' When i execute only part with cat, it... (8 Replies)
Discussion started by: tomislav91
8 Replies

4. Red Hat

Use variable that needs "" in remote ssh command.

I want to use the below but since the date has a space it doesn't grep exactly what's needed. I need to keep the quotes around the variable when it passes it. I need the quotes around $DATE2 just to recognize the variable. Thanks in advance for any help given. D=`date +%d` DN=`echo -n $D |... (3 Replies)
Discussion started by: toor13
3 Replies

5. Shell Programming and Scripting

Execute command on remote host via ssh

How should i make the following code working #!/bin/bash INPUTFILE="test.txt" while read STRING; do IP=`host -t A $STRING | awk '{print $NF}'` HOSTNAME=`ssh -oPasswordAuthentication=no -oStrictHostKeyChecking=no $IP "hostname"` echo $HOSTNAME > out.txt done < $INPUTFILE At this moment while... (3 Replies)
Discussion started by: urello
3 Replies

6. Shell Programming and Scripting

ssh - running remote command not exiting

Hi, i have a shellscript, where in i need to connect to different server start these three jobs and exit from the server and start the same three jobs on local server. ssh user@remotehost 'bash -s' << EOF ${GETT_HOME}/bin/start1 & sleep 10 ${GETT_HOME}/bin/start2 & sleep 10... (1 Reply)
Discussion started by: swapnabhargav
1 Replies

7. Shell Programming and Scripting

ssh sending local variable to remote system

I am running a useradd script, which works locally but I want to take some of that local information and send it to a remote system, ssh keys are set up between the two systems. I am attaching the script, look at the section titled "Sending information to FTP2" Removed attachment, added... (0 Replies)
Discussion started by: slufoot80
0 Replies

8. Shell Programming and Scripting

ssh to remote command prompt

Hi all, Scenario - trying to run a batch job on a second machine. DIR=directory user=user server=server Here is what i have- ssh user@server 'sleep2; cd $DIR; <jobname>' I have got access to the correct directory, i checked with a pwd command previous. Problem is the job i... (1 Reply)
Discussion started by: Hopper_no1
1 Replies

9. Shell Programming and Scripting

ssh remote command not sourcing bashrc

Hi, WHen I login to the host using ssh, it sources bash_profile then bashrc file, and reads in my path and other variables. But when I want to execute a command remotely, it is not sourcing my bash_profile or bashrc files and I dont get the env I want. THis is causing a lot of problems. HOw... (1 Reply)
Discussion started by: sardare
1 Replies

10. Shell Programming and Scripting

ssh and remote command exec `uname -r`

Hi guys, I am trying to do a ssh for performing a set of actions. Find it below: I need to put the user/ kernel/ DISTRO variables before I complete this operation. what I observed is when ever I put a `command` in those quotes, it performs thta action in local system rather than remote one.... (5 Replies)
Discussion started by: jbmukund
5 Replies

Featured Tech Videos