rename script, improve loggin

This commit is contained in:
Jakobus Schürz 2020-09-24 13:05:26 +02:00
parent ee685d5429
commit e36adb3c71
4 changed files with 31 additions and 13 deletions

View file

@ -0,0 +1,14 @@
#!/bin/bash
function _sshidentities() {
[ -z "${SSH_IDENTITIES_DIR+x}" ] && { SSH_IDENTITIES_DIR="${SSH_IDENTITIES_DEFAULT_DIR-${HOME}/.ssh/identities}"; export SSH_IDENTITIES_DIR; }
COMPREPLY=()
local CUR KEYS
CUR="${COMP_WORDS[COMP_CWORD]}"
KEYS="$(find $SSH_IDENTITIES_DEFAULT_DIR -maxdepth 1 -mindepth 1 -type d -exec basename {} \;)"
COMPREPLY=( $(compgen -W "${KEYS}" -- ${CUR}) )
return 0
}
complete -F _sshidentities startagent loadagent

View file

@ -72,7 +72,7 @@ while :; do
exit 1
;;
*)
# ssh_identity=${1-${SSH_DEFAULT_IDENTITY-default}}
ssh_identity=${1-${SSH_DEFAULT_IDENTITY}}
break
;;
esac
@ -211,12 +211,15 @@ agent-load-identity-keys () {
# fi
# fi
#
logdebug "fingerprints: ${fingerprints[*]}"
logdebug "fingerprints from loaded keys:"
for f in ${fingerprints[*]};do
logdebug "$f"
done
if ! $tokenonly ; then
for key in $(ls ${SSH_IDENTITIES_DIR}/${ssh_identity}/id_*|grep -v "pub$\|so$\|config$\|public$"); do
fingerprint=$(ssh-keygen -l -f $key|awk '{print $2}')
if [[ ${fingerprints[*]} =~ "$fingerprint" ]]; then
logdebug "key: $key is loaded" >&2
logdebug "key: $(basename $key) (with fp $fingerprint) is loaded" >&2
if $readd || $remove ; then
$readd && logwarn "readd key $key" >&2
$remove && logwarn "remove key $key" >&2
@ -225,7 +228,7 @@ agent-load-identity-keys () {
fi
else
logwarn "key: $key is not loaded -> load it" >&2
loginfo "$(ssh-runinagent $agentfile ssh-add ${SSH_ADD_OPTIONS} -d ${key} 2>&1)"
#loginfo "$(ssh-runinagent $agentfile ssh-add ${SSH_ADD_OPTIONS} -d ${key} 2>&1)"
$remove || loginfo "$(ssh-runinagent $agentfile ssh-add ${SSH_ADD_OPTIONS} ${key} 2>&1)"
fi
done
@ -234,7 +237,7 @@ agent-load-identity-keys () {
for token in $(ls ${SSH_IDENTITIES_DIR}/${ssh_identity}/*|grep "\.so$"); do
tokenfingerprint="$(ssh-keygen -l -D $token|tr -s ' '|awk '{print $2}')"
if [[ ${fingerprints[*]} =~ "$tokenfingerprint" ]]; then
logdebug "token: $token is loaded" >&2
logdebug "token: $token ($fingerprint) is loaded" >&2
if $readd || $remove ; then
$readd && logwarn "readd token $token" >&2
$remove && logwarn "remove token $token" >&2
@ -248,7 +251,8 @@ agent-load-identity-keys () {
fi
done
fi
logdebug "current loaded keys: $(ssh-runinagent $agentfile ssh-add -l)"
logdebug "currently loaded keys:
$(ssh-runinagent $agentfile ssh-add -l)"
else
logwarn "ssh-identity $ssh_identity is not configured. Please create $identitydir and add keys"
fi

View file

@ -770,7 +770,7 @@ token(){
# Usage:
# token <identity> will load token in agent. does nothing, if token is already loaded
# token -r|-f|--reload-token <identity> will remove token from agent and add it again (if plugged off and plugged in again
ssh-agent-start-or-restart -t $1 $2
startagent -t $1 $2
}
tokenold () {
@ -866,7 +866,7 @@ token-list-objects() {
loadagent() {
ENTRY
local af
af=$(ssh-agent-start-or-restart --create-only $1 )
af=$(startagent --create-only $1 )
loginfo "Load agent from $af"
eval $(<$af)
# . $af

10
logging
View file

@ -65,7 +65,7 @@ function logerror(){
declare -a loglevels
loglevels=()
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
echo $opts $msg >&2
echo $opts "$msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [ERROR] $msg" >> $SCRIPT_LOG
@ -86,7 +86,7 @@ function logwarn(){
declare -a loglevels
loglevels=("ERROR" )
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
echo $opts $msg >&2
echo $opts "$msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [WARN] $msg" >> $SCRIPT_LOG
@ -107,7 +107,7 @@ function loginfo(){
declare -a loglevels
loglevels=("ERROR" "WARN" )
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
echo $opts $msg >&2
echo $opts "$msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [INFO] $msg" >> $SCRIPT_LOG
@ -129,7 +129,7 @@ function logdebug(){
declare -a loglevels
loglevels=("ERROR" "WARN" "INFO")
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
echo $opts $msg >&2
echo $opts "$msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [DEBUG] $msg" >> $SCRIPT_LOG
@ -150,7 +150,7 @@ function logtrace(){
declare -a loglevels
loglevels=("ERROR" "WARN" "INFO" "DEBUG")
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
echo $opts $msg >&2
echo $opts "$msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [TRACE] $msg" >> $SCRIPT_LOG