Check if file exists via ssh in ssh (nested)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if file exists via ssh in ssh (nested)
# 1  
Old 10-23-2014
Check if file exists via ssh in ssh (nested)

I'm using redhat and have an odd issue with a nested ssh call.

Code:
ssh -i ~/.ssh/transfer-key -q transfer@fserver1 [[ -f /home/S/outbox/complete ]] 
&& ssh -i ~/.ssh/transfer-key transfer@fserver1 "ssh -i ~/.ssh/sftp-key sftpin@10.0.0.1 [[ ! -f /datain/complete]]" 
&& ssh -i ~/.ssh/transfer-key transfer@fserver1 "scp -i ~/.ssh/sftp-key /home/S/outbox/* sftpin@10.0.0.1:/datain" 
&& ssh -i ~/.ssh/transfer-key transfer@server1 "rm /home/S/outbox/*"

what this is doing is checking if a complete file is in an outbox, if so, ssh to a transfer server, executing a second ssh and checking if a file is 'not' present on the destination and if no, sending the files. I'm restricted to which ids and servers can be used, hence this convoluted method.

I'm getting a syntax error on the part where I'm checking if the file doesn't exist on the target.

Code:
ksh: syntax error at line 1: `[[' unmatched

Any ideas how to check this in an embedded ssh call?

Thanks in advance

---------- Post updated at 10:19 AM ---------- Previous update was at 10:13 AM ----------

doh... missed a space at the end before ]]
only when I posted it here was it obvious.

Last edited by say170; 10-23-2014 at 12:15 PM.. Reason: added some CR to code to improve readability
# 2  
Old 10-23-2014
If you tunnel to this machine a lot, did you know you can setup ssh to do it for you.

So lets say this machine 10.0.0.1 is called saysdata.

If you put something like the below in your ~/.ssh/config file:

Code:
host fserver1
user transfer
IdentityFile ~/.ssh/transfer-key

host saysdata
user sftpin
IdentityFile ~/.ssh/sftp-key
ProxyCommand ssh fserver1 -W %h:%p

I haven't tried this with alternate identityfiles and this will require you to bring the sftp-key to your local machine. Perhaps someone with more experience in this area knows of a way to keep the identity file on the jump box.

Anyway the result should be that you will be able to simply type:

Code:
ssh saydata "[[ -f /datain/complete ]]"
scp fserver:/home/S/outbox saydata:/datain
ssh fserver1 "rm /home/S/outbox/*"

Without worrying about identity files or nested ssh and scp commands. Much easier and probably worth a little mucking around to get working if you do it semi-regular.
# 3  
Old 10-23-2014
Note that ssh is running a shell on the remote end, so the rules are most likely bash or ksh as sh does not do [[]], but not ssh. I guess 'ssh' as 'secure shell' is a misnomer, since it is not a shell but a remote execution tool.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

The Shell lost the inverted comma in a nested ssh command

Hi, i want use this Comand for my psql request sh ssh -o StrictHostKeyChecking=no rootatemailaddress.de sudo psql -U postgres -c "select pg_terminate_backend(pid) from pg_stat_activity where datnam=\'$DB\';"'" but the shell lost the inverted comma for datnam=\'$DB\'. The request deliver... (2 Replies)
Discussion started by: peterpane007
2 Replies

2. UNIX for Beginners Questions & Answers

How to check via SSH and credentials if file on remote server exists?

Hi there, I am sorry to ask that kind of beginner thing, but all the code I found online didnt work for me. All I want to do is: Check via SSH if a File exists on my webserver. The SSH login has to be with username and password. So I would be very thankful if somebody could write the line.... (8 Replies)
Discussion started by: Jens885544
8 Replies

3. UNIX for Beginners Questions & Answers

Ssh script to validate ssh connection to multiple serves with status

Hi, I want to validate ssh connection one after one for multiple servers..... password less keys already setup but now i want to validate if ssh is working fine or not... I have .sh script like below and i have servers.txt contains all the list of servers #/bin/bash for host in $(cat... (3 Replies)
Discussion started by: sreeram4
3 Replies

4. Shell Programming and Scripting

Problem with ssh and checking if file exists

Hi All, I am facing a problem while checking for existence of file over ssh ! Basically, i want to ssh and check if file exists.. If file exists return 1. If file does not exits return 0 (or any value) I am using the below code file_avail=`ssh username@host "if ]; then exit 1;... (10 Replies)
Discussion started by: galaxy_rocky
10 Replies

5. Shell Programming and Scripting

ssh to remote server and check if file exists

Hi everyone, I am trying to figure out a way to ssh to remote server and check if file exists, and if it doesn't I want to leave the script with an exit status of 5. I have the following that I am attempting to use, but it is not returning anything: check() { ssh ${SOURCE_SERV} "ls -l... (4 Replies)
Discussion started by: jimbojames
4 Replies

6. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

7. UNIX for Advanced & Expert Users

how to check if I'm allowed to ssh several hosts.

Hi guys! I'm working on a little script. I have a txtfile with several hosts, Unix team has copied my keys into several of those servers, but not all of them, I need to figure out which ones I don't have access to, (I want a list of servers I don't have access to, so I can request for it). This... (1 Reply)
Discussion started by: erick_tuk
1 Replies

8. Shell Programming and Scripting

could not send commands SSH session with Net::SSH::Expect

I am using Net::SSH::Expect to connect to the device(iLO) with SSH. After the $ssh->login() I'm able to view the prompt, but not able to send any coommands. With the putty I can connect to the device and execute the commands without any issues. Here is the sample script my $ssh =... (0 Replies)
Discussion started by: hansini
0 Replies

9. Solaris

Check SSH Connection .. help ..

I've script where i check connection to many hosts ... it take input from HOSTLIST.txt Ex. cat HOSTLIST.txt host1 host2 while read HOSTNAME do echo $HOSTNAME ssh -o "BatchMode=yes" ${HOSTNAME} "echo 2>&1" $$ echo flag=0 || flag=1; if ; then echo "No SSH... (3 Replies)
Discussion started by: prash184u
3 Replies
Login or Register to Ask a Question