myshellconfig/functions.sh

851 lines
23 KiB
Bash
Raw Normal View History

# Initialize variables, if not set
[ -z ${TMUX_SESSION_DIRS+x} ] && TMUX_SESSION_DIRS=( ~/.config/tmux/sessions ~/.local/share/tmux/sessions ~/.tmux/sessions)
[ -z ${SETPROXY_CREDS_DIRS+x} ] && SETPROXY_CREDS_DIRS=(~/.config/proxycreds)
[ -z ${KERBEROS_CONFIG_DIRS+x} ] && KERBEROS_CONFIG_DIRS=(~/.config/kinit)
[ -z ${ENCFS_CONFIG_DIRS+x} ] && ENCFS_CONFIG_DIRS=(~/.config/encfs)
export TMUX_SESSION_DIRS SETPROXY_CREDS_DIRS KERBEROS_CONFIG_DIRS
## this function updates in combination with PROMPT_COMMAND the shell-environment-variables in tmus-sessions,
# every time prompt is called. It does it only, when called from tmux (Environment TMUX is set)
function prompt_command() {
2020-09-23 10:19:50 +02:00
# [ -z "${TMUX+x}" ] || eval "$(tmux show-environment -s)"
if [ -n "${TMUX}" ]; then
eval "$(tmux show-environment -s)"
fi
}
PROMPT_COMMAND=prompt_command
2020-09-21 20:34:28 +02:00
2020-02-12 12:46:50 +01:00
cpb() {
scp "$1" ${SSH_CLIENT%% *}:~/Work
}
sudo() {
local SUDO
SUDO=$( if [ -e /bin/sudo ]; then echo /bin/sudo; else echo /usr/bin/sudo; fi )
2020-07-06 10:28:14 +02:00
$SUDO \
2020-07-17 00:58:45 +02:00
GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
GIT_COMMITTER_EMAIL="$GIT_COMMITTER_EMAIL" \
GIT_COMMITTER_NAME="$GIT_COMMITTER_NAME" \
TMUX="$TMUX" \
SSHS="$SSHS" \
SSH_TTY="$SSH_TTY" \
SSH_AUTH_SOCK="$SSH_AUTH_SOCK" \
2020-07-06 10:28:14 +02:00
"$@"
}
create_symlinks() {
#echo MYSHELLCONFIG_BASE: $MYSHELLCONFIG_BASE
# MYSHELLCONFIG_BASEDIR="$1"
# DIR="$(basename ${MYSHELLCONFIG_BASEDIR})"
# cd "${MYSHELLCONFIG_BASEDIR}"
cd ${MYSHELLCONFIG_BASE}
#echo "DIR MYSHELLCONFIG_BASEDIR $DIR $MYSHELLCONFIG_BASEDIR"
git config credential.helper 'cache --timeout=300'
#Anlegen von Symlinks
rm -rf ~/.vimrc ~/.vim ~/bashrc_add ~/.gitconfig ~/.tmux.conf ~/.tmux
ln -sf "${MYSHELLCONFIG_BASE}/vimrc" ~/.vimrc
ln -sf "${MYSHELLCONFIG_BASE}/vim" ~/.vim
ln -sf "${MYSHELLCONFIG_BASE}/.gitconfig" ~/.gitconfig
2020-01-15 01:26:48 +01:00
#ln -sf "${MYSHELLCONFIG_BASE}/bashrc_add" ~/bashrc_add
ln -sf "${MYSHELLCONFIG_BASE}/tmux" ~/.tmux
ln -sf "${MYSHELLCONFIG_BASE}/tmux/tmux.conf" ~/.tmux.conf
# Configure to use githooks in .githooks, not in standardlocation .git/hooks
$SGIT config core.hooksPath .githooks
# remove all old symlinks in .githooks and relink files from .githooks to .git/hooks
# don't know, why i do it here. TODO: Check it
find .git/hooks -type l -exec rm {} \; && find .githooks -type f -exec ln -sf ../../{} .git/hooks/ \;
cd ~-
}
setproxy () {
ENTRY
local CONFIG
case $# in
0)
2020-09-22 01:28:29 +02:00
logwarn "too few arguments"
return
;;
*)
if [ -z ${SETPROXY_CREDS_DIRS+x} ] ; then
2020-09-22 01:28:29 +02:00
logwarn "are you sure, SETPROXY_CREDS_DIRS is defined?"
return 1
else
CONFIG=$(find ${SETPROXY_CREDS_DIRS[*]} -mindepth 1 -name "$1.conf" -print -quit 2>/dev/null )
fi
;;
esac
if [ -e ${CONFIG} ]; then
2020-09-22 01:28:29 +02:00
loginfo -n "${CONFIG} existing: "
source "${CONFIG}"
2020-09-22 01:28:29 +02:00
loginfo "sourced"
export PROXY_CREDS="${PROXY_USER}:${PROXY_PASS}@"
else
2020-09-22 01:28:29 +02:00
loginfo "${CONFIG} not existing"
export PROXY_CREDS=""
fi
export {http,https,ftp}_proxy="http://${PROXY_CREDS}${PROXY_SERVER}:${PROXY_PORT}"
export {HTTP,HTTPS,FTP}_PROXY="http://${PROXY_CREDS}${PROXY_SERVER}:${PROXY_PORT}"
EXIT
}
mencfs () {
ENTRY
2020-09-22 01:28:29 +02:00
[ $# -eq 0 ] && { logwarn "too few arguments" >&2; return 1; }
local PKEY
local ENCDIR
local DESTDIR
local PASS=$(which pass 2>/dev/null || exit 127 )
local ENCFS=$(which encfs 2>/dev/null || exit 127 )
local CONFIG
if [ -z ${ENCFS_CONFIG_DIRS+x} ] ; then
2020-09-22 01:28:29 +02:00
logwarn "are you sure, ENCFS_CONFIG_DIRS is defined?"
EXIT
return 1
else
CONFIG=$(find ${ENCFS_CONFIG_DIRS[*]} -mindepth 1 -name "$1.conf" -print -quit 2>/dev/null )
fi
if [ -e ${CONFIG} ]; then
2020-09-22 01:28:29 +02:00
loginfo -n "${CONFIG} existing: "
source "${CONFIG}"
2020-09-22 01:28:29 +02:00
loginfo "sourced"
else
2020-09-22 01:28:29 +02:00
loginfo "${CONFIG} not existing"
EXIT
return 2
fi
[ -z ${PKEY+x} ] && { EXIT; return 3; }
[ -z ${ENCDIR+x} ] && { EXIT; return 4; }
[ -z ${DESTDIR+x} ] && DESTDIR="$(dirname $ENCDIR)/$(basename $ENCDIR| tr '[:lower:]' '[:upper:]'| sed -e 's/^\.//')"
2020-09-22 01:28:29 +02:00
$PASS "${PKEY}" 1>/dev/null 2>&1 || { logerror "entry $PKEY does not exist in passwordsotre"; return 5; }
local ENCFS_PASSWORD=$($PASS "${PKEY}" | head -n1)
if [ -z ${ENCDIR+x} -a -d ${ENCDIR} ];then
2020-09-22 01:28:29 +02:00
logerror "no encrypted directory found -> exit"
EXIT
return 4
else
2020-09-22 01:28:29 +02:00
loginfo "mount encrypted directory $ENCDIR on $DESTDIR"
$ENCFS -S $ENCDIR $DESTDIR <<!
$ENCFS_PASSWORD
!
if [ $? ]; then
2020-09-22 01:28:29 +02:00
loginfo "open $DESTDIR"
xdg-open $DESTDIR
fi
fi
EXIT
}
uencfs () {
ENTRY
local FUSERMOUNT=$(which fusermount 2>/dev/null || exit 127 )
2020-04-09 21:26:24 +02:00
local i
[ -z ${FUSERMOUNT+x} ] && return 127
if [ $# -eq 1 ]; then
if [ ! -d ${1} ];then
2020-09-22 01:28:29 +02:00
logwarn "encrypted directory ${1} not found -> exit" >&2
EXIT
return 128
else
2020-09-22 01:28:29 +02:00
loginfo "umount encrypted directory" $1 >&2
2020-04-09 21:26:24 +02:00
sync
$FUSERMOUNT -u "$1"
fi
else
2020-09-22 01:28:29 +02:00
loginfo "no arguments given. Umount all mounted encfs-dirs" >&2
for i in $(mount|grep encfs|sed -e 's/^encfs on \(.*\)\ type.*$/\1/');do
2020-09-22 01:28:29 +02:00
loginfo "$FUSERMOUNT -u $i"
2020-04-09 21:26:24 +02:00
sync
$FUSERMOUNT -u "$i"
done
EXIT
return 1
fi
EXIT
}
kinit-custom () {
ENTRY
local PKEY
local REALM
local PASS=$(which pass 2>/dev/null || exit 127 )
local KINIT=$(which kinit 2>/dev/null || exit 127 )
local CONFIG
if [ -z ${KERBEROS_CONFIG_DIRS+x} ] ; then
logwarn "are you sure, KERBEROS_CONFIG_DIRS is defined?"
EXIT
return 1
else
CONFIG=$(find ${KERBEROS_CONFIG_DIRS[*]} -mindepth 1 -name "$1.conf" -print -quit 2>/dev/null )
fi
if [ -e ${CONFIG} ]; then
logdebug -n "${CONFIG} existing: "
source "${CONFIG}"
logdebug "sourced"
else
logwarn "${CONFIG} not existing"
EXIT
return 2
fi
[ -z ${PKEY+x} ] && return 3
$PASS "${PKEY}" 1>/dev/null 2>&1 || return 3
local KERBEROS_PASSWORD=$($PASS "${PKEY}" | head -n1)
local KERBEROS_USER=$($PASS "${PKEY}" | grep login | sed -e 's/^login: //' )
#echo KERBEROS_PASSWORD: $KERBEROS_PASSWORD
loginfo "Get kerberos-ticket for: $KERBEROS_USER@$REALM"
if [ -z ${KERBEROS_USER+x} ];then
logwarn "no kerberos user found -> exit"
EXIT
return 4
else
$KINIT -R "${KERBEROS_USER}@${REALM}" <<!
${KERBEROS_PASSWORD}
!
if [ $? -gt 0 ] ; then
loginfo renew kerberos-ticket failed. try to get a new one
$KINIT "${KERBEROS_USER}@${REALM}" <<!
${KERBEROS_PASSWORD}
!
fi
fi
EXIT
}
unsetproxy () {
ENTRY
unset {HTTP,HTTPS,FTP}_PROXY
unset PROXY_{CREDS,USER,PASS,SERVER,PORT}
unset {http,https,ftp}_proxy
2020-02-27 15:34:47 +01:00
unset proxy_{creds,user,pass,server,port}
EXIT
}
git-mergedetachedheadtomaster () {
ENTRY
git checkout -b tmp
git branch -f master tmp
git checkout master
git branch -d tmp
git commit -m "Merged detached head into master" .
#git push origin master
EXIT
}
pathmunge () {
ENTRY
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
EXIT
}
mkcd () {
mkdir -p "$1"
cd "$1"
}
2020-01-14 11:37:02 +01:00
sshmyshellconfig() {
ENTRY
2020-04-05 01:42:17 +02:00
[ -z "${MYSHELLCONFIG_SUBPATH+x}" ] && MYSHELLCONFIG_SUBPATH=".local/myshellconfig"
[ -z "${MYSHELLCONFIG_BASE+x}" ] && MYSHELLCONFIG_BASE="${HOME}/${MYSHELLCONFIG_SUBPATH}"
MYSHELLCONFIG_BASE_PARENT="$(dirname $MYSHELLCONFIG_BASE)"
2020-01-14 14:44:36 +01:00
if [ $1 == "localhost" ]; then
2020-01-14 14:32:07 +01:00
CMD=""
else
local SSH="/usr/bin/ssh"
2020-01-15 02:10:02 +01:00
[ -e ${MYSHELLCONFIG_BASE}/bashrc_add ] && $SSH -T -o VisualHostKey=no $@ "mkdir -p ~/\$MYSHELLCONFIG_BASE_PARENT; cat > ~/bashrc_add" < "${MYSHELLCONFIG_BASE}/bashrc_add"
2020-01-14 14:32:07 +01:00
local CMD="$SSH -T $@"
fi
$CMD /bin/bash << EOF
[ -e /etc/bashrc ] && . /etc/bashrc
[ -e /etc/bash.bashrc ] && . /etc/bash.bashrc
loginfo "modify ~/.bashrc"
2020-01-14 21:37:53 +01:00
sed -i -e '/^\[ -f bashrc_add \] /d' ~/.bashrc
2020-01-14 14:01:05 +01:00
sed -i -e '/#MYSHELLCONFIG-start/,/#MYSHELLCONFIG-end/d' ~/.bashrc
echo
2020-01-17 12:27:00 +01:00
printf "%s\n" "#MYSHELLCONFIG-start" "[ -f \"\${HOME}/${MYSHELLCONFIG_SUBPATH}/bashrc_add\" ] && . \"\${HOME}/${MYSHELLCONFIG_SUBPATH}/bashrc_add\"" "#MYSHELLCONFIG-end"| tee -a ~/.bashrc
#printf "%s\n" "#MYSHELLCONFIG-start" "if [ -e \${HOME}/${MYSHELLCONFIG_SUBPATH}/bashrc_add ]; then" " . \${HOME}/${MYSHELLCONFIG_SUBPATH}/bashrc_add;" "else" " if [ -f ~/bashrc_add ] ;then" " . ~/bashrc_add;" " fi;" "fi" "#MYSHELLCONFIG-end" |tee -a ~/.bashrc
echo
loginfo cleanup from old config
2020-01-15 01:46:55 +01:00
rm -rf ~/server-config && echo rm -rf ~/server-config
EOF
EXIT
}
2020-01-14 14:53:38 +01:00
sshs() {
ENTRY
2020-10-01 08:03:54 +02:00
local LOGLEVEL="WARN"
# MKTMPCMD='mktemp $(echo ${XDG_RUNTIME_DIR}/bashrc.XXXXXXXX.conf)'
# VIMMKTMPCMD="mktemp ${XDG_RUNTIME_DIR}/vimrc.XXXXXXXX.conf"
2020-04-09 21:26:24 +02:00
local f
local TMPBASHCONFIG=$(mktemp -p ${XDG_RUNTIME_DIR} -t bashrc.XXXXXXXX --suffix=.conf)
2020-09-22 21:40:43 +02:00
local FILELIST=( "${MYSHELLCONFIG_BASE}/functions.sh" "${MYSHELLCONFIG_BASE}/logging" "${MYSHELLCONFIG_BASE}/myshell_load_fortmpconfig" $(getbashrcfile) ~/.aliases "${MYSHELLCONFIG_BASE}/aliases" "${MYSHELLCONFIG_BASE}/PS1" "${MYSHELLCONFIG_BASE}/bash_completion.d/*" )
2020-04-22 01:08:40 +02:00
local SSH_OPTS="-o VisualHostKey=no -o ControlMaster=auto -o ControlPersist=15s -o ControlPath=~/.ssh/ssh-%r@%h:%p"
# Read /etc/bashrc or /etc/bash.bashrc (depending on distribution) and /etc/profile.d/*.sh first
cat << EOF >> "${TMPBASHCONFIG}"
[ -e /etc/bashrc ] && BASHRC=/etc/bashrc
[ -e /etc/bash.bashrc ] && BASHRC=/etc/bash.bashrc
. \$BASHRC
export USERNAME="${USERNAME}"
export FULLNAME="${FULLNAME}"
export USEREMAIL="${USEREMAIL}"
2020-09-22 22:28:20 +02:00
export SCRIPT_LOG="\$(cat /proc/\$$/cmdline | xargs -0 echo|awk '{print \$3}' |sed 's/.conf$/.log/')"
2020-09-30 09:09:40 +02:00
export LOGLEVEL_DEFAULT="${LOGLEVEL_DEFAULT}"
export FILELOGLEVEL_DEFAULT="${FILELOGLEVEL_DEFAULT}"
for i in /etc/profile.d/*.sh; do
if [ -r "$i" ];then
if [ "$PS1" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
2020-04-09 21:26:24 +02:00
unset i
EOF
for f in ${FILELIST[*]}; do
if [ -e $f ]; then
#echo add $f to tmpconfig
cat "$f" >> "${TMPBASHCONFIG}";
fi
done
if [ $# -ge 1 ]; then
if [ -e "${TMPBASHCONFIG}" ] ; then
local RCMD="/bin/bash --noprofile --norc -c "
2020-04-22 01:08:40 +02:00
local REMOTETMPBASHCONFIG=$(ssh -T ${SSH_OPTS} $@ "mktemp -p \${XDG_RUNTIME_DIR} -t bashrc.XXXXXXXX --suffix=.conf"| tr -d '[:space:]' )
local REMOTETMPVIMCONFIG=$(ssh -T ${SSH_OPTS} $@ "mktemp -p \${XDG_RUNTIME_DIR} -t vimrc.XXXXXXXX --suffix=.conf"| tr -d '[:space:]')
# Add additional aliases to bashrc for remote-machine
cat << EOF >> "${TMPBASHCONFIG}"
alias vi='vim -u ${REMOTETMPVIMCONFIG}'
alias vim='vim -u ${REMOTETMPVIMCONFIG}'
alias vimdiff='vimdiff -u ${REMOTETMPVIMCONFIG}'
export LS_OPTIONS="${LS_OPTIONS}"
export VIMRC="${REMOTETMPVIMCONFIG}"
export BASHRC="${REMOTETMPBASHCONFIG}"
title "\$USER@\$HOSTNAME: \$PWD"
loginfo "This bash runs with temporary config from \$BASHRC"
EOF
2020-04-22 01:08:40 +02:00
ssh -T ${SSH_OPTS} $@ "cat > ${REMOTETMPBASHCONFIG}" < "${TMPBASHCONFIG}"
ssh -T ${SSH_OPTS} $@ "cat > ${REMOTETMPVIMCONFIG}" < "${MYSHELLCONFIG_BASE}/vimrc"
RCMD="
trap \"rm -f ${REMOTETMPBASHCONFIG} ${REMOTETMPVIMCONFIG}\" EXIT " ;
2020-04-22 01:08:40 +02:00
ssh -t ${SSH_OPTS} $@ "$RCMD; SSHS=true bash -c \"function bash () { /bin/bash --rcfile ${REMOTETMPBASHCONFIG} -i ; } ; export -f bash; exec bash --rcfile ${REMOTETMPBASHCONFIG}\""
rm "${TMPBASHCONFIG}"
else
loginfo "${TMPBASHCONFIG} does not exist. Use »ssh $@«" >&2
ssh -t "$@"
fi
else
logwarn "too few arguments for sshs" >&2
ssh
fi
EXIT
}
2020-02-17 20:03:22 +01:00
VIMRC="${MYSHELLCONFIG_BASE}/vimrc"
svi () {
ENTRY
if [ -f ${VIMRC} ]; then
sudo vim -u "${VIMRC}" $@;
else
sudo vim $@
fi
EXIT
}
vim-plugins-update () {
ENTRY
vim -c "PluginUpdate" -c ":qa!"
EXIT
}
vim-plugins-install () {
ENTRY
vim -c "PluginInstall" -c ":qa!"
EXIT
}
vim-repair-vundle () {
ENTRY
if [ -z ${MYSHELLCONFIG_BASE+x} ]; then
echo "MYSHELLCONFIG_BASE nicht gesetzt. Eventuell noch einmal ausloggen und wieder einloggen"
else
cd $MYSHELLCONFIG_BASE
cd vim/bundle
rm -rf Vundle.vim
2020-01-14 11:51:24 +01:00
git clone "${MYSHELLCONFIG_GIT_REMOTE_PUBLIC}Vim/gmarik/Vundle.vim.git"
cd ~-
fi
EXIT
}
getbashrcfile () {
ENTRY
if [ -z ${BASHRC+x} ] ; then
2020-04-23 22:23:40 +02:00
echo "bash uses default" >&2
else
cat /proc/$$/cmdline | xargs -0 echo|awk '{print $3}'
fi
EXIT
}
catbashrcfile () {
ENTRY
if [ -z ${BASHRC+x} ] ; then
2020-04-23 22:23:40 +02:00
echo "bash uses default" >&2
else
#cat $(cat /proc/$$/cmdline | xargs -0 echo|awk '{print $3}')
cat $(getbashrcfile)
fi
EXIT
}
getvimrcfile () {
ENTRY
if [ -z ${VIMRC+x} ] ; then
2020-04-23 22:23:40 +02:00
echo "vim uses default" >&2
else
echo $VIMRC
fi
EXIT
}
catvimrcfile () {
ENTRY
if [ -z ${VIMRC+x} ] ; then
2020-04-23 22:23:40 +02:00
echo "vim uses default" >&2
else
#cat $VIMRC
cat $(getvimrcfile)
fi
EXIT
}
# Functions to set the correct title of the terminal
function title()
{
ENTRY
# change the title of the current window or tab
echo -ne "\033]0;$*\007"
EXIT
}
function sshx()
{
/usr/bin/ssh "$@"
# revert the window title after the ssh command
title $USER@$HOST
}
function su()
{
/bin/su "$@"
# revert the window title after the su command
title $USER@$HOST
}
function usage()
{
cat << EOF
Keyboard-shortcuts:
# tmux:
C+Cursor tmux window change size
M+[hjkl] tmux change splitted windows
# vim:
C+[hjkl] vim change splitted windows
EOF
}
function pdsh-update-hetzner()
{
curl -s -H "Authorization: Bearer $(pass hetzner.com/api-token | head -n1)" \
https://api.hetzner.cloud/v1/servers \
| /usr/bin/jq '.servers[].public_net.ipv4.ip'|sed -e 's/\"//g' \
|while read i; do
dig -x $i | awk '$0 !~ /^;/ && $4 == "PTR" {print $5}'
done |sed -s -e 's/\.$//' > ~/.dsh/group/hetzner-servers
}
function tmuxx() {
ENTRY
case $# in
1)
SESS=($(find ${TMUX_SESSION_DIRS[*]} -mindepth 1 -name "$1.session" 2>/dev/null ))
;;
*)
logwarn no session specified return
;;
esac
TMUX_BIN='/usr/bin/tmux'
$TMUX_BIN -f ~/.tmux.conf new-session -d
[ -e ${SESS[0]} ] && $TMUX_BIN source-file ${SESS[0]}
$TMUX_BIN attach-session -d
EXIT
}
2020-02-05 12:03:32 +01:00
function checkbkp() {
ENTRY
2020-02-05 11:57:10 +01:00
if ping -c 3 backup.vpn >/dev/null 2>&1 ; then
local SSH="/usr/bin/ssh"
local CMD="$SSH -T backup.vpn"
$CMD /bin/bash << EOF
sudo find /srv/nfs/backup -mindepth 1 -maxdepth 1|grep -v -e "git$\|git-backup-repos"|while read i;do printf "%-30s%s\\n" "\$i" \$(ls \$i|tail -n1);done|sort -k 2.1 -r
EOF
else
echo "backup.vpn is not reachable -> exit"
return 1
fi
EXIT
2020-02-05 11:57:10 +01:00
}
2020-02-05 12:03:32 +01:00
function checkbkp-full() {
ENTRY
2020-02-05 11:57:10 +01:00
if ping -c 3 backup.vpn >/dev/null 2>&1 ; then
local SSH="/usr/bin/ssh"
local CMD="$SSH -T backup.vpn"
$CMD /bin/bash << EOF
2020-01-20 12:06:22 +01:00
sudo find /srv/nfs/backup -mindepth 1 -maxdepth 1|grep -v -e "git$\|git-backup-repos"|while read i;do printf "%-30s%s\\n" "\$i" \$(ls \$i|tail -n1);done|sort -k 2.1 -r
EOF
2020-01-16 17:36:36 +01:00
#which pdsh 1>/dev/null 2>&1 && pdsh -g hetzner-servers sudo systemctl status backup.service
which pdsh 1>/dev/null 2>&1 && pdsh -g vpn sudo systemctl status backup.service
else
logwarn "backup.vpn is not reachable -> exit"
return 1
fi
EXIT
}
2020-03-04 10:27:13 +01:00
turnoffbeep() {
ENTRY
2020-03-04 11:45:14 +01:00
changebeep none
EXIT
2020-03-04 11:45:14 +01:00
}
changebeep() {
ENTRY
local style
2020-03-04 11:45:14 +01:00
case $1 in
none)
2020-03-04 11:47:11 +01:00
style=none
2020-03-04 11:45:14 +01:00
;;
visible)
style=visible
;;
audible)
style=audible
;;
*)
logwarn "usage: changebeep [none|visible|audible]"
EXIT
2020-03-04 11:45:14 +01:00
return 1
;;
esac
local line='set bell-style'
local file=~/.inputrc
2020-03-04 11:45:14 +01:00
if [ -e "${file}" ] ; then
sed -i -e "/$line/d" "${file}"
fi
echo "${line} ${style}" >> "${file}"
return 0
EXIT
2020-03-04 10:27:13 +01:00
}
turnoffconfigsync() {
ENTRY
local line='MYSHELLCONFIG_GIT_SYNC='
local file=~/.bashrc
2020-03-04 11:45:14 +01:00
if [ -e "${file}" ] ; then
sed -i -e "/${line}/d" "${file}"
fi
2020-03-04 11:45:14 +01:00
sed -i -e "/#MYSHELLCONFIG-start/i${line}false" "${file}"
EXIT
}
turnonconfigsync() {
ENTRY
local line='MYSHELLCONFIG_GIT_SYNC='
local file=~/.bashrc
2020-03-04 11:45:14 +01:00
if [ -e "${file}" ] ; then
sed -i -e "/${line}/d" "${file}"
fi
2020-03-04 11:45:14 +01:00
sed -i "/#MYSHELLCONFIG-start/i${line}true" "${file}"
EXIT
}
2020-03-05 11:52:01 +01:00
function gnome-shell-extensions-enable-defaults() {
ENTRY
2020-04-09 21:26:24 +02:00
local i
2020-03-05 11:52:01 +01:00
if [ -f ~/.config/gnome-shell-extensions-default.list ]; then
for i in $(cat ~/.config/gnome-shell-extensions-default.list); do
#gnome-shell-extension-tool -e $i;
gnome-extensions enable $i;
done;
fi
EXIT
2020-03-05 11:52:01 +01:00
}
gnome-shell-extensions-make-actual-permanent-systemwide() {
ENTRY
# https://people.gnome.org/~pmkovar/system-admin-guide/extensions-enable.html
# https://askubuntu.com/questions/359958/extensions-are-turned-off-after-reboot
local file="/etc/dconf/profile/user"
2020-03-05 11:52:01 +01:00
sudo mkdir -p "/etc/dconf/profile/"
local line='user-db:user'
if [ -e "${file}" ] ; then
logtrace "$command"
sudo sh -c "$command"
fi
local line='system-db:local'
if [ -e "${file}" ] ; then
command="grep -xqF -- ${line} ${file} || echo $line >> $file"
sudo sh -c "$command"
fi
local line='enabled-extensions='
local file='/etc/dconf/db/local.d/00-extensions'
2020-03-05 11:52:01 +01:00
sudo mkdir -p '/etc/dconf/db/local.d'
if [ -e "${file}" ] ; then
sudo sed -i -e "/${line}/d" "${file}"
2020-03-05 12:53:58 +01:00
#sudo sed -i -e "/\[org\/gnome\/shell\]/d" "${file}"
fi
local EXTENSIONS=$(gsettings get org.gnome.shell enabled-extensions)
2020-03-05 12:53:58 +01:00
line="[org/gnome/shell]"
command="grep -xqF -- ${line} ${file} || echo $line >> $file"
sudo sh -c "$command"
local line='enabled-extensions='
loginfo "Update or add extensions"
2020-03-05 12:53:58 +01:00
#echo "${line}${EXTENSIONS}" | sudo tee -a "${file}"
sudo sed -i "/\[org\/gnome\/shell\]/a${line}${EXTENSIONS}" "${file}"
sudo dconf update
EXIT
}
2020-04-22 01:08:40 +02:00
2020-04-27 15:45:19 +02:00
reachable-default () {
2020-04-22 01:08:40 +02:00
local SERVER=$1
local PORT=${2:-22}
2020-09-11 09:47:52 +02:00
local res=3
2020-10-19 14:35:07 +02:00
if which nc >/dev/null; then
if nc -w 2 -z $SERVER $PORT 2>/dev/null; then
res=0
fi
else
res=2
2020-04-22 01:08:40 +02:00
fi
2020-04-26 09:10:41 +02:00
return $res
}
2020-04-27 15:45:19 +02:00
reachable () {
2020-09-22 01:28:29 +02:00
ENTRY
2020-09-11 09:47:52 +02:00
# returncodes:
# 1: servername not resolveable
2020-10-19 14:35:07 +02:00
# 2: command nc not found
2020-09-11 09:47:52 +02:00
# 3: server:port not reachable
# 999999: something went wrong
# 0: server was resolve- and reachable
2020-09-11 16:26:47 +02:00
GETENTHOSTS=ahosts
2020-04-26 09:10:41 +02:00
local SERVER=$1
2020-09-11 09:10:18 +02:00
# dig does not consult /etc/hosts, so use getent hosts instead
#local IP=$(dig +nocmd $SERVER a +noall +answer|tail -n 1 |awk '{print $5}')
# getent ahostsv4 returns only ipv4 addresses
loginfo -n "Try to resolve $SERVER: "
2020-09-11 16:26:47 +02:00
local IP=$(getent $GETENTHOSTS $SERVER|awk '$0 ~ /STREAM/ {print $1}'|uniq|head -n1)
if [ -z ${IP-x} ]; then
logwarn "not resolvable -> exit"
return 1
else
2020-10-06 14:32:25 +02:00
loginfo $IP
fi
2020-04-26 09:10:41 +02:00
local PORT=${2:-22}
2020-04-27 15:45:19 +02:00
local SEC=${3:-1}
2020-09-11 09:52:43 +02:00
local res=999
2020-04-26 09:10:41 +02:00
local i
loginfo -n "Try to connect to ${SERVER} (${IP}):${PORT} " >&2
2020-04-27 15:45:19 +02:00
for i in $(seq 1 $SEC); do
2020-10-06 14:32:25 +02:00
logdebug -n "." >&2
2020-05-01 11:28:28 +02:00
if reachable-default ${IP} ${PORT} 2>/dev/null; then
2020-09-11 09:52:43 +02:00
res=0
2020-04-26 09:10:41 +02:00
break
2020-09-11 09:47:52 +02:00
else
res=$?
2020-04-26 09:10:41 +02:00
fi
2020-04-27 15:45:19 +02:00
[ ${SEC} -gt 1 -a $i -lt ${SEC} ] && sleep 1
2020-04-26 09:10:41 +02:00
done
2020-10-06 14:32:25 +02:00
[ ${res} -gt 0 ] && loginfo " not reachable" >&2 || loginfo " success" >&2;
2020-04-26 09:10:41 +02:00
2020-09-22 01:28:29 +02:00
EXIT
2020-04-26 09:10:41 +02:00
return $res
2020-04-22 01:08:40 +02:00
}
2020-08-13 22:29:39 +02:00
utoken () {
2020-09-20 11:23:40 +02:00
2020-09-22 01:28:29 +02:00
ENTRY
2020-09-20 11:23:40 +02:00
ssh_identity=$1
2020-09-21 12:39:58 +02:00
[ -z "${PKCS11_MODULE+x}" ] && { PKCS11_MODULE=/usr/lib64/p11-kit-proxy.so; export PKCS11_MODULE; }
2020-09-20 11:23:40 +02:00
if [ -n "${ssh_identity+x}" ]; then
agentfile="${HOME}/.ssh/agents/agent-${ssh_identity}-$(hostname)"
if [ -e "$agentfile" ]; then
local SSH_AUTH_SOCK
local SSH_AGENT_PID
2020-09-21 12:39:58 +02:00
/bin/sh -c ". $agentfile >/dev/null 2>/dev/null; ssh-add -l; ssh-add -e $PKCS11_MODULE; ssh-add -l"
2020-09-20 11:23:40 +02:00
fi
fi
2020-09-22 01:28:29 +02:00
EXIT
2020-08-13 22:29:39 +02:00
}
2020-09-20 11:23:40 +02:00
2020-09-22 09:47:15 +02:00
token(){
2020-09-22 09:47:15 +02:00
[ -z "${PKCS11_MODULE+x}" ] && { PKCS11_MODULE=/usr/lib64/p11-kit-proxy.so; export PKCS11_MODULE; }
2020-09-26 07:29:38 +02:00
# 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
# startagent -t $@
loadagent $@
loginfo "$(ssh-add -s $PKCS11_MODULE)"
loginfo "$(ssh-add -l)"
2020-09-20 11:23:40 +02:00
2020-09-26 07:29:38 +02:00
}
2020-09-26 07:29:38 +02:00
2020-09-16 08:55:31 +02:00
token-extract-pubkey() {
2020-09-16 11:25:27 +02:00
if pkcs11-tool --module $PKCS11_MODULE --list-token-slots >&2 ;then
ssh-keygen -i -m pkcs8 -f <(pkcs11-tool --module $PKCS11_MODULE -r --type pubkey $1 $2 |openssl rsa -pubin -inform DER )
if [ $? -gt 0 ] ; then
token-list-objects
fi
else
echo "Please insert token. Exit"
return 1
fi
}
token-list-objects() {
2020-09-16 08:55:31 +02:00
case $1 in
--login|-l)
2020-09-16 11:25:27 +02:00
pkcs11-tool --module $PKCS11_MODULE --login --list-objects
2020-09-16 08:55:31 +02:00
;;
*)
2020-09-16 11:25:27 +02:00
pkcs11-tool --module $PKCS11_MODULE --list-objects
2020-09-16 08:55:31 +02:00
;;
esac
2020-09-16 11:25:27 +02:00
2020-09-16 08:55:31 +02:00
}
2020-09-20 11:23:40 +02:00
2020-09-21 12:39:58 +02:00
loadagent() {
2020-09-22 01:28:29 +02:00
ENTRY
2020-09-21 12:39:58 +02:00
local af
2020-09-24 13:05:26 +02:00
af=$(startagent --create-only $1 )
2020-09-22 09:08:16 +02:00
loginfo "Load agent from $af"
2020-09-26 07:29:38 +02:00
unset SSH_AUTH_SOCKET SSH_AGENT_PID
eval $(<$af)
# . $af
2020-09-26 07:29:38 +02:00
logdebug "SSH_AUTH_SOCK: ${SSH_AUTH_SOCK-not set}"
logdebug "SSH_AGENT_PID: ${SSH_AGENT_PID-not set}"
loginfo "currently loaded keys in agent:
$(ssh-add -l)"
EXIT
2020-09-21 12:39:58 +02:00
}
2020-09-21 13:00:56 +02:00
setloglevel () {
2020-09-22 01:28:29 +02:00
ENTRY
2020-09-21 21:52:34 +02:00
local loglevels
2020-09-22 09:12:19 +02:00
local oldloglevel=${LOGLEVEL-$LOGLEVEL_DEFAULT}
2020-09-21 21:52:34 +02:00
declare -a loglevels
loglevels=("ERROR" "WARN" "INFO" "DEBUG" "TRACE")
2020-10-08 06:28:19 +02:00
if [[ ${loglevels[*]} =~ "${1^^}" ]]; then
export LOGLEVEL=${1^^}
2020-09-21 21:52:34 +02:00
else
logerror "LOGLEVEL must be one of ERROR, WARN, INFO, DEBUG or TRACE"
fi
2020-09-22 09:12:19 +02:00
logerror "change LOGLEVEL from $oldloglevel -> $LOGLEVEL"
2020-09-22 01:28:29 +02:00
EXIT
2020-09-21 21:52:34 +02:00
}
2020-09-21 22:02:58 +02:00
setfileloglevel () {
2020-09-22 01:28:29 +02:00
ENTRY
2020-09-21 22:02:58 +02:00
local loglevels
2020-09-22 09:12:19 +02:00
local oldloglevel=${FILELOGLEVEL-${FILELOGLEVEL_DEFAULT}}
2020-09-21 22:02:58 +02:00
declare -a loglevels
loglevels=("ERROR" "WARN" "INFO" "DEBUG" "TRACE")
if [[ ${loglevels[*]} =~ "$1" ]]; then
export FILELOGLEVEL=$1
else
logerror "FILELOGLEVEL must be one of ERROR, WARN, INFO, DEBUG or TRACE"
fi
2020-09-22 09:12:19 +02:00
logerror "change FILELOGLEVEL from $oldloglevel -> $FILELOGLEVEL"
2020-09-22 01:28:29 +02:00
EXIT
2020-09-21 22:02:58 +02:00
}
2020-10-08 06:28:19 +02:00
getloglevels () {
ENTRY
2020-10-08 06:32:44 +02:00
cat << EOF |tee -a $SCRIPT_LOG
2020-10-08 06:28:19 +02:00
LOGLEVEL: ${LOGLEVEL-${LOGLEVEL_DEFAULT}}
FILELOGLEVEL: ${FILELOGLEVEL-${FILELOGLEVEL_DEFAULT}}
change LOGLEVEL: \$ setloglevel [ERROR|WARN|INFO|DEBUG|TRACE]
change FILELOGLEVEL: \$ setfileloglevel [ERROR|WARN|INFO|DEBUG|TRACE]
EOF
}
2020-09-26 07:29:38 +02:00
#EOF