shcrt/shcrt

254 lines
6.1 KiB
Plaintext
Raw Normal View History

2019-09-06 16:58:07 +00:00
#!/bin/bash
2019-09-06 19:29:23 +00:00
export supermode="dialog"
# Determine script location
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
# if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
export SHELL_LIBRARY_PATH="$SHELL_LIBRARY_PATH:$DIR/easybashgui/lib"
export PATH="$PATH:$DIR/easybashgui/src"
2019-09-06 16:58:07 +00:00
source easybashgui
function is_wsl {
grep -q Microsoft /proc/version
return $?
}
2019-09-06 17:51:56 +00:00
function wsl_win_var {
/mnt/c/Windows/System32/cmd.exe /C "echo $1"
}
2019-09-06 16:58:07 +00:00
function win_path_to_wsl {
2019-09-06 17:51:56 +00:00
if [[ "$1" != "" ]]; then
echo "$1" | sed -e 's|\\|/|g' -e 's|\(.*\):|/mnt/\L\1|'
else
cat - | sed -e 's|\\|/|g' -e 's|\(.*\):|/mnt/\L\1|'
fi
2019-09-06 16:58:07 +00:00
}
function copy_to_clipboard {
if is_wsl; then
cat - | /mnt/c/Windows/System32/clip.exe
elif [[ "$WAYLAND_DISPLAY" != "" ]] && which wl-copy > /dev/null; then
cat - | wl-copy
elif which xclip > /dev/null; then
cat - | xclip -selection clipboard
else
return 1
fi
}
2019-09-06 16:58:07 +00:00
function scrt_session_path {
if is_wsl; then
2019-09-06 17:51:56 +00:00
appdata="$(wsl_win_var '%APPDATA%' | win_path_to_wsl)"
2019-09-06 16:58:07 +00:00
echo "$appdata/VanDyke/Config/Sessions"
else
echo "$HOME/.vandyke/SecureCRT/Config/Sessions"
fi
}
2019-09-06 19:29:23 +00:00
if is_wsl; then
dir_char="+"
file_char=" "
2019-09-07 00:04:45 +00:00
search_entry="> Search"
up_entry=".. Back"
exit_search_entry="X Exit search"
query_question="Enter search query: "
now_at_entry="Now at: "
2019-09-06 19:29:23 +00:00
else
dir_char="📁"
file_char="🔗"
2019-09-07 00:04:45 +00:00
search_entry="🔍 Search"
up_entry="↩️ .. Back"
exit_search_entry="❌ Exit search"
query_question="🔍 Enter search query: "
now_at_entry="📍 Now at: "
2019-09-06 19:29:23 +00:00
fi
2019-09-06 16:58:07 +00:00
session_path="$(scrt_session_path)"
2019-09-07 00:04:45 +00:00
current_path="."
2019-09-06 16:58:07 +00:00
function scrt_cd {
2019-09-06 19:29:23 +00:00
newdir="$session_path/$current_path/$1"
if [ ! -d "$newdir" ]; then
2019-09-06 16:58:07 +00:00
return 1
fi
2019-09-06 19:29:23 +00:00
current_path="$(realpath --no-symlinks --relative-to "$session_path" "$newdir")"
}
function scrt_pwd {
echo "$session_path/$current_path"
}
function find_dirs_or_symlinks {
while read -r name; do
[ -d "$name" ] && echo "$name";
done < <(find $@)
}
function find_files_or_symlinks {
while read -r name; do
[ -f "$name" ] && echo "$name";
done < <(find $@)
2019-09-06 16:58:07 +00:00
}
function scrt_ls {
2019-09-06 19:29:23 +00:00
cwd="$(pwd)"
cd "$(scrt_pwd)"
find_dirs_or_symlinks -maxdepth 1 -mindepth 1 | sort | sed "s|./||" | while read dir; do
echo "$dir_char $dir\\$dir"
done
find_files_or_symlinks -maxdepth 1 -mindepth 1 | grep '.ini$' | grep -v 'Default.ini' | grep -v '__FolderData__.ini' | sort | sed 's|./||' | while read file; do
proto="$(cat "$file" | grep 'S:"Protocol Name"' | cut -d '=' -f 2 | sed 's|\r||g')"
echo "$file_char $(echo "$file" | sed 's/.ini$//') ($proto)\\$file"
done
cd "$cwd"
2019-09-06 16:58:07 +00:00
}
2019-09-06 19:29:23 +00:00
2019-09-07 00:04:45 +00:00
function scrt_find {
query="$1"
(
find "$session_path/$current_path" -type d -iname '*'"$query"'*'
find "$session_path/$current_path" -type f -iname '*'"$query"'*.ini$' | grep -v "__FolderData__.ini" | grep -v "Default.ini"
)| while read result; do
if basename "$result" | grep -qi "$query"; then
realpath --relative-to "$session_path/$current_path" "$result"
fi
done
}
function scrt_search {
input 1 "$query_question"
query="$(cat "${dir_tmp}/${file_tmp}")"
query="${query#"$query_question"}"
if [[ "$query" == "" ]]; then
return 1
fi
results="$(scrt_find "$query")"
if [[ "$results" == "" ]]; then
alert_message "No results"
return 1;
fi
IFSBAK="$IFS"
IFS=$'\n'
menu "$exit_search_entry" $results
IFS="$IFSBAK"
menu_choice="$(0< "${dir_tmp}/${file_tmp}" )"
if [[ "$menu_choice" == "$exit_search_entry" ]]; then
return 1
else
choice_path="$(scrt_pwd)/$menu_choice"
if [ -d "$choice_path" ]; then
scrt_cd "$menu_choice"
return 1
else
scrt_session "$menu_choice"
fi
fi
}
2019-09-06 19:29:23 +00:00
function scrt_menu {
listing="$(scrt_ls)"
menu_listing="$(echo "$listing" | cut -d "\\" -f 1)"
2019-09-07 00:04:45 +00:00
now_at="$(echo "$current_path" | sed -e 's|^[.]|/|' -e 's|//|/|')"
2019-09-06 19:29:23 +00:00
IFSBAK="$IFS"
IFS=$'\n'
2019-09-07 00:04:45 +00:00
if [[ "$current_path" != "" ]] && [[ "$current_path" != "." ]]; then
menu "$now_at_entry $now_at" "$search_entry" "$up_entry" $menu_listing
else
menu "$now_at_entry $now_at" "$search_entry" $menu_listing
fi
2019-09-06 19:29:23 +00:00
IFS="$IFSBAK"
menu_choice="$(0< "${dir_tmp}/${file_tmp}" )"
choice="$(echo "$listing" | grep "$menu_choice" | cut -d "\\" -f 2)"
choice_path="$(scrt_pwd)/$choice"
2019-09-07 00:04:45 +00:00
if [[ "$menu_choice" == "" ]] || [[ "$menu_choice" == "$up_entry" ]]; then
2019-09-06 19:29:23 +00:00
scrt_cd ..
2019-09-06 19:59:44 +00:00
if [[ "$current_path" == "" ]] || [[ "$current_path" == ".." ]]; then
2019-09-06 19:29:23 +00:00
return
2019-09-06 19:59:44 +00:00
else
scrt_menu
2019-09-06 19:29:23 +00:00
fi
2019-09-07 00:04:45 +00:00
elif [[ "$menu_choice" == "$search_entry" ]]; then
if scrt_search; then
return
else
scrt_menu
fi
elif [[ "$menu_choice" == "$now_at_entry"* ]]; then
scrt_menu
elif [ -d "$choice_path" ]; then
scrt_cd "$choice"
scrt_menu
elif [ -f "$choice_path" ]; then
scrt_session "$choice_path"
2019-09-06 19:29:23 +00:00
else
alert_message "Could not find selected item \"$menu_choice\""
fi
}
function scrt_has_password {
cat "$1" | grep -q '"Password"'
return $?
}
function scrt_get_cleartext_pwd {
session="$1"
encrypted="$(cat "$session" | grep '"Password"' | cut -d '=' -f 2 | cut -c 1 --complement)"
"$DIR/SecureCRTCipher.py" dec "$encrypted"
}
function scrt_session {
if [[ "$mode" == "dialog" ]] || [[ "$supermode" == "dialog" ]]; then
clear
fi
session="$1"
echo
echo "Session file:"
echo "$session"
echo
echo "Commands:"
"$DIR/crt2foss.py" "$session_path/../" "$session"
if scrt_has_password "$session"; then
echo
if scrt_get_cleartext_pwd "$session" | copy_to_clipboard; then
echo "Password copied to clipboard."
else
echo "Unable to copy password to clipboard."
read -p "Do you want me to print it to the console? [yN] " yn
case $yn in
[Yy]*) scrt_get_cleartext_pwd "$session";;
esac
fi
fi
}
2019-09-06 19:29:23 +00:00
function main {
scrt_menu
}
# Run if not sourced
BASH_SOURCE=".$0"
test ".$0" != ".$BASH_SOURCE" || main $@