Advertisement
Guest User

bash_completion.d ssh file cygwin

a guest
Apr 26th, 2015
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.13 KB | None | 0 0
  1. # ssh(1) completion
  2.  
  3. have ssh &&
  4. {
  5.  
  6. _ssh_bindaddress()
  7. {
  8.     COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W \
  9.         "$( PATH="$PATH:/sbin" ifconfig -a | \
  10.        sed -ne 's/.*addr:\([^[:space:]]*\).*/\1/p' \
  11.            -ne 's/.*inet[[:space:]]\{1,\}\([^[:space:]]*\).*/\1/p' )" \
  12.         -- "$cur" ) )
  13. }
  14.  
  15. _ssh_ciphers()
  16. {
  17.     COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W '3des-cbc aes128-cbc \
  18.        aes192-cbc aes256-cbc aes128-ctr aes192-ctr aes256-ctr arcfour128 \
  19.        arcfour256 arcfour blowfish-cbc cast128-cbc' -- "$cur" ) )
  20. }
  21.  
  22. _ssh_macs()
  23. {
  24.     COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W 'hmac-md5 hmac-sha1 \
  25.        umac-64@openssh.com hmac-ripemd160 hmac-sha1-96 hmac-md5-96' \
  26.         -- "$cur" ) )
  27. }
  28.  
  29. _ssh_options()
  30. {
  31.     type compopt &>/dev/null && compopt -o nospace
  32.     COMPREPLY=( $( compgen -S = -W 'AddressFamily BatchMode BindAddress \
  33.        ChallengeResponseAuthentication CheckHostIP Cipher Ciphers \
  34.        ClearAllForwardings Compression CompressionLevel ConnectionAttempts \
  35.        ConnectTimeout ControlMaster ControlPath DynamicForward EscapeChar \
  36.        ExitOnForwardFailure ForwardAgent ForwardX11 ForwardX11Trusted \
  37.        GatewayPorts GlobalKnownHostsFile GSSAPIAuthentication \
  38.        GSSAPIDelegateCredentials HashKnownHosts Host HostbasedAuthentication \
  39.        HostKeyAlgorithms HostKeyAlias HostName IdentityFile IdentitiesOnly \
  40.        KbdInteractiveDevices LocalCommand LocalForward LogLevel MACs \
  41.        NoHostAuthenticationForLocalhost NumberOfPasswordPrompts \
  42.        PasswordAuthentication PermitLocalCommand Port \
  43.        PreferredAuthentications Protocol ProxyCommand PubkeyAuthentication \
  44.        RekeyLimit RemoteForward RhostsRSAAuthentication RSAAuthentication \
  45.        SendEnv ServerAliveInterval ServerAliveCountMax SmartcardDevice \
  46.        StrictHostKeyChecking TCPKeepAlive Tunnel TunnelDevice \
  47.        UsePrivilegedPort User UserKnownHostsFile VerifyHostKeyDNS \
  48.        VisualHostKey XAuthLocation' -- "$cur" ) )
  49. }
  50.  
  51. # Complete a ssh suboption (like ForwardAgent=y<tab>)
  52. # Only one parameter: the string to complete including the equal sign.
  53. # Not all suboptions are completed.
  54. # Doesn't handle comma-separated lists.
  55. _ssh_suboption()
  56. {
  57.     # Split into subopt and subval
  58.     local prev=${1%%=*} cur=${1#*=}
  59.  
  60.     case $prev in
  61.         BatchMode|ChallengeResponseAuthentication|CheckHostIP|\
  62.         ClearAllForwardings|Compression|ExitOnForwardFailure|ForwardAgent|\
  63.         ForwardX11|ForwardX11Trusted|GatewayPorts|GSSAPIAuthentication|\
  64.         GSSAPIKeyExchange|GSSAPIDelegateCredentials|GSSAPITrustDns|\
  65.         HashKnownHosts|HostbasedAuthentication|IdentitiesOnly|\
  66.         KbdInteractiveAuthentication|KbdInteractiveDevices|\
  67.         NoHostAuthenticationForLocalhost|PasswordAuthentication|\
  68.         PubkeyAuthentication|RhostsRSAAuthentication|RSAAuthentication|\
  69.         StrictHostKeyChecking|TCPKeepAlive|UsePrivilegedPort|\
  70.         VerifyHostKeyDNS|VisualHostKey)
  71.             COMPREPLY=( $( compgen -W 'yes no' -- "$cur") )
  72.             ;;
  73.         AddressFamily)
  74.             COMPREPLY=( $( compgen -W 'any inet inet6' -- "$cur" ) )
  75.             ;;
  76.         BindAddress)
  77.             _ssh_bindaddress
  78.             ;;
  79.         Cipher)
  80.             COMPREPLY=( $( compgen -W 'blowfish des 3des' -- "$cur" ) )
  81.             ;;
  82.         Protocol)
  83.             COMPREPLY=( $( compgen -W '1 2 1,2 2,1' -- "$cur" ) )
  84.             ;;
  85.         Tunnel)
  86.             COMPREPLY=( $( compgen -W 'yes no point-to-point ethernet' \
  87.                     -- "$cur" ) )
  88.             ;;
  89.         PreferredAuthentications)
  90.             COMPREPLY=( $( compgen -W 'gssapi-with-mic host-based \
  91.                    publickey keyboard-interactive password' -- "$cur" ) )
  92.             ;;
  93.         MACs)
  94.             _ssh_macs
  95.             ;;
  96.         Ciphers)
  97.             _ssh_ciphers
  98.             ;;
  99.     esac
  100.     return 0
  101. }
  102.  
  103. # Try to complete -o SubOptions=
  104. #
  105. # Returns 0 if the completion was handled or non-zero otherwise.
  106. _ssh_suboption_check()
  107. {
  108.     # Get prev and cur words without splitting on =
  109.     local cureq=`_get_cword :=` preveq=`_get_pword :=`
  110.     if [[ $cureq == *=* && $preveq == -o ]]; then
  111.         _ssh_suboption $cureq
  112.         return $?
  113.     fi
  114.     return 1
  115. }
  116.  
  117. _ssh()
  118. {
  119.     local cur prev configfile
  120.     local -a config
  121.  
  122.     COMPREPLY=()
  123.     _get_comp_words_by_ref -n : cur prev
  124.     #cur=`_get_cword :`
  125.     #prev=`_get_pword`
  126.  
  127.     _ssh_suboption_check && return 0
  128.  
  129.     case $prev in
  130.         -F|-i|-S)
  131.             _filedir
  132.             return 0
  133.             ;;
  134.         -c)
  135.             _ssh_ciphers
  136.             return 0
  137.             ;;
  138.         -m)
  139.             _ssh_macs
  140.             return 0
  141.             ;;
  142.         -l)
  143.             COMPREPLY=( $( compgen -u -- "$cur" ) )
  144.             return 0
  145.             ;;
  146.         -o)
  147.             _ssh_options
  148.             return 0
  149.             ;;
  150.         -w)
  151.             _available_interfaces
  152.             return 0
  153.             ;;
  154.         -b)
  155.             _ssh_bindaddress
  156.             return 0
  157.             ;;
  158.     esac
  159.  
  160.     if [[ "$cur" == -F* ]]; then
  161.         cur=${cur#-F}
  162.         _filedir
  163.         # Prefix completions with '-F'
  164.         COMPREPLY=( "${COMPREPLY[@]/#/-F}" )
  165.         cur=-F$cur  # Restore cur
  166.     elif [[ "$cur" == -* ]]; then
  167.         COMPREPLY=( $( compgen -W '-1 -2 -4 -6 -A -a -C -f -g -K -k -M \
  168.            -N -n -q -s -T -t -V -v -X -v -Y -y -b -b -c -D -e -F \
  169.            -i -L -l -m -O -o -p -R -S -w' -- "$cur" ) )
  170.     else
  171.         # Search COMP_WORDS for '-F configfile' or '-Fconfigfile' argument
  172.         set -- "${COMP_WORDS[@]}"
  173.         while [ $# -gt 0 ]; do
  174.             if [ "${1:0:2}" = -F ]; then
  175.                 if [ ${#1} -gt 2 ]; then
  176.                     configfile="$(dequote "${1:2}")"
  177.                 else
  178.                     shift
  179.                     [ "$1" ] && configfile="$(dequote "$1")"
  180.                 fi
  181.                 break
  182.             fi
  183.             shift
  184.         done
  185.         _known_hosts_real -a -F "$configfile" "$cur"
  186.         if [ $COMP_CWORD -ne 1 ]; then
  187.             _compopt_o_filenames
  188.             COMPREPLY=( "${COMPREPLY[@]}" $( compgen -c -- "$cur" ) )
  189.         fi
  190.     fi
  191.  
  192.     return 0
  193. }
  194. shopt -u hostcomplete && complete -F _ssh ssh slogin autossh
  195.  
  196. # sftp(1) completion
  197. #
  198. _sftp()
  199. {
  200.     local cur prev configfile
  201.  
  202.     COMPREPLY=()
  203.     _get_comp_words_by_ref cur prev
  204.  
  205.     _ssh_suboption_check && return 0
  206.  
  207.     case $prev in
  208.         -b|-F|-P)
  209.             _filedir
  210.             return 0
  211.             ;;
  212.         -o)
  213.             _ssh_options
  214.             return 0
  215.             ;;
  216.     esac
  217.  
  218.     if [[ "$cur" == -F* ]]; then
  219.         cur=${cur#-F}
  220.         _filedir
  221.         # Prefix completions with '-F'
  222.         COMPREPLY=( "${COMPREPLY[@]/#/-F}" )
  223.         cur=-F$cur  # Restore cur
  224.     elif [[ "$cur" == -* ]]; then
  225.         COMPREPLY=( $( compgen -W '-1 -C -v -B -b -F -o -P -R -S -s' \
  226.             -- "$cur" ) )
  227.     else
  228.         # Search COMP_WORDS for '-F configfile' argument
  229.         set -- "${COMP_WORDS[@]}"
  230.         while [ $# -gt 0 ]; do
  231.             if [ "${1:0:2}" = -F ]; then
  232.                 if [ ${#1} -gt 2 ]; then
  233.                     configfile="$(dequote "${1:2}")"
  234.                 else
  235.                     shift
  236.                     [ "$1" ] && configfile="$(dequote "$1")"
  237.                 fi
  238.                 break
  239.             fi
  240.             shift
  241.         done
  242.         _known_hosts_real -a -F "$configfile" "$cur"
  243.     fi
  244.  
  245.     return 0
  246. }
  247. shopt -u hostcomplete && complete -F _sftp sftp
  248.  
  249. # things we want to backslash escape in scp paths
  250. _scp_path_esc='[][(){}<>",:;^&!$=?`|\\'"'"'[:space:]]'
  251.  
  252. # Complete remote files with ssh.  If the first arg is -d, complete on dirs
  253. # only.  Returns paths escaped with three backslashes.
  254. _scp_remote_files()
  255. {
  256.     local IFS=$'\n'
  257.  
  258.     # remove backslash escape from the first colon
  259.     cur=${cur/\\:/:}
  260.  
  261.     local userhost=${cur%%?(\\):*}
  262.     local path=${cur#*:}
  263.  
  264.     # unescape (3 backslashes to 1 for chars we escaped)
  265.     path=$( sed -e 's/\\\\\\\('$_scp_path_esc'\)/\\\1/g' <<<"$path" )
  266.  
  267.     # default to home dir of specified user on remote host
  268.     if [ -z "$path" ]; then
  269.         path=$(ssh -o 'Batchmode yes' $userhost pwd 2>/dev/null)
  270.     fi
  271.  
  272.     local files
  273.     if [ "$1" = -d ] ; then
  274.         # escape problematic characters; remove non-dirs
  275.         files=$( ssh -o 'Batchmode yes' $userhost \
  276.             command ls -aF1d "$path*" 2>/dev/null | \
  277.             sed -e 's/'$_scp_path_esc'/\\\\\\&/g' -e '/[^\/]$/d' )
  278.     else
  279.         # escape problematic characters; remove executables, aliases, pipes
  280.         # and sockets; add space at end of file names
  281.         files=$( ssh -o 'Batchmode yes' $userhost \
  282.             command ls -aF1d "$path*" 2>/dev/null | \
  283.             sed -e 's/'$_scp_path_esc'/\\\\\\&/g' -e 's/[*@|=]$//g' \
  284.             -e 's/[^\/]$/& /g' )
  285.     fi
  286.     COMPREPLY=( "${COMPREPLY[@]}" $files )
  287. }
  288.  
  289. # This approach is used instead of _filedir to get a space appended
  290. # after local file/dir completions, and -o nospace retained for others.
  291. # If first arg is -d, complete on directory names only.  The next arg is
  292. # an optional prefix to add to returned completions.
  293. _scp_local_files()
  294. {
  295.     local IFS=$'\n'
  296.  
  297.     local dirsonly=false
  298.     if [ "$1" = -d ]; then
  299.         dirsonly=true
  300.         shift
  301.     fi
  302.  
  303.     if $dirsonly ; then
  304.         COMPREPLY=( "${COMPREPLY[@]}" $( command ls -aF1d $cur* 2>/dev/null | \
  305.             sed -e "s/$_scp_path_esc/\\\\&/g" -e '/[^\/]$/d' -e "s/^/$1/") )
  306.     else
  307.         COMPREPLY=( "${COMPREPLY[@]}" $( command ls -aF1d $cur* 2>/dev/null | \
  308.             sed -e "s/$_scp_path_esc/\\\\&/g" -e 's/[*@|=]$//g' \
  309.             -e 's/[^\/]$/& /g' -e "s/^/$1/") )
  310.     fi
  311. }
  312.  
  313. # scp(1) completion
  314. #
  315. _scp()
  316. {
  317.     local configfile cur prev prefix
  318.  
  319.     COMPREPLY=()
  320.     _get_comp_words_by_ref -n : cur prev
  321.  
  322.     _ssh_suboption_check && {
  323.         COMPREPLY=( "${COMPREPLY[@]/%/ }" )
  324.         return 0
  325.     }
  326.  
  327.     case $prev in
  328.         -l|-P)
  329.             return 0
  330.             ;;
  331.         -F|-i|-S)
  332.             _filedir
  333.             type compopt &>/dev/null && compopt +o nospace
  334.             return 0
  335.             ;;
  336.         -c)
  337.             _ssh_ciphers
  338.             COMPREPLY=( "${COMPREPLY[@]/%/ }" )
  339.             return 0
  340.             ;;
  341.         -o)
  342.             _ssh_options
  343.             return 0
  344.             ;;
  345.     esac
  346.  
  347.     _expand || return 0
  348.  
  349.     if [[ "$cur" == *:* ]]; then
  350.         _scp_remote_files
  351.         return 0
  352.     fi
  353.  
  354.     if [[ "$cur" == -F* ]]; then
  355.         cur=${cur#-F}
  356.         prefix=-F
  357.     else
  358.         # Search COMP_WORDS for '-F configfile' or '-Fconfigfile' argument
  359.         set -- "${COMP_WORDS[@]}"
  360.         while [ $# -gt 0 ]; do
  361.             if [ "${1:0:2}" = -F ]; then
  362.                 if [ ${#1} -gt 2 ]; then
  363.                     configfile="$(dequote "${1:2}")"
  364.                 else
  365.                     shift
  366.                     [ "$1" ] && configfile="$(dequote "$1")"
  367.                 fi
  368.                 break
  369.             fi
  370.             shift
  371.         done
  372.  
  373.         case $cur in
  374.             -*)
  375.                 COMPREPLY=( $( compgen -W '-1 -2 -4 -6 -B -C -c -F -i -l -o \
  376.                    -P -p -q -r -S -v' -- "$cur" ) )
  377.                 COMPREPLY=( "${COMPREPLY[@]/%/ }" )
  378.                 return 0
  379.                 ;;
  380.             */*)
  381.                 # pass through
  382.                 ;;
  383.             *)
  384.                 _known_hosts_real -c -a -F "$configfile" "$cur"
  385.                 ;;
  386.         esac
  387.     fi
  388.  
  389.     _scp_local_files "$prefix"
  390.  
  391.     return 0
  392. }
  393. complete -F _scp -o nospace scp
  394.  
  395. # ssh-copy-id(1) completion
  396. #
  397. _ssh_copy_id()
  398. {
  399.     local cur prev
  400.  
  401.     COMPREPLY=()
  402.     _get_comp_words_by_ref cur prev
  403.  
  404.     case $prev in
  405.         -i)
  406.             _filedir
  407.             return 0
  408.             ;;
  409.     esac
  410.  
  411.     if [[ "$cur" == -* ]]; then
  412.         COMPREPLY=( $( compgen -W '-i' -- "$cur" ) )
  413.     else
  414.         _known_hosts_real -a "$cur"
  415.     fi
  416.  
  417.     return 0
  418. }
  419. complete -F _ssh_copy_id ssh-copy-id
  420. }
  421.  
  422. # Local variables:
  423. # mode: shell-script
  424. # sh-basic-offset: 4
  425. # sh-indent-comment: t
  426. # indent-tabs-mode: nil
  427. # End:
  428. # ex: ts=4 sw=4 et filetype=sh
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement