Sponsored Content
Top Forums Shell Programming and Scripting my scripts does not check if directory exists Post 302239464 by Gee-Money on Tuesday 23rd of September 2008 06:12:18 PM
Old 09-23-2008
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!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

check if directory exists

Hi, I need to prompt for a response from a user to enter a path read dest_dir?"Please Enter Directory :" How do I do this until a valid directory is entered by the user. I can use to check the existence of the directory. However when I try the following I cannot get it to work. while ... (2 Replies)
Discussion started by: jerardfjay
2 Replies

2. Shell Programming and Scripting

how to check if masked directory exists?

I'm trying to write a script that identifies whether a directory of the form AWL.????????.IP exists. I have one that exists which is AWL.05301032.IP. When I test like this: If ] I get true, but when I test like this: If ] Or like this If ] Or any other variation of wild cards, I... (4 Replies)
Discussion started by: philplasma
4 Replies

3. UNIX for Dummies Questions & Answers

How can I check if directory exists in a makefile

Hi. I what to write a make file that inside a target I want to check if a directory exists. some like: ### make a: if ;then <command 1> else <command 2> fi ### make end Thanks a lot ---------------------- (2 Replies)
Discussion started by: zivsegal
2 Replies

4. Shell Programming and Scripting

check if a directory exists if not make it

Hey guys im trying to check if the directory exists i get a syntax error on the elif statement iv tried using else and still same result im not sure. If the directory does not exist can i just insert mkdir /tmp/old under the elif once that part gets working if ; then #do nothing elif echo... (20 Replies)
Discussion started by: musicmancanora
20 Replies

5. Shell Programming and Scripting

check if directory and file exists

cp $PATHLOGS/$DATE/*.* $TMP/logs_tmp/ cp $PATHLOGS/$DATE1/*.* $TMP/logs_tmp/ Before copying the files I have to check if the directory $DATE1 and $DATE2 exists. If directory exists then, check if the folder contains some files. if the file exists then, check if the file size is greater... (3 Replies)
Discussion started by: sandy1028
3 Replies

6. Shell Programming and Scripting

How to check if a file exists in a directory?

I want to perform SQL *Loader operation only if a file named "load.txt" exists in a directory "/home/loc/etc". Please help how to check this with a if condition. (8 Replies)
Discussion started by: vel4ever
8 Replies

7. Shell Programming and Scripting

how to check if a directory exists or not.if not need to create it

Hi, I am using solaris 10 OS and bash shell.just checking for small code snippet which follows below. /export/home/vomappservers/spa/common/5.0 /export/home/vomappservers/spa/common/scripts /export/home/vomappservers/spa/tools /export/home/vomappservers/spa/scm5.0/SCCS... (5 Replies)
Discussion started by: muraliinfy04
5 Replies

8. Shell Programming and Scripting

How to check if the file exists in directory?

Hi Gurus, I have a requests to find if all the file in the filelist exist in certain directory. example: my filelist abc def ddd cde afg how can I find these 5 files exists at director /home/abc Thanks in advance (7 Replies)
Discussion started by: ken6503
7 Replies

9. Shell Programming and Scripting

Check whether file exists in directory

Hi guys, I am beginner trying to learn unix. So any help is welcomed. My requirement is to check whether is a file exists in a particular directory or not. The directory path and filename are taken dynamically with user interaction. So the program should continue only if the $filename... (1 Reply)
Discussion started by: maris_markur
1 Replies

10. UNIX for Beginners Questions & Answers

Check that at least one file exists in the directory.

There are some files with suffix dates like abc_20032019.dat abc_17032019.dat If at least one file exists then perform some operation else exit from execution. Korn shell ---------------------------------- array=($inputdir/abc*.dat) If ] ] then echo " file exits" else echo " file does... (10 Replies)
Discussion started by: Rajesh123
10 Replies
SSH-COPY-ID(1)						      General Commands Manual						    SSH-COPY-ID(1)

NAME
ssh-copy-id - install your public key in a remote machine's authorized_keys SYNOPSIS
ssh-copy-id [-i [identity_file]] [user@]machine DESCRIPTION
ssh-copy-id is a script that uses ssh to log into a remote machine and append the indicated identity file to that machine's ~/.ssh/autho- rized_keys file. If the -i option is given then the identity file (defaults to ~/.ssh/id_rsa.pub) is used, regardless of whether there are any keys in your ssh-agent. Otherwise, if this: ssh-add -L provides any output, it uses that in preference to the identity file. If the -i option is used, or the ssh-add produced no output, then it uses the contents of the identity file. Once it has one or more fin- gerprints (by whatever means) it uses ssh to append them to ~/.ssh/authorized_keys on the remote machine (creating the file, and directory, if necessary.) NOTES
This program does not modify the permissions of any pre-existing files or directories. Therefore, if the remote sshd has StrictModes set in its configuration, then the user's home, ~/.ssh folder, and ~/.ssh/authorized_keys file may need to have group writability disabled manu- ally, e.g. via chmod go-w ~ ~/.ssh ~/.ssh/authorized_keys on the remote machine. SEE ALSO
ssh(1), ssh-agent(1), sshd(8) OpenSSH 14 November 1999 SSH-COPY-ID(1)
All times are GMT -4. The time now is 04:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy