home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / etc / bash_completion.d / ssh < prev    next >
Encoding:
Text File  |  2010-11-16  |  11.7 KB  |  428 lines

  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.             COMPREPLY=( "${COMPREPLY[@]}" $( compgen -c -- "$cur" ) )
  188.         fi
  189.     fi
  190.  
  191.     return 0
  192. }
  193. shopt -u hostcomplete && complete -F _ssh ssh slogin autossh
  194.  
  195. # sftp(1) completion
  196. #
  197. _sftp()
  198. {
  199.     local cur prev configfile
  200.  
  201.     COMPREPLY=()
  202.     _get_comp_words_by_ref cur prev
  203.  
  204.     _ssh_suboption_check && return 0
  205.  
  206.     case $prev in
  207.         -b|-F|-P)
  208.             _filedir
  209.             return 0
  210.             ;;
  211.         -o)
  212.             _ssh_options
  213.             return 0
  214.             ;;
  215.     esac
  216.  
  217.     if [[ "$cur" == -F* ]]; then
  218.         cur=${cur#-F}
  219.         _filedir
  220.         # Prefix completions with '-F'
  221.         COMPREPLY=( "${COMPREPLY[@]/#/-F}" )
  222.         cur=-F$cur  # Restore cur
  223.     elif [[ "$cur" == -* ]]; then
  224.         COMPREPLY=( $( compgen -W '-1 -C -v -B -b -F -o -P -R -S -s' \
  225.             -- "$cur" ) )
  226.     else
  227.         # Search COMP_WORDS for '-F configfile' argument
  228.         set -- "${COMP_WORDS[@]}"
  229.         while [ $# -gt 0 ]; do
  230.             if [ "${1:0:2}" = -F ]; then
  231.                 if [ ${#1} -gt 2 ]; then
  232.                     configfile="$(dequote "${1:2}")"
  233.                 else
  234.                     shift
  235.                     [ "$1" ] && configfile="$(dequote "$1")"
  236.                 fi
  237.                 break
  238.             fi
  239.             shift
  240.         done
  241.         _known_hosts_real -a -F "$configfile" "$cur"
  242.     fi
  243.  
  244.     return 0
  245. }
  246. shopt -u hostcomplete && complete -F _sftp sftp
  247.  
  248. # things we want to escape in remote scp paths
  249. _scp_path_esc="[][(){}<>\",:;^&\!$=?\`|\\ ']"
  250.  
  251. # Complete remote files with ssh.  If the first arg is -d, complete on dirs
  252. # only.  Returns paths escaped with three backslashes.
  253. _scp_remote_files()
  254. {
  255.     local IFS=$'\t\n'
  256.  
  257.     # remove backslash escape from the first colon
  258.     cur=${cur/\\:/:}
  259.  
  260.     local userhost=${cur%%?(\\):*}
  261.     local path=${cur#*:}
  262.  
  263.     # unescape (3 backslashes to 1 for chars we escaped)
  264.     path=$( sed -e 's/\\\\\\\('$_scp_path_esc'\)/\\\1/g' <<<"$path" )
  265.  
  266.     # default to home dir of specified user on remote host
  267.     if [ -z "$path" ]; then
  268.         path=$(ssh -o 'Batchmode yes' $userhost pwd 2>/dev/null)
  269.     fi
  270.  
  271.     local files
  272.     if [ "$1" = -d ] ; then
  273.         # escape problematic characters; remove non-dirs
  274.         files=$( ssh -o 'Batchmode yes' $userhost \
  275.             command ls -aF1d "$path*" 2>/dev/null | \
  276.             sed -e 's/'$_scp_path_esc'/\\\\\\&/g' -e '/[^\/]$/d' )
  277.     else
  278.         # escape problematic characters; remove executables, aliases, pipes
  279.         # and sockets; add space at end of file names
  280.         files=$( ssh -o 'Batchmode yes' $userhost \
  281.             command ls -aF1d "$path*" 2>/dev/null | \
  282.             sed -e 's/'$_scp_path_esc'/\\\\\\&/g' -e 's/[*@|=]$//g' \
  283.             -e 's/[^\/]$/& /g' )
  284.     fi
  285.     COMPREPLY=( "${COMPREPLY[@]}" $files )
  286. }
  287.  
  288. # This approach is used instead of _filedir to get a space appended
  289. # after local file/dir completions, and -o nospace retained for others.
  290. # If first arg is -d, complete on directory names only.  The next arg is
  291. # an optional prefix to add to returned completions.
  292. _scp_local_files()
  293. {
  294.     local IFS=$'\t\n'
  295.  
  296.     local dirsonly=false
  297.     if [ "$1" = -d ]; then
  298.         dirsonly=true
  299.         shift
  300.     fi
  301.  
  302.     if $dirsonly ; then
  303.         COMPREPLY=( "${COMPREPLY[@]}" $( command ls -aF1d $cur* 2>/dev/null | \
  304.             sed -e "s/$_scp_path_esc/\\\\&/g" -e '/[^\/]$/d' -e "s/^/$1/") )
  305.     else
  306.         COMPREPLY=( "${COMPREPLY[@]}" $( command ls -aF1d $cur* 2>/dev/null | \
  307.             sed -e "s/$_scp_path_esc/\\\\&/g" -e 's/[*@|=]$//g' \
  308.             -e 's/[^\/]$/& /g' -e "s/^/$1/") )
  309.     fi
  310. }
  311.  
  312. # scp(1) completion
  313. #
  314. _scp()
  315. {
  316.     local configfile cur prev prefix
  317.  
  318.     COMPREPLY=()
  319.     _get_comp_words_by_ref -n : cur prev
  320.  
  321.     _ssh_suboption_check && {
  322.         COMPREPLY=( "${COMPREPLY[@]/%/ }" )
  323.         return 0
  324.     }
  325.  
  326.     case $prev in
  327.         -l|-P)
  328.             return 0
  329.             ;;
  330.         -F|-i|-S)
  331.             _filedir
  332.             type compopt &>/dev/null && compopt +o nospace
  333.             return 0
  334.             ;;
  335.         -c)
  336.             _ssh_ciphers
  337.             COMPREPLY=( "${COMPREPLY[@]/%/ }" )
  338.             return 0
  339.             ;;
  340.         -o)
  341.             _ssh_options
  342.             return 0
  343.             ;;
  344.     esac
  345.  
  346.     _expand || return 0
  347.  
  348.     if [[ "$cur" == *:* ]]; then
  349.         _scp_remote_files
  350.         return 0
  351.     fi
  352.  
  353.     if [[ "$cur" == -F* ]]; then
  354.         cur=${cur#-F}
  355.         prefix=-F
  356.     else
  357.         # Search COMP_WORDS for '-F configfile' or '-Fconfigfile' argument
  358.         set -- "${COMP_WORDS[@]}"
  359.         while [ $# -gt 0 ]; do
  360.             if [ "${1:0:2}" = -F ]; then
  361.                 if [ ${#1} -gt 2 ]; then
  362.                     configfile="$(dequote "${1:2}")"
  363.                 else
  364.                     shift
  365.                     [ "$1" ] && configfile="$(dequote "$1")"
  366.                 fi
  367.                 break
  368.             fi
  369.             shift
  370.         done
  371.  
  372.         case $cur in
  373.             -*)
  374.                 COMPREPLY=( $( compgen -W '-1 -2 -4 -6 -B -C -c -F -i -l -o \
  375.                     -P -p -q -r -S -v' -- "$cur" ) )
  376.                 COMPREPLY=( "${COMPREPLY[@]/%/ }" )
  377.                 return 0
  378.                 ;;
  379.             */*)
  380.                 # pass through
  381.                 ;;
  382.             *)
  383.                 _known_hosts_real -c -a -F "$configfile" "$cur"
  384.                 ;;
  385.         esac
  386.     fi
  387.  
  388.     _scp_local_files "$prefix"
  389.  
  390.     return 0
  391. }
  392. complete -F _scp -o nospace scp
  393.  
  394. # ssh-copy-id(1) completion
  395. #
  396. _ssh_copy_id()
  397. {
  398.     local cur prev
  399.  
  400.     COMPREPLY=()
  401.     _get_comp_words_by_ref cur prev
  402.  
  403.     case $prev in
  404.         -i)
  405.             _filedir
  406.             return 0
  407.             ;;
  408.     esac
  409.  
  410.     if [[ "$cur" == -* ]]; then
  411.         COMPREPLY=( $( compgen -W '-i' -- "$cur" ) )
  412.     else
  413.         _known_hosts_real -a "$cur"
  414.     fi
  415.  
  416.     return 0
  417. }
  418. complete -F _ssh_copy_id -o filenames ssh-copy-id
  419. }
  420.  
  421. # Local variables:
  422. # mode: shell-script
  423. # sh-basic-offset: 4
  424. # sh-indent-comment: t
  425. # indent-tabs-mode: nil
  426. # End:
  427. # ex: ts=4 sw=4 et filetype=sh
  428.