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:
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.
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:
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
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?
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.
For that many files, an ssh connection for every single one is pointless unless they all are on different servers. Should they be on one or few servers, and you just need to check their existence, why not use the sftp command, which is part of the ssh suite, to get a remote directory listing and evaluate that locally? Like