Add session decoding to ssh command lines

This commit is contained in:
Davide Depau 2019-09-07 00:52:32 +02:00
parent 6aeaf0222c
commit 2cb447ddbd
Signed by: depau
GPG Key ID: C7D999B6A55EFE86
2 changed files with 126 additions and 7 deletions

99
crt2foss.py Executable file
View File

@ -0,0 +1,99 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import errno
import re
from typing import List
item_regex = re.compile(r'.*(?P<type>\w):"(?P<key>.+?)"=(?P<value>.*)$', re.DOTALL)
def parse_ini(path: str) -> dict:
values = []
with open(path) as f:
for line in f.readlines():
line = line.strip()
if line.startswith("\ufeff"):
line = line[1:]
if len(line) > 2 and (line[1] == ":" or line[2] == ":"):
values.append(line)
elif line == "":
continue
else:
values[-1] += "\n" + line
matches = []
for i in values:
try:
match = item_regex.match(i)
matches.append(match.groupdict())
except Exception:
pass
result = {}
for match in matches:
t = match["type"]
k = match["key"]
v = match["value"]
if t == "S":
result[k] = v
if t == "D":
result[k] = int(v, 16)
# We don't care about the other types
return result
def gen_cmdlines_ssh(ini: dict, cfgdir: str) -> List[str]:
hostname = ini["Hostname"]
port = ini.get("[SSH2] Port", 22)
username = ini["Username"]
keyfile = ini.get("Identity Filename V2", None)
if keyfile:
keyfile = os.path.normpath(
keyfile \
.replace('\\', '/') \
.replace('${VDS_CONFIG_PATH}', cfgdir)
) \
.replace('"', '\\"')
cmdline = []
if keyfile:
cmdline += ["-i", f'"{keyfile}"']
if port != 22:
cmdline += ["-p", str(port)]
cmdline.append(f"{username}@{hostname}")
ssh = ["ssh"] + cmdline
scp = ["scp"] + cmdline
return [' '.join(ssh), ' '.join(scp)]
def gen_cmdlines(ini: dict, cfgdir: str) -> List[str]:
if ini["Protocol Name"].startswith("SSH"):
return gen_cmdlines_ssh(ini, cfgdir)
return []
if __name__ == "__main__":
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} path/to/Config path/to/session.ini", file=sys.stderr)
sys.exit(1)
cfg = sys.argv[1]
ini = sys.argv[2]
if not os.path.exists(ini):
raise OSError(f"Session file does not exist: '{ini}'", errno=errno.ENOENT)
if not os.path.isfile(ini):
raise OSError(f"Not a file: '{ini}'", errno=errno.ENFILE)
inidict = parse_ini(ini)
cmdlines = gen_cmdlines(inidict, cfg)
print('\n'.join(cmdlines))

34
shcrt
View File

@ -2,8 +2,18 @@
export supermode="dialog"
export SHELL_LIBRARY_PATH="$SHELL_LIBRARY_PATH:./easybashgui/lib"
export PATH="$PATH:./easybashgui/src"
# 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"
source easybashgui
function is_wsl {
@ -86,8 +96,6 @@ function scrt_ls {
function scrt_menu {
listing="$(scrt_ls)"
menu_listing="$(echo "$listing" | cut -d "\\" -f 1)"
echo "LISTING"
echo "$listing"
IFSBAK="$IFS"
IFS=$'\n'
@ -102,11 +110,9 @@ function scrt_menu {
scrt_cd "$choice"
scrt_menu
elif [ -f "$choice_path" ]; then
echo "$choice_path"
return
scrt_session "$choice_path"
elif [[ "$menu_choice" == "" ]]; then
scrt_cd ..
echo "$current_path"
if [[ "$current_path" == "" ]] || [[ "$current_path" == ".." ]]; then
return
else
@ -117,6 +123,20 @@ function scrt_menu {
fi
}
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"
}
function main {
scrt_menu
}