Created
September 11, 2012 16:23
-
-
Save CMAD/3699643 to your computer and use it in GitHub Desktop.
evil ssh wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this function is a ticket to hell, each time you connect | |
# somewhere, it records a log of when you connect and disconnect | |
ssh() { | |
# create a file, for ssh to let us know the hostname | |
ssh_hostname_tmp=$(mktemp) | |
ssh_username_tmp=$(mktemp) | |
ssh_port_tmp=$(mktemp) | |
# why do I need a fifo, see below, hint: controll flow from hell | |
ssh_adquire_hostname=$(mktemp -u) | |
mkfifo "$ssh_adquire_hostname" | |
# the pipe read will block till ssh writes the hostname, it get's on | |
# the background, and voila, when ssh connects, the backlog is updated | |
( | |
xargs -I{} -n1 backlog ssh "{} -> connect" < "$ssh_adquire_hostname" && \ | |
rm "$ssh_adquire_hostname" & | |
) 2>&1 >/dev/null | |
# ssh will get all original parameters, plus the instruction of writing | |
# the hostname to a file, by doing this, you can avoid scavenging | |
# parameters, the cost is that, doing scavenging will by far cleaner | |
env ssh "$@" \ | |
-o LocalCommand="echo %n | tee $ssh_hostname_tmp > $ssh_adquire_hostname; | |
echo %r > $ssh_username_tmp; echo %p > $ssh_port_tmp" \ | |
-o PermitLocalCommand=yes | |
ssh_hostname=$(cat $ssh_hostname_tmp) | |
# the temp file is discarded as soon as it's read | |
rm "$ssh_hostname_tmp" | |
ssh_username=$(cat $ssh_username_tmp) | |
rm "$ssh_username_tmp" | |
ssh_port=$(cat $ssh_port_tmp) | |
rm "$ssh_port_tmp" | |
# the ssh command was blocking, we reach here just after the disconnect | |
backlog ssh "$ssh_hostname -> disconnect" 2>&1 >/dev/null | |
# the rest of the code is even worse, so just run away now | |
return | |
# now we will be commiting files to the backlog repo | |
backlog_dir=$(backlog -e) | |
history_dst="$backlog_dir/bash_histories/$ssh_hostname/bash_history_$(_date)" | |
mkdir -p "$backlog_dir/bash_histories/$ssh_hostname/" 2>&1 >/dev/null | |
# we get a bash_history copy inside the repo | |
scp -P "$ssh_port" "$ssh_username@$ssh_hostname":./.bash_history \ | |
"$history_dst" 2>&1 >/dev/null | |
# if we have a copy and git, it gets commited | |
if [ -e "$history_dst" ] && [ -e "$backlog_dir/.git" ] && \ | |
which git 2>&1 >/dev/null; then | |
cd "$backlog_dir" | |
git add -A "./bash_histories/$1" | |
backlog ssh "$ssh_hostname -> bash_history copy commited to $history_dst" \ | |
2>&1 >/dev/null | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment