The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check if a directory exists if not make it musicmancanora Shell Programming and Scripting 20 03-25-2009 04:22 PM
How to check if database exists? Fatbob Shell Programming and Scripting 2 09-10-2008 03:23 PM
How can I check if directory exists in a makefile zivsegal UNIX for Dummies Questions & Answers 2 09-10-2007 04:12 AM
how to check if masked directory exists? philplasma Shell Programming and Scripting 4 05-30-2007 09:46 PM
check if directory exists jerardfjay Shell Programming and Scripting 2 06-13-2005 03:26 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-23-2008
Sara-sh Sara-sh is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 9
my scripts does not check if directory exists

Hello:

Can someone please help me figure out what is wrong here, my script does not move on to the "else" part even though there is no .ssh directory on my remote server:

$more putkey.sh
#!/bin/ksh

for server in `cat list`
do
if [ -d /.ssh ]; then
cat $HOME/.ssh/id_rsa.pub |ssh $server ' cat >> .ssh/authorized_keys && echo "key copied"'
else
cat $HOME/.ssh/id_rsa.pub |ssh $server ' mkdir .ssh && cat >> .ssh/authorized_keys && echo "key copied"'
fi
done


$./putkey.sh
Password:
sh: .ssh/authorized_keys: cannot create


The script works if the else part is executed:

$cat $HOME/.ssh/id_rsa.pub |ssh myserver ' mkdir .ssh && cat >> .ssh/authorized_keys && echo "key copied"'
Password:
key copied


Thanks.
  #2 (permalink)  
Old 09-23-2008
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
Are you trying to check if the /.ssh directory exists locally? Or are you trying to check if the /.ssh directory exists on the remote server?
  #3 (permalink)  
Old 09-23-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,432
Quote:
Originally Posted by Sara-sh View Post
..my script does not move on to the "else" part even though there is no .ssh directory on my remote server:
And what part of your script check if the directory exist on remote ?
  #4 (permalink)  
Old 09-23-2008
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
Maybe you need to check this:

Code:
ssh $server ls /$HOME/.ssh
If it returns
Code:
/$HOME/.ssh: No such file or directory
then do stuff
else do other stuff.
  #5 (permalink)  
Old 09-23-2008
Gee-Money Gee-Money is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 48
I made a script a while back that does almost the same thing, but the usage is a little different,

"./ssh-putkey.sh -H server" is the most basic usage, but if you do a "-h", it will show you the complete usage.

I'm not real sure if Ksh has the getopts built-in for parsing command line options, as i wrote this for bash, but it should work.

Code:
#!/bin/bash

### defaults
ID_FILE="$HOME/.ssh/id_dsa"
username=$USER
rsa_or_dsa="dsa"

usage_line="$0 -H <host>\nOptions:\n\t-?|-h Display this Output\n\t-l <user>; Default == current user\n\t-f <identity file>; Default == '~/.ssh/id_dsa'\n\t-t <rsa|dsa> ***Only when generating new file; Default == $rsa_or_dsa"
### Start function
USAGE() {
        clear 
        echo -e "Usage: $usage_line"
        echo -e "\nIf you use the '-f' option,\n it will generate the file if it doesn't exist."
        echo -e "\nYou will have to enter the remote password once,\n ...but never again after that.\n"
        exit 2
                }
### End Function

### if no args, complain
[ "$#" -lt "1" ] && USAGE

### set command line options
while getopts ":f:t:H:l:?h" flag; do
        case $flag in
            f )  ID_FILE=$OPTARG ;;
            t ) rsa_or_dsa=$OPTARG ;;
            H ) remote_host=$OPTARG ;;
            l ) username=$OPTARG ;;
            ?|h )  USAGE ;;
        esac
done

### make sure options are set correctly, or complain
if [ -z $remote_host ]; then
        clear
        echo -e "\nYou need to specify a valid hostname or ip-address,\nof the remote host you want to connect to.\n"
        USAGE
        exit 1
elif [ $rsa_or_dsa != "rsa" -a $rsa_or_dsa != "dsa" ]; then
        clear
        echo -e "\nWhoa....the '-t' needs to be either 'rsa', or 'dsa'....not '$rsa_or_dsa', as you put it.\n\n"
        exit 1
fi

### make key files if not already there
[ -e "$ID_FILE" ] || ssh-keygen -t $rsa_or_dsa -f $ID_FILE || exit 1

### read pub file, and complain if it cant be read
pub_key=`cat $ID_FILE.pub`
[ -z "$pub_key" ] && (clear ; echo -e "$ID_FILE.pub could not be read...exiting.\n") && exit 1

### Start Function
SSH_PUT_KEY() {
        ssh $username@$remote_host "[ ! -d \$HOME/.ssh/ ] && mkdir \$HOME/.ssh/ ; echo $pub_key >> \$HOME/.ssh/authorized_keys"
                }
### End Function

### run function
SSH_PUT_KEY
It may look kinda big, and unfriendly, but it has never failed me, and it is very easy to use
, and it is commented so it shouldn't be that hard to modify it to suit your exact needs.

Have Fun!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0