LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-30-2011, 11:18 PM   #1
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Rep: Reputation: 0
Question 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!
 
Old 10-01-2011, 03:26 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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.
 
Old 10-01-2011, 01:39 PM   #3
kasl33
Member
 
Registered: Oct 2004
Location: USA
Distribution: Ubuntu-Server, Mac OS X, Arch Linux
Posts: 356

Rep: Reputation: 48
What macemoneta says should work. Keep us posted on whether or not you had any success please.
 
Old 10-02-2011, 10:26 PM   #4
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
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?
 
Old 10-02-2011, 10:47 PM   #5
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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.
 
Old 10-02-2011, 11:03 PM   #6
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by macemoneta View Post
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.
 
Old 10-02-2011, 11:06 PM   #7
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
It doesn't rename, it appends the key to authorized_keys. The authorized_keys file can contain many keys.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Trouble booting custom kernel: Root device 'xxx' doesn't exist. Attempting to create archnemesis Linux - Newbie 8 06-17-2011 04:09 AM
If directory doesn't exist, try the next one? - Which command to use? gqchynaboy Linux - Newbie 3 12-09-2009 06:24 PM
samba complains home directory doesn't exist??? lmcilwain Fedora 3 08-19-2007 01:36 PM
report that such directory doesn't exist hpaixao Programming 3 02-20-2005 11:30 AM
login error: home directory supposedly doesn't exist jmelton Linux - General 3 11-30-2003 01:42 PM


All times are GMT -5. The time now is 04:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration