LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ssh and create directory if it doesn't exist (https://www.linuxquestions.org/questions/linux-newbie-8/ssh-and-create-directory-if-it-doesnt-exist-905874/)

greenpool 09-30-2011 10:18 PM

ssh and create directory if it doesn't exist
 
Hi guys,

i finally figured out how to ssh and check if a directory exists. now i'd like to create a directory if it doesn't.

here's my script:

Code:

#!/bin/bash
#Purpose: ssh and create dir if it doesn't exist


echo $(date) >> dircheck.txt
for i in Server1  Server2  Server3  Server4
do
echo "$i"
RETURNVAL=1
ssh $i "[ -d ~/.ssh ]" && RETURNVAL=0


if [ $RETURNVAL -eq 0 ]
then
 echo "$i: .ssh dir does exist" >> dircheck.txt

else
 echo "$i: .ssh dir doesn't exist" >> dircheck.txt
 fi

done
echo "" >> dircheck.txt
cat dircheck.txt


i tried creating the the dir in the else if dir doesn't exist statement but obviously that doesn't work. (ut created it in the local server)

how can i do this? any hint, tips, code etc. is much appreciated!

macemoneta 10-01-2011 02:26 AM

You can just:
Code:

ssh $i /bin/mkdir -p .ssh
The '-p' option of mkdir will create the directory if it doesn't exist, without raising an error.

kasl33 10-01-2011 12:39 PM

What macemoneta says should work. Keep us posted on whether or not you had any success please.

greenpool 10-02-2011 09:26 PM

thanks guys that works but i do want to do a little bit more.

how would i incorporate the following:

scp ~/.ssh/id_dsa.pub server1:~/.ssh/authorized_keys

so basically the logic is:

1. create directory if it doesn't exist on server1 which now i know can be done via:

Code:

ssh $i /bin/mkdir -p .ssh
2. once directory is created copy id_dsa.pub as authorized_keys to .ssh folder in server1

which also can be done via:

Code:

scp ~/.ssh/id_dsa.pub server1:~/.ssh/authorized_keys

so my questions is how do i combine the two in a script? :)

macemoneta 10-02-2011 09:47 PM

You just need to:
Code:

ssh-copy-id server1
It will take care of it. Also, you should be using RSA keys; DSA keys are considered obsolete. Substantial weaknesses have been found in DSA.

greenpool 10-02-2011 10:03 PM

Quote:

Originally Posted by macemoneta (Post 4488551)
You just need to:
Code:

ssh-copy-id server1
It will take care of it. Also, you should be using RSA keys; DSA keys are considered obsolete. Substantial weaknesses have been found in DSA.

Cool thanks.

I did:

Code:

ssh-copy-id -i ~/.ssh/id_dsa.pub server1

quick question, does this always rename the file id_dsa.pub to authorized_keys in the remote server?

will look into rsa too, thanks.

macemoneta 10-02-2011 10:06 PM

It doesn't rename, it appends the key to authorized_keys. The authorized_keys file can contain many keys.


All times are GMT -5. The time now is 06:50 PM.