Skip to content

scp remote file completion extended with port parsing #738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions completions/ssh
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ _sftp()

local ipvx


case $prev in
-*[BDlPRs])
return
Expand Down Expand Up @@ -429,13 +430,25 @@ _sftp()
# shellcheck disable=SC2089
_comp_cmd_scp__path_esc='[][(){}<>"'"'"',:;^&!$=?`\\|[:space:]]'

# Complete remote files with ssh. If the first arg is -d, complete on dirs
# only. Returns paths escaped with three backslashes.
# Complete remote files with ssh. If the arg is -d, complete on dirs
# only. Returns paths escaped with three backslashes.
# Pass argument as "-p portnumber" for non-default ssh connection port.
# shellcheck disable=SC2120
_comp_xfunc_ssh_scp_remote_files()
{
local IFS=$'\n'


local sshoption
local dirsonly=false
local i
for i in "$@"; do
if [[ ${i-} == -d ]]; then
dirsonly=true
elif [[ ${i:0:2} == -p ]]; then
sshoption=$1
fi
done

# remove backslash escape from the first colon
cur=${cur/\\:/:}

Expand All @@ -448,21 +461,21 @@ _comp_xfunc_ssh_scp_remote_files()

# default to home dir of specified user on remote host
if [[ ! $path ]]; then
path=$(ssh -o 'Batchmode yes' "$userhost" pwd 2>/dev/null)
path=$(ssh $sshoption -o 'Batchmode yes' "$userhost" pwd 2>/dev/null)
fi

local files
if [[ ${1-} == -d ]]; then
if $dirsonly; then
# escape problematic characters; remove non-dirs
# shellcheck disable=SC2090
files=$(ssh -o 'Batchmode yes' "$userhost" \
files=$(ssh $sshoption -o 'Batchmode yes' "$userhost" \
command ls -aF1dL "$path*" 2>/dev/null |
command sed -e 's/'"$_comp_cmd_scp__path_esc"'/\\\\\\&/g' -e '/[^\/]$/d')
else
# escape problematic characters; remove executables, aliases, pipes
# and sockets; add space at end of file names
# shellcheck disable=SC2090
files=$(ssh -o 'Batchmode yes' "$userhost" \
files=$(ssh $sshoption -o 'Batchmode yes' "$userhost" \
command ls -aF1dL "$path*" 2>/dev/null |
command sed -e 's/'"$_comp_cmd_scp__path_esc"'/\\\\\\&/g' -e 's/[*@|=]$//g' \
-e 's/[^\/]$/& /g')
Expand Down Expand Up @@ -515,6 +528,31 @@ _scp()
}

local ipvx

# Parse Port number from command
local sshport=""
local sshoption=""
local i
for ((i = 0; i < ${#words[@]}; i++)); do
if [[ ${words[i]} =~ -[a-zA-Z0-9]*P[0-9]*$ ]]; then
if [[ ${words[i]: -1} == P ]]; then
((i+=1))
[[ ${words[i]} =~ ^[0-9]+$ ]] && sshport=${words[i]}
else
sshport=$(echo ${words[i]} | command sed -n 's/-.*P\([0-9]\{1,\}\)/\1/p')
fi
elif [[ ${words[i]} =~ -[a-zA-Z0-9]*o$ ]]; then
if [[ ${words[i+1],,} == port ]]; then
((i+=3))
[[ ${words[i]} =~ ^[0-9]+$ ]] && sshport=${words[i]}
fi
elif [[ ${words[i]} =~ -[a-zA-Z0-9]*o[pP][oO][rR][tT]$ ]]; then
((i+=2))
[[ ${words[i]} =~ ^[0-9]+$ ]] && sshport=${words[i]}
fi
done

[[ ${sshport// } ]] && sshoption="-p $sshport"

case $prev in
-*c)
Expand Down Expand Up @@ -561,7 +599,7 @@ _scp()
case $cur in
!(*:*)/* | [.~]*) ;; # looks like a path
*:*)
_comp_xfunc_ssh_scp_remote_files
_comp_xfunc_ssh_scp_remote_files "$sshoption"
return
;;
esac
Expand Down