mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 14:47:47 +00:00
Add ssh completion (#891)
Read ssh config from `/etc/ssh/ssh_config` and `~/.ssh/config`. Given:  Will show: 
This commit is contained in:
parent
d6cf03e315
commit
bfd2af7106
2 changed files with 92 additions and 0 deletions
40
custom-completions/ssh/README.md
Normal file
40
custom-completions/ssh/README.md
Normal file
|
@ -0,0 +1,40 @@
|
|||
# SSH completions
|
||||
|
||||
A Nushell extern definition and completers for the ssh command.
|
||||
|
||||
This module provides extern definitions for most of the ssh command options and flags.
|
||||
|
||||
## Usage
|
||||
|
||||
Simply import the extern definitions with
|
||||
|
||||
```nu
|
||||
source path/to/ssh-completions.nu
|
||||
```
|
||||
|
||||
This script will parse `/etc/ssh/ssh_config` and `~/.ssh/config` to fetch SSH config hosts.
|
||||
|
||||
Given the following config:
|
||||
|
||||
```
|
||||
Host my-ip
|
||||
HostName 192.168.50.237
|
||||
Host mydomain
|
||||
HostName mydomain.example.com
|
||||
Host my-domain-2
|
||||
HostName mydomain-2.example.com
|
||||
Host my_domain_3
|
||||
HostName mydomain_3.example.com
|
||||
```
|
||||
|
||||
|
||||
When you press the tab key, it will display:
|
||||
|
||||
```
|
||||
❯ | ssh
|
||||
my-ip 192.168.50.237
|
||||
mydomain mydomain.example.com
|
||||
my-domain-2 mydomain-2.example.com
|
||||
my_domain_3 mydomain_3.example.com
|
||||
|
||||
```
|
52
custom-completions/ssh/ssh-completions.nu
Normal file
52
custom-completions/ssh/ssh-completions.nu
Normal file
|
@ -0,0 +1,52 @@
|
|||
export extern "ssh" [
|
||||
destination?: string@"nu-complete ssh-host"
|
||||
-4 # Forces ssh to use IPv4 addresses only.
|
||||
-6 # Forces ssh to use IPv6 addresses only.
|
||||
-A # Enables forwarding of connections from an authentication agent such as ssh-agent(1).
|
||||
-a # Disables forwarding of the authentication agent connection.
|
||||
-B: string # bind_interface
|
||||
-b: string # bind_address
|
||||
-C # Requests compression of all data
|
||||
-c: string # cipher_spec
|
||||
-D # [bind_address:]port
|
||||
-E: string # log_file
|
||||
-e # escape_char
|
||||
-F: string # configfile
|
||||
-f # Requests ssh to go to background just before command execution.
|
||||
-G # Causes ssh to print its configuration after evaluating Host and Match blocks and exit.
|
||||
-g # Allows remote hosts to connect to local forwarded ports
|
||||
-I: string # pkcs11
|
||||
-i: string # identity_file
|
||||
-J: string # destination
|
||||
-K # Enables GSSAPI-based authentication and forwarding(delegation) of GSSAPI credentials to the server.
|
||||
-k # Disables forwarding (delegation) of GSSAPI credentials to the server.
|
||||
-L: string # [bind_address:]port:host:hostport / [bind_address:]port:remote_socket / local_socket:host:hostport / local_socket:remote_socket
|
||||
-l: string # login_name
|
||||
-M # Places the ssh client into “master” mode for connection sharing.
|
||||
-m: string # mac_spec
|
||||
-N # Do not execute a remote command.
|
||||
-n # Redirects stdin from /dev/null (5) for details.
|
||||
-O: string # ctl_cmd
|
||||
-o: string # option
|
||||
]
|
||||
|
||||
def "nu-complete ssh-host" [] {
|
||||
let files = [
|
||||
'/etc/ssh/ssh_config',
|
||||
'~/.ssh/config'
|
||||
] | filter { |file| $file | path exists }
|
||||
|
||||
$files | each { |file|
|
||||
let lines = $file | open | lines | str trim
|
||||
let hosts = $lines
|
||||
| parse --regex '^Host\s+(?<host>.+)'
|
||||
| get host
|
||||
|
||||
let hostnames = $lines
|
||||
| parse --regex '^HostName\s+(?<hostname>.+)'
|
||||
| get hostname
|
||||
$hosts | zip $hostnames | each { ||
|
||||
{'value': $in.0, 'description': $in.1}
|
||||
}
|
||||
} | flatten
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue