Skip to content

Commit

Permalink
lib/ssh.sh: Add
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Dec 14, 2022
1 parent 2fed4bb commit a61e867
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
6 changes: 3 additions & 3 deletions examples/date.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ main() {
;;
format)
flag_nonemptyarg && shift "$FLAGSHIFT"
DATE_FORMAT="$FLAGARG"
DATE_FORMAT=$FLAGARG
;;
o|output)
shift "$FLAGSHIFT"
OUTPUT="${FLAGARG:-date-out.txt}"
OUTPUT=${FLAGARG:-date-out.txt}
if [ "$OUTPUT" != - ]; then
exec >"$OUTPUT"
exec >$OUTPUT
fi
;;
*)
Expand Down
46 changes: 45 additions & 1 deletion lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ mdtocsubst() {
case "$FLAG" in
h|help)
mdtocsubst_help
return 0
return 1
;;
skip)
flag_nonemptyarg && shift "$FLAGSHIFT"
Expand Down Expand Up @@ -1198,6 +1198,50 @@ ensure_prefix_sh_c() {
fi
}
#!/bin/sh
if [ "${LIB_SSH-}" ]; then
return 0
fi
LIB_SSH=1

ssh_copy_id_help() {
cat <<EOF
usage: ssh_copy_id -i=id.pub host
EOF
}

ssh_copy_id() {
while flag_parse "$@"; do
case "$FLAG" in
h|help)
ssh_copy_id_help
return 1
;;
i)
flag_nonemptyarg && shift "$FLAGSHIFT"
ID_PUB_PATH=$FLAGARG
;;
*)
flag_errusage "unrecognized flag $FLAGRAW"
;;
esac
done
shift "$FLAGSHIFT"

if [ -z "${ID_PUB_PATH-}" ]; then
flag_errusage "-i for id.pub is mandatory"
fi

if [ $# -ne 1 ] ; then
flag_errusage "only one argument for the remote host is accepted"
fi

REMOTE_HOST=${1-}
sh_c ssh-copy-id -fi "$ID_PUB_PATH" "$REMOTE_HOST"
sh_c ssh "$REMOTE_HOST" 'cat .ssh/authorized_keys \| sort -u \> .ssh/authorized_keys.dedup'
sh_c ssh "$REMOTE_HOST" 'cp .ssh/authorized_keys.dedup .ssh/authorized_keys'
sh_c ssh "$REMOTE_HOST" 'rm .ssh/authorized_keys.dedup'
}
#!/bin/sh
if [ "${LIB_TEMP-}" ]; then
return 0
fi
Expand Down
2 changes: 1 addition & 1 deletion lib/misc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mdtocsubst() {
case "$FLAG" in
h|help)
mdtocsubst_help
return 0
return 1
;;
skip)
flag_nonemptyarg && shift "$FLAGSHIFT"
Expand Down
46 changes: 46 additions & 0 deletions lib/ssh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/sh
if [ "${LIB_SSH-}" ]; then
return 0
fi
LIB_SSH=1
. ./log.sh
. ./flag.sh

ssh_copy_id_help() {
cat <<EOF
usage: ssh_copy_id -i=id.pub host
EOF
}

ssh_copy_id() {
while flag_parse "$@"; do
case "$FLAG" in
h|help)
ssh_copy_id_help
return 1
;;
i)
flag_nonemptyarg && shift "$FLAGSHIFT"
ID_PUB_PATH=$FLAGARG
;;
*)
flag_errusage "unrecognized flag $FLAGRAW"
;;
esac
done
shift "$FLAGSHIFT"

if [ -z "${ID_PUB_PATH-}" ]; then
flag_errusage "-i for id.pub is mandatory"
fi

if [ $# -ne 1 ] ; then
flag_errusage "only one argument for the remote host is accepted"
fi

REMOTE_HOST=${1-}
sh_c ssh-copy-id -fi "$ID_PUB_PATH" "$REMOTE_HOST"
sh_c ssh "$REMOTE_HOST" 'cat .ssh/authorized_keys \| sort -u \> .ssh/authorized_keys.dedup'
sh_c ssh "$REMOTE_HOST" 'cp .ssh/authorized_keys.dedup .ssh/authorized_keys'
sh_c ssh "$REMOTE_HOST" 'rm .ssh/authorized_keys.dedup'
}

0 comments on commit a61e867

Please sign in to comment.