Issue in executing cat (remote ssh)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue in executing cat (remote ssh)
# 1  
Old 08-22-2015
Issue in executing cat (remote ssh)

Hi,
I need to ssh remotely to a machine and cat a file assign the value to a variable

Script:
Code:
#!/bin/bash -x
value=`cat config.txt`
echo "$value"

ssh me@xxx.host.com "valu='cat /export/home/test.md5'; echo "$valu"" | tee

Execution:
Code:
$ ./x
++ cat config.txt
+ value='touch me'
+ echo 'touch me'
touch me
+ ssh me@xxx.host.com 'valu='\''cat /export/home/test.md5'\''; echo '
+ tee

It's not giving any result when it executes the command remotely.

remote server:
Code:
cat test.md5
ce65f87aecee3d99cd2e947de5e4d4ea

I tried alot but can't able to resolve. Could you please help me on this.

Thanks.

Last edited by Scrutinizer; 08-22-2015 at 04:05 AM.. Reason: code tags
# 2  
Old 08-22-2015
I am unsure what you are trying to achieve. A noticed a couple of things:
  • Use $(...) instead of `...`. It saves you quoting troubles..
  • You use value first and then valu later. That may lead to confusion
  • Perhaps it is something like this it wat you are looking for?
Code:
ssh me@xxx.host.com 'valu=$(cat /export/home/test.md5); echo "$valu"'

  • but why not just use
Code:
ssh me@xxx.host.com cat /export/home/test.md5

  • Or perhaps you mean to do this:
Code:
valu=$(ssh me@xxx.host.com cat /export/home/test.md5)
echo "$valu"

  • I do not understand why you use tee, since you are not redirecting its output.
# 3  
Old 08-22-2015
Sorry for not being clear on my initial post. Below are the details:

jar=test.jar
md5=test.jar.md5

now i want to do the below:

#1. md5sum test.jar (exists in remote server x)
#2. cat test.jar.md5 (exists in remote server x)
#3. Now from my local machine i want to ssh to the remote server x and then want to check whether md5sum is matching

I tried the below:

==================
Code:
#!/bin/bash -x

ssh datac@x.host.com cat /home/data/test.jar.md5


ssh datac@x.host.com "if [ `cat test.jar.md5` == `md5sum test.jar` ]; then exit 0; else exit 1; fi"

========================
output:
-----------
Code:
$ ./x
ce65f87aecee3d99cd2e947de5e4d4eacat: /home/data/test.jar.md5: Permission denied
md5sum: /home/data/test.jar: Permission denied

-- when i cat the file its displaying the result without any issue
-- but when i do the same inside the if condition its throwing permission error

Note: On the remote server I have all the required permissions to read and execute these jar and md5 files.

I tried alot of time on this but couldn't able to find the issue. Could someone help me here.

Thanks

Moderator's Comments:
Mod Comment Please use next time code tags for your cade and data, thanks

Last edited by vbe; 08-22-2015 at 02:21 PM..
# 4  
Old 08-22-2015
Hi, some more remarks:
  • Since you are using double quotes, your two command substitutions are being executed on your local computer. Try single quotes
  • == is not syntactically correct. a single = should be used
  • The if statement with the exit commands is reproducing the exact return codes that the test command would produce anyway, so you can just leave it out.
  • it is best to quote both sides of a string comparison in a test statement
  • Still consider using $(...) instead of `...` (unless you are using a classic Bourne shell). It saves you quoting troubles..

Combining all this into one, try something like:
Code:
ssh datac@x.host.com '[ "$(cat test.jar.md5)" = "$(md5sum test.jar)" ]'

You can add 2>/dev/null is you want to suppress messages..
# 5  
Old 08-23-2015
Thanks alot, it works now.
# 6  
Old 08-23-2015
Hi, good to hear,

In addition, if the files are in the same directory you do not need to use a test, you could also use:
Code:
ssh datac@x.host.com 'md5sum -c test.jar.md5'

or
Code:
ssh datac@x.host.com 'md5sum --status -c test.jar.md5'

(or alternatively use >/dev/null 2>&1) to quiesce the output and only use the return code..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remote script via SSH not executing

I have worked on multiple scenarios to execute remote script via ssh. This problem I am not able to resolve. 2 linux hosts. Server1, Server2 on Server1 I have script called ~/scripts/start_standalone.sh XXXX cd $JBOSS_HOME NODENAME=xyz; IP_ADDR=`hostname`; MGMT_IPADDR=`hostname`;... (3 Replies)
Discussion started by: oraclermanpt
3 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. Shell Programming and Scripting

Executing remote commands via ssh

Hi, I'm tryin to write a script that will collect information about a remote servers, put them into variables and print them to screen. # /usr/bin/bash ls $1 > /dev/null 2>/dev/null if then echo "$1 is file" for server in $(cat $1) do # echo $server ... (5 Replies)
Discussion started by: moshesa
5 Replies

4. Shell Programming and Scripting

executing commands in remote server using ssh

I have some commands which need to be executed in remote machine. I have Linux Server from where I need to connect to Solaris server using ssh and then declare some variable over there and run some commands. I don't want to call a script which is present in Solaris server from Linux server... (7 Replies)
Discussion started by: maitree
7 Replies

5. 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

6. Shell Programming and Scripting

Executing remote application using ssh without full reference to its location

Hello again :) , My script has an ssh command to run a script on a remote machine. The script has commands such as sqlplus and unzip. However, the return I get in my own terminal says it can't find sqlplus and unzip. the ssh command is: ssh user@host "cd ScriptDir; ./Script.sh" and the... (6 Replies)
Discussion started by: jangozo
6 Replies

7. UNIX for Dummies Questions & Answers

how to stay in remote shell after executing commands in ssh?

the ssh calling convention: ssh <server> If I put commands in the section, ssh will execute them immediately after logging in and return to local shell. I want to stay in the remote shell after executing these commands. How can I achieve this? Thanks for all. (1 Reply)
Discussion started by: hplonlien
1 Replies

8. Shell Programming and Scripting

Executing a script on a remote system via SSH

Hello all, I have a relatively simple script I wrote to generate a count of errors broken down. What I would like to do is execute this script from another server so that I don't actually have to log in to the server to run the check. The script on what we'll call "Server A" is: ... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

SSH Remote Server issue

I have a script that connects to remote servers using a public key. Some of the servers are not set up for the public key and I receive the following when I attempt to ssh: The authenticity of host 'XXX' can't be established. RSA key fingerprint is... (1 Reply)
Discussion started by: la_womn
1 Replies

10. Shell Programming and Scripting

Executing awk in a remote server using ssh

Hello Everybody, I'm facing a weird problem with the awk command. I'm trying to execute a simple awk command as follows, echo 1 2 | awk '{print $2}' This command prints the output 2. When i try to execute the same command in a remote server using ssh as follows, ssh user@host... (2 Replies)
Discussion started by: karthikv
2 Replies
Login or Register to Ask a Question