---
name: connect-to-server
description: Use when the user says "connect to <server>", "ssh to <server>", "ssh into <server>", "log into <server>", "disconnect from <server>", or "close connection to <server>". Resolves the server name against Host aliases in ~/.ssh/config and opens a multiplexed SSH connection so subsequent ssh commands reuse the session.
---

# connect-to-server

## What this skill does

Resolve a natural-language server name (e.g. "zerina-prod", "the staging box") against Host aliases declared in `~/.ssh/config`, then open a backgrounded SSH ControlMaster socket. Subsequent `ssh` commands in this conversation reuse that socket so each command pays only one TCP/auth round-trip — not a full handshake.

Assumes a working SSH key is already configured for the target Host (via `IdentityFile` in `~/.ssh/config` or `ssh-agent`). The skill never modifies `~/.ssh/config`.

## Non-goals

- Driving an interactive shell on the remote host. Claude Code can't drive a real PTY; commands run non-interactively over the shared socket.
- Managing SSH keys or agents.
- Running privileged commands that require an interactive sudo prompt.

## Procedure

### 1. Extract the server name

The user's message will look like one of:
- "connect to zerina-prod"
- "ssh into staging"
- "log into 192.168.1.50"
- "disconnect from production"

Take the token (or quoted phrase) after the preposition ("to" / "into" / "from") as the server name. If the user says "connect to my server" with no name, ask which one and list the available aliases (see step 3).

### 2. Check whether the name is a literal address

If the name contains a `.` (IPv4 or FQDN) or a `:` (IPv6), skip alias lookup and use the name verbatim as the ssh target.

### 3. Parse Host aliases from ~/.ssh/config

```bash
awk '/^Host / && $2 !~ /\*/ { for (i=2; i<=NF; i++) print $i }' ~/.ssh/config
```

This emits one alias per line, excluding wildcard scopes like `Host *` or `Host *.example.com`.

If `~/.ssh/config` does not exist, tell the user and stop. Do not auto-create it. Suggest a sample block:

```
Host my-server
  HostName 1.2.3.4
  User myuser
  IdentityFile ~/.ssh/id_ed25519
```

If the file exists but contains only wildcard `Host` lines, treat it as no-aliases and report the same way.

### 4. Match the name against the alias list

Apply in order, stop at the first that resolves to a single alias:

1. **Exact match** (case-sensitive): the name equals an alias verbatim → use it.
2. **Substring match** (case-insensitive): the name appears as a substring of an alias → use it if exactly one matches.
3. **Multiple substring matches**: list them, ask the user to pick. Never auto-select.
4. **Zero matches**: tell the user the name didn't match anything, list all aliases, and stop.

### 5. Open the master socket (idempotent)

First check whether a master is already up:

```bash
ssh -O check -o ControlPath=~/.ssh/cm-%r@%h:%p <alias> 2>&1
```

- Exit 0 → master is already running. Skip to step 6.
- Non-zero exit → no master. Open one:

```bash
ssh -fN \
    -o ControlMaster=auto \
    -o ControlPath=~/.ssh/cm-%r@%h:%p \
    -o ControlPersist=30m \
    <alias>
```

Flags explained:
- `-f` background after authentication
- `-N` do not run a remote command (master only multiplexes for others)
- `ControlMaster=auto` open or reuse a master socket
- `ControlPath=~/.ssh/cm-%r@%h:%p` per-(remote-user, host, port) socket file
- `ControlPersist=30m` keep the master alive 30 minutes after the last child exits

If this command exits non-zero (auth failure, host unreachable, etc.), surface the stderr verbatim. Do not silently retry.

### 6. Verify the connection

Run a small command over the new master to confirm it works:

```bash
ssh -o ControlPath=~/.ssh/cm-%r@%h:%p <alias> 'hostname && whoami'
```

Show the user the hostname and username so they know which box and identity they're on.

### 7. Run subsequent commands on the same server

Every later ssh invocation in this conversation MUST include the same ControlPath flag so it rides the master:

```bash
ssh -o ControlPath=~/.ssh/cm-%r@%h:%p <alias> '<remote command>'
```

Without this flag, the call would open a fresh handshake instead of reusing the master.

## Lifecycle

### Idle timeout

`ControlPersist=30m` means the master self-closes after 30 minutes with no active children. The user does not need to disconnect manually. The next ssh call to that alias transparently reopens a master.

### Multiple servers concurrently

Each alias gets its own socket because the path template includes `%r@%h:%p`. Connecting to staging while production is open is fine — both masters coexist.

### Explicit disconnect

When the user says "disconnect from <name>", "close connection to <name>", or "close X":

```bash
ssh -O exit -o ControlPath=~/.ssh/cm-%r@%h:%p <alias>
```

If the user says "disconnect" with no name:
- If exactly one ControlMaster socket exists under `~/.ssh/cm-*`, close that one.
- If multiple exist, list them (parse the filenames) and ask which.
- If none exist, tell the user there is no active connection.

To list active masters:

```bash
ls ~/.ssh/cm-* 2>/dev/null
```

### Status check

When the user asks "am I still connected to <name>?" or similar:

```bash
ssh -O check -o ControlPath=~/.ssh/cm-%r@%h:%p <alias>
```

Exit 0 → connected. Non-zero → not connected. Report the result plainly.

## Errors and edge cases

| Situation | Behavior |
|---|---|
| `~/.ssh/config` missing | Tell user, suggest creating it with a sample `Host` block (see procedure step 3). Don't auto-create. |
| Only `Host *` / wildcard lines in config | Treat as no aliases. Same response as missing config. |
| `ssh -fN` fails (key rejected, host unreachable) | Surface stderr verbatim. No silent retry. Suggest the user check `IdentityFile`, `User`, and connectivity. |
| Multiple fuzzy matches | List candidates, ask user. Never auto-pick. |
| Name looks like IP / FQDN | Skip alias lookup, ssh directly with the literal name. |
| User says "disconnect" with no name, one socket open | Disconnect that one. |
| User says "disconnect" with no name, multiple sockets open | List sockets, ask which. |
| User says "disconnect" with no name, zero sockets | Tell user there is no active connection. |
| ssh command on remote returns non-zero | Show the user the exit code and stderr. Do not retry automatically. |
