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


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 2 Weeks Ago
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.
Example data:
Code:
User: user0815
Password: pwd0815
Host: example.com

Path of file on remote server: /htdocs/www/my/folder/file.jpg

Thank you very much.
Jens


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 2 Weeks Ago at 09:24 AM.. Reason: Added CODE tags.
# 2  
Old 2 Weeks Ago
Welcome to the forum.


Any attempts / ideas / thoughts from your side? Where are you stuck?
# 3  
Old 2 Weeks Ago
Welcome Jens885544,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • Why must SSH using id/password be used for the connection?
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using? The output of uname -a on both the client and the server would be useful here.
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.



Kind regards,
Robin
# 4  
Old 2 Weeks Ago
Hey guys. You are totally right so far. I forgot to add my try.
The problem was how to add the credentials. Well the problem still exists but now I was able to add the public key to the webserver.
So now it is working without entering user and password.

The so far working code is:
Code:
FILETOCHECK=/test.txt
if ssh 'user0815@example.com' stat $FILETOCHECK \> /dev/null 2\>\&1
            then
                    echo "File exists"
            else
                    echo "File does not exist"

fi

Thank you.
I was not able to do with pwd authentication. If you know how to do so, please post it, because then we have a 100% solution on the topic.
Jens




Moderator's Comments:
Mod Comment Seriously: Please use CODE tags as required by forum rules!

Last edited by RudiC; 2 Weeks Ago at 10:55 AM.. Reason: Added CODE tags.
# 5  
Old 2 Weeks Ago
Quote:
Originally Posted by Jens885544
Hey guys. You are totally right so far. I forgot to add my try.
The problem was how to add the credentials. Well the problem still exists but now I was able to add the public key to the webserver.
So now it is working without entering user and password.
Congratulation. That is an easy, short, comfortable, and, before all, safe solution, working as intended by the ssh creators (at least I hope so).

Quote:
I was not able to do with pwd authentication. If you know how to do so, please post it, because then we have a 100% solution on the topic. ...
Why do you want (or have to?) fall back to a complicated and unsafe workaround?
# 6  
Old 2 Weeks Ago
Quote:
Originally Posted by RudiC
Why do you want (or have to?) fall back to a complicated and unsafe workaround?
Not for me, but maybe somebody would need it and expects it because its in the headline.

But maybe you can give me a hint: I now build in the working code into my script. The script does a foreach on 7000 files. Is it a recommended way to do
"if ssh 'ssh-user0815@example.com' stat $FILETOCHECK \> /dev/null 2\>\&1"
on every file? I ask because to me it looks like it builds then 7000 times a ssh connection.
Is there a way to build it once and then check the files?

Thank you.
Jens
# 7  
Old 2 Weeks Ago
It's difficult to say, in a way. What do you want to do with the output from the ssh command? Because the output will be in a standard format, you might be able to call stat just once for all of them. The output will be one long trail, but there is a clear marker for the file name, size and other information. You can even direct it to format in a way most useful to you.

If you fancy a fairly terse output, you could try ssh $username@target_server "stat -t $files_to_check" or push it even further with ssh $username@target_server "stat -tc '%n' $files_to_check" which will give you the filenames that exist or a message something like stat: cannot stat `not_a_file': No such file or directory as one big list of output.


What do you eventually want to get out of it?

Are any files likely to be a symbolic link? You might need to add the -L flag to follow links to check them.




Does this help,
Robin

Last edited by rbatte1; 2 Weeks Ago at 11:44 AM.. Reason: Corrected formatting definition for last suggestion.
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Check if file exists via ssh in ssh (nested) say170 Shell Programming and Scripting 2 10-23-2014 04:31 PM
Except script fails to check file exists or not in remote node Sudhakar333 Shell Programming and Scripting 4 09-02-2014 05:13 PM
File Check in remote server ChandanN Shell Programming and Scripting 3 08-13-2014 02:55 PM
Unable to check if file exists on remote server using expect temp_user Shell Programming and Scripting 6 04-02-2014 04:47 AM
ssh to remote server and check if file exists jimbojames Shell Programming and Scripting 4 09-03-2012 08:09 PM
C shell scripting, check if link exists on remote servers CDi Shell Programming and Scripting 0 12-28-2011 03:49 PM
csh Check if file exists on remote system meteorologistks Shell Programming and Scripting 3 10-07-2011 11:40 AM
Check if remote folder exists funonnet Shell Programming and Scripting 0 03-10-2011 05:21 AM
Check file exists on remote machine. marpadga18 Shell Programming and Scripting 2 10-24-2010 02:41 PM
ftp - check if file on remote exists (skip overriding) spiriad UNIX for Dummies Questions & Answers 3 07-30-2010 05:25 AM
Problem with ftp- how to check if the remote directory exists.? anijan Shell Programming and Scripting 2 03-02-2010 11:50 PM
check if remote file exists hcclnoodles Shell Programming and Scripting 2 08-27-2008 05:53 PM
check if file exists on remote system ? hcclnoodles Shell Programming and Scripting 2 10-26-2006 04:08 AM
Need Script to check whether user exists in the remote machine Srini75 SCO 1 09-07-2005 11:23 AM
Check Remote Folder Exists borncrazy Shell Programming and Scripting 1 07-12-2004 04:15 PM