diff --git a/completion b/completion new file mode 100644 index 0000000..f6824f6 --- /dev/null +++ b/completion @@ -0,0 +1,17 @@ +function _ssh_connector_completion() +{ + latest="${COMP_WORDS[$COMP_CWORD]}" + words=$(grep -v -E "\#" $SSH_HELPER_HOST_LIST | awk '{print $1}') + COMPREPLY=($(compgen -W "$words" -- $latest)) + return 0 +} + +function _scp_path_completition() +{ + #COMPREPLY=(${COMP_WORDS}) + COMPREPLY=($(scp-completion.py ${COMP_WORDS[*]} $COMP_CWORD)) + return 0 +} + +complete -F _ssh_connector_completion ssh-helper +complete -F _scp_path_completition scp-helper diff --git a/helperlib.py b/helperlib.py index 61c1b2f..69043b0 100644 --- a/helperlib.py +++ b/helperlib.py @@ -66,7 +66,7 @@ def add_sudo(s): return res def make_ssh_cmd(host, port, user, cmd): - res = f"ssh {user}@{host} -p {port} '{cmd}'" + res = f"/usr/bin/ssh {user}@{host} -p {port} '{cmd}'" return res def get_connection_config(con_name): diff --git a/install_completition.sh b/install_completition.sh new file mode 100755 index 0000000..633f06f --- /dev/null +++ b/install_completition.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +ln -s $(pwd)/completion /etc/bash_completion.d/ssh-helper diff --git a/scp-completion.py b/scp-completion.py new file mode 100755 index 0000000..4787128 --- /dev/null +++ b/scp-completion.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 + +import argparse +import os +import sys +import subprocess +from helperlib import get_config, parse_arg, add_sudo, make_ssh_cmd, get_path_config + +DEBUG = True +SUDO_REMOTE = True + +def get_conf_names(): + res = [] + for i in get_config().keys(): + res.append(f'{i}:') + return res + +def ls_cmd(path): + cmd = f'ls {path}' + return cmd + +def make_full_cmd(config): + cmd = ls_cmd(config['work_path']) + + if SUDO_REMOTE and config['connection_name'] != 'local': + cmd = add_sudo(cmd) + + if config['connection_name'] != 'local': + cmd = make_ssh_cmd( + config['host'], + config['port'], + config['user'], + cmd + ) + + return cmd + +def get_current_arg(args): + # Возвращает заполняемый аргумент + res = None + if len(args.command) > args.pos_num: + res = args.command[args.pos_num] + + return res + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + + ) + parser.add_argument('command', nargs='*') + parser.add_argument('pos_num', type=int) + args = parser.parse_args() + if DEBUG: + print(f'{args}', file=sys.stderr) + + cur_arg = get_current_arg(args) + if DEBUG: + print(f'{cur_arg}', file=sys.stderr) + + if cur_arg is not None and ':' in cur_arg: + # В нем уже прописали имя подключения + cur_set = get_path_config(cur_arg) + + cmd = make_full_cmd(cur_set) + if DEBUG: + print(f'{cmd}', file=sys.stderr) + obj_list = subprocess.run(cmd, capture_output=True, shell=True, text=True).stdout + if DEBUG: + print(f'{obj_list}', file=sys.stderr) + print(cur_set, file=sys.stderr) + + comp_list = [] + for i in obj_list.split('\n'): + i_res = '' + if cur_set['connection_name'] != 'local': + i_res = cur_set['connection_name'] + ':' + + i_res += cur_set["work_path"] + + if i_res[-1] != '/': + i_res += '/' + + i_res += i + comp_list.append(i_res) + + if DEBUG: + print(f'{comp_list}', file=sys.stderr) + + print(' '.join(comp_list)) + else: + # Заполняем имя подключения + print('/ '.join(get_conf_names())) + + + + diff --git a/ssh_connector_comletition b/ssh_connector_comletition deleted file mode 100644 index a44923c..0000000 --- a/ssh_connector_comletition +++ /dev/null @@ -1,17 +0,0 @@ -function _ssh_connector_completion() -{ - latest="${COMP_WORDS[$COMP_CWORD]}" - words=$(grep -v -E "\#" $SSH_CONNECTOR_HOST_LIST | awk '{print $1}') - COMPREPLY=($(compgen -W "$words" -- $latest)) - return 0 -} - -function _scp_path_completition() -{ - latest="${COMP_WORDS[$COMP_CWORD]}" - COMPREPLY=$(grep -v -E "\#" $SSH_CONNECTOR_HOST_LIST | awk '{print $1}') - return 0 -} - -complete -F _ssh_connector_completion ssh_helper -complete -F _ssh_connector_completion scp_helper