graphical-ssh 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- graphical_ssh-0.1.1.dist-info/METADATA +151 -0
- graphical_ssh-0.1.1.dist-info/RECORD +18 -0
- graphical_ssh-0.1.1.dist-info/WHEEL +4 -0
- graphical_ssh-0.1.1.dist-info/entry_points.txt +3 -0
- graphical_ssh-0.1.1.dist-info/licenses/LICENSE +21 -0
- gssh/__init__.py +3 -0
- gssh/__main__.py +43 -0
- gssh/config.py +245 -0
- gssh/connect.py +156 -0
- gssh/data/__init__.py +0 -0
- gssh/data/gssh.desktop +10 -0
- gssh/data/gssh.svg +26 -0
- gssh/host_editor.py +174 -0
- gssh/linter.py +402 -0
- gssh/main_window.py +384 -0
- gssh/models.py +91 -0
- gssh/py.typed +0 -0
- gssh/terminal.py +490 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: graphical-ssh
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Graphical SSH client connection and configuration manager for Debian
|
|
5
|
+
Keywords: ssh,terminal,config,gui,pyqt,debian
|
|
6
|
+
Author: Greg Grace
|
|
7
|
+
Author-email: Greg Grace <ggrace@519lab.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Environment :: X11 Applications :: Qt
|
|
12
|
+
Classifier: Intended Audience :: System Administrators
|
|
13
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Topic :: System :: Networking
|
|
18
|
+
Classifier: Topic :: Terminals
|
|
19
|
+
Requires-Dist: pyqt6>=6.7
|
|
20
|
+
Requires-Dist: pyte>=0.8
|
|
21
|
+
Requires-Dist: pytest>=8.0 ; extra == 'dev'
|
|
22
|
+
Requires-Dist: pytest-cov>=5.0 ; extra == 'dev'
|
|
23
|
+
Requires-Dist: pytest-qt>=4.4 ; extra == 'dev'
|
|
24
|
+
Requires-Dist: ruff>=0.6 ; extra == 'dev'
|
|
25
|
+
Requires-Python: >=3.12
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# gssh
|
|
30
|
+
|
|
31
|
+
A graphical SSH client connection and configuration manager for Debian/Linux. Manage your `~/.ssh/config` hosts through a GUI, edit connection parameters, and open SSH sessions — either in an embedded terminal or by launching your preferred external terminal emulator.
|
|
32
|
+
|
|
33
|
+
## Features
|
|
34
|
+
|
|
35
|
+
- **Visual host management** — view, add, edit, and delete Host entries from `~/.ssh/config`
|
|
36
|
+
- **Dedicated fields** for the common options: HostName, User, Port, IdentityFile, ProxyJump, ForwardAgent
|
|
37
|
+
- **Extra options** — any arbitrary ssh_config directive via a freeform text area
|
|
38
|
+
- **Safe writes** — atomic file replacement with automatic timestamped backups (`.config.bak.YYYYMMDD_HHMMSS`) and `0600` permissions
|
|
39
|
+
- **Config linter** — catches unknown/deprecated options, insecure key permissions, invalid ports, undefined ProxyJump references, and pattern-overlap conflicts (first-match-wins)
|
|
40
|
+
- **Connection test** — TCP ping-before-connect with latency display
|
|
41
|
+
- **Wildcard hosts** — supports patterns like `*.lab.local`
|
|
42
|
+
- **Search/filter** — live filter by alias, hostname, or user
|
|
43
|
+
- **In-app terminal** — embedded PTY-based terminal using [pyte](https://github.com/selectel/pyte) for VT220 emulation with 5000-line scrollback and 16-colour ANSI
|
|
44
|
+
- **External terminal** — toggle to launch `ssh` in konsole, gnome-terminal, xterm, or any detected emulator
|
|
45
|
+
- **Round-trip safe** — preserves comments, preamble, and unknown directives
|
|
46
|
+
|
|
47
|
+
## Requirements
|
|
48
|
+
|
|
49
|
+
- Python 3.12+
|
|
50
|
+
- PyQt6
|
|
51
|
+
- pyte
|
|
52
|
+
- (Optional) a terminal emulator for external mode: konsole, gnome-terminal, xterm, etc.
|
|
53
|
+
|
|
54
|
+
## Install
|
|
55
|
+
|
|
56
|
+
### From PyPI (recommended)
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install gssh
|
|
60
|
+
gssh
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Or run without installing:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
uvx gssh # via uv
|
|
67
|
+
pipx run gssh # via pipx
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### From source
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
git clone https://github.com/ggrace519/graphical-ssh.git
|
|
74
|
+
cd graphical-ssh
|
|
75
|
+
uv sync
|
|
76
|
+
uv run gssh
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Desktop integration
|
|
80
|
+
|
|
81
|
+
To add gssh to your application menu:
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
# If installed via pip, the data files are inside the package:
|
|
85
|
+
python -c "import gssh.data; print(gssh.data.__path__)"
|
|
86
|
+
sudo cp $(python -c "import gssh.data; print(gssh.data.__path__[0])")/gssh.desktop /usr/share/applications/
|
|
87
|
+
sudo cp $(python -c "import gssh.data; print(gssh.data.__path__[0])")/gssh.svg /usr/share/icons/hicolor/scalable/apps/
|
|
88
|
+
sudo update-desktop-database
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Usage
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
# Use default ~/.ssh/config
|
|
95
|
+
uv run gssh
|
|
96
|
+
|
|
97
|
+
# Specify a custom config file
|
|
98
|
+
uv run gssh -c /path/to/ssh/config
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### GUI controls
|
|
102
|
+
|
|
103
|
+
| Action | How |
|
|
104
|
+
|---|---|
|
|
105
|
+
| Add a host | **Add Host** button → fill in the dialog |
|
|
106
|
+
| Edit a host | Select row → **Edit** button (or double-click to connect) |
|
|
107
|
+
| Delete a host | Select row → **Delete** button → confirm |
|
|
108
|
+
| Connect | Select row → **Connect** (or double-click the row) |
|
|
109
|
+
| Toggle terminal mode | **In-App Terminal** / **External Terminal** toggle |
|
|
110
|
+
| Filter hosts | Type in the search bar |
|
|
111
|
+
| Save changes | **Save** button (writes to config file with backup) |
|
|
112
|
+
| Reload from disk | **Reload** button |
|
|
113
|
+
|
|
114
|
+
## Development
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
# Run tests with coverage
|
|
118
|
+
uv run pytest
|
|
119
|
+
|
|
120
|
+
# Run the app
|
|
121
|
+
uv run gssh
|
|
122
|
+
|
|
123
|
+
# Run with a test config
|
|
124
|
+
uv run gssh -c /tmp/test-config
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Project structure
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
src/gssh/
|
|
131
|
+
├── __init__.py # package metadata
|
|
132
|
+
├── __main__.py # CLI entry point (argparse + QApplication)
|
|
133
|
+
├── config.py # SSH config parser/writer (round-trip safe, atomic writes)
|
|
134
|
+
├── models.py # Qt table model for host entries
|
|
135
|
+
├── host_editor.py # Add/Edit host dialog
|
|
136
|
+
├── connect.py # SSH command builder + launch (in-app or external)
|
|
137
|
+
├── terminal.py # In-app terminal widget (pyte + PTY)
|
|
138
|
+
└── main_window.py # Main window: host list, toolbar, terminal container
|
|
139
|
+
|
|
140
|
+
tests/
|
|
141
|
+
└── test_config.py # Parser/writer tests (20 tests, 90% config coverage)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Roadmap
|
|
145
|
+
|
|
146
|
+
- **Tier 2:** key file management (generate, view, copy public key), known-hosts viewer, connection test, import/export
|
|
147
|
+
- **Tier 3:** port-forwarding manager, per-host tags/notes, system tray quick-connect
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
MIT
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
gssh/__init__.py,sha256=LNUYGX1heB0vqMGb4wPmdGx_QB3LNkz64l-jRPqUQRg,97
|
|
2
|
+
gssh/__main__.py,sha256=ohqkL2bQPsMrA6jUDil_-8Z2ftbEXoFBaDwSH2TBews,910
|
|
3
|
+
gssh/config.py,sha256=DXm15fXdXbo64ekejgJGv_WxlFwRG5HKvSnrso07qpY,7461
|
|
4
|
+
gssh/connect.py,sha256=78Y7RM14QJbIU4gk9vQdzjDVtzfcyDpAbMeZI1eCjdA,4846
|
|
5
|
+
gssh/data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
gssh/data/gssh.desktop,sha256=bfGIkMCtElNbBJNRvqdz7rhr4ZYgIj4yCAzRbude7zk,253
|
|
7
|
+
gssh/data/gssh.svg,sha256=Wta2J2s6MYxT5lCk52m3bQBHGcCymCMf2LC97YUUDjo,1420
|
|
8
|
+
gssh/host_editor.py,sha256=umeHZpKSJ_6MBP6m-shjMMVUKAdCS9D7gnQL76B_qNM,5743
|
|
9
|
+
gssh/linter.py,sha256=UlTIAmFnaYPVtgeAlC1qwKA4A0e5rDtnftZ2diImyXw,14332
|
|
10
|
+
gssh/main_window.py,sha256=dOtET9JXXonMi21Gn3WtJ0baUqXjYU800TnOwqW4BSw,14772
|
|
11
|
+
gssh/models.py,sha256=P7E8Y9odGAIjmhXNjZSdXboSdgTnq4q80Pq4VkBAKPg,3248
|
|
12
|
+
gssh/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
|
+
gssh/terminal.py,sha256=SpB2slF7jl3OqxIjxGIHdB_3VYoGWrqLegDJrxkkVr8,17378
|
|
14
|
+
graphical_ssh-0.1.1.dist-info/licenses/LICENSE,sha256=uQMF9ITa2aEl5F3I8Lpdx2k12NPpsTc1wekmpSIaH4o,1066
|
|
15
|
+
graphical_ssh-0.1.1.dist-info/WHEEL,sha256=4OL6Foqnnp3xRY5wMkjgc25_i5YJC6dKsC6LPcjqEoU,80
|
|
16
|
+
graphical_ssh-0.1.1.dist-info/entry_points.txt,sha256=G8FgJ05c1l3-OTvk_k-YzXkdp3VddiG9aur3Cv8u7KM,45
|
|
17
|
+
graphical_ssh-0.1.1.dist-info/METADATA,sha256=OBRkwh4dN1Y2yVXglMKF3aatNisEmx0s-IwT8r_ZM9I,5108
|
|
18
|
+
graphical_ssh-0.1.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Greg Grace
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
gssh/__init__.py
ADDED
gssh/__main__.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Entry point for gssh."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from PyQt6.QtWidgets import QApplication
|
|
10
|
+
|
|
11
|
+
from . import __version__
|
|
12
|
+
from .main_window import MainWindow
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def main() -> int:
|
|
16
|
+
parser = argparse.ArgumentParser(
|
|
17
|
+
prog="gssh",
|
|
18
|
+
description="Graphical SSH client connection and configuration manager",
|
|
19
|
+
)
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"-c", "--config",
|
|
22
|
+
type=Path,
|
|
23
|
+
default=None,
|
|
24
|
+
help="Path to ssh config file (default: ~/.ssh/config)",
|
|
25
|
+
)
|
|
26
|
+
parser.add_argument(
|
|
27
|
+
"--version",
|
|
28
|
+
action="version",
|
|
29
|
+
version=f"gssh {__version__}",
|
|
30
|
+
)
|
|
31
|
+
args = parser.parse_args()
|
|
32
|
+
|
|
33
|
+
app = QApplication(sys.argv)
|
|
34
|
+
app.setApplicationName("gssh")
|
|
35
|
+
|
|
36
|
+
window = MainWindow(config_path=args.config)
|
|
37
|
+
window.show()
|
|
38
|
+
|
|
39
|
+
return app.exec()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
if __name__ == "__main__":
|
|
43
|
+
sys.exit(main())
|
gssh/config.py
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"""SSH client configuration parser and writer.
|
|
2
|
+
|
|
3
|
+
Reads and writes ~/.ssh/config in a round-trip-safe manner. Preserves
|
|
4
|
+
global options (outside Host blocks) and per-host options. Unknown
|
|
5
|
+
directives are kept verbatim so nothing is lost on save.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
import re
|
|
12
|
+
import shutil
|
|
13
|
+
import tempfile
|
|
14
|
+
from collections.abc import Iterator
|
|
15
|
+
from dataclasses import dataclass, field
|
|
16
|
+
from datetime import datetime
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
|
|
19
|
+
__all__ = ["HostEntry", "SSHConfig", "load_config", "save_config"]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# Keys that are commonly edited in the GUI — shown as dedicated fields.
|
|
23
|
+
# Everything else goes into the "extra options" area.
|
|
24
|
+
GUI_FIELDS = {
|
|
25
|
+
"hostname", "user", "port", "identityfile",
|
|
26
|
+
"proxyjump", "forwardagent",
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class HostEntry:
|
|
32
|
+
"""A single Host block in ssh_config."""
|
|
33
|
+
|
|
34
|
+
pattern: str # the pattern(s) after "Host ", e.g. "myserver" or "*.example.com"
|
|
35
|
+
options: dict[str, str] = field(default_factory=dict)
|
|
36
|
+
# Raw lines that belong to this block but weren't key/value (comments, blank lines)
|
|
37
|
+
raw_trailing: list[str] = field(default_factory=list)
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def alias(self) -> str:
|
|
41
|
+
"""Primary alias — first pattern, stripped of wildcards for display."""
|
|
42
|
+
return self.pattern.split()[0] if self.pattern else ""
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def hostname(self) -> str:
|
|
46
|
+
return self.options.get("hostname", "")
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def user(self) -> str:
|
|
50
|
+
return self.options.get("user", "")
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def port(self) -> str:
|
|
54
|
+
return self.options.get("port", "")
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def identity_file(self) -> str:
|
|
58
|
+
return self.options.get("identityfile", "")
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def proxy_jump(self) -> str:
|
|
62
|
+
return self.options.get("proxyjump", "")
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def forward_agent(self) -> str:
|
|
66
|
+
return self.options.get("forwardagent", "")
|
|
67
|
+
|
|
68
|
+
def extra_options(self) -> dict[str, str]:
|
|
69
|
+
"""Options not shown as dedicated GUI fields."""
|
|
70
|
+
return {k: v for k, v in self.options.items() if k not in GUI_FIELDS}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@dataclass
|
|
74
|
+
class SSHConfig:
|
|
75
|
+
"""Full ssh_config file representation."""
|
|
76
|
+
|
|
77
|
+
# Global lines before any Host block (comments, includes, global options)
|
|
78
|
+
preamble: list[str] = field(default_factory=list)
|
|
79
|
+
hosts: list[HostEntry] = field(default_factory=list)
|
|
80
|
+
# Trailing lines after the last Host block
|
|
81
|
+
postamble: list[str] = field(default_factory=list)
|
|
82
|
+
|
|
83
|
+
def find_host(self, alias: str) -> HostEntry | None:
|
|
84
|
+
for h in self.hosts:
|
|
85
|
+
if h.alias == alias:
|
|
86
|
+
return h
|
|
87
|
+
return None
|
|
88
|
+
|
|
89
|
+
def __iter__(self) -> Iterator[HostEntry]:
|
|
90
|
+
return iter(self.hosts)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
_LINE_RE = re.compile(r"^\s*(\S+)\s+(.+?)\s*$")
|
|
94
|
+
_HOST_RE = re.compile(r"^\s*[Hh]ost\s+(.+?)\s*$")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _parse_value(raw: str) -> str:
|
|
98
|
+
"""Strip quotes and whitespace from a value."""
|
|
99
|
+
v = raw.strip()
|
|
100
|
+
if len(v) >= 2 and v[0] in "\"'" and v[-1] == v[0]:
|
|
101
|
+
v = v[1:-1]
|
|
102
|
+
return v
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def load_config(path: Path | None = None) -> SSHConfig:
|
|
106
|
+
"""Parse an ssh_config file into an SSHConfig structure.
|
|
107
|
+
|
|
108
|
+
If *path* is None, defaults to ~/.ssh/config. If the file does not
|
|
109
|
+
exist, returns an empty SSHConfig.
|
|
110
|
+
"""
|
|
111
|
+
if path is None:
|
|
112
|
+
path = Path.home() / ".ssh" / "config"
|
|
113
|
+
|
|
114
|
+
config = SSHConfig()
|
|
115
|
+
|
|
116
|
+
if not path.exists():
|
|
117
|
+
return config
|
|
118
|
+
|
|
119
|
+
lines = path.read_text(encoding="utf-8", errors="replace").splitlines(keepends=False)
|
|
120
|
+
|
|
121
|
+
preamble: list[str] = []
|
|
122
|
+
hosts: list[HostEntry] = []
|
|
123
|
+
postamble: list[str] = []
|
|
124
|
+
|
|
125
|
+
current_host: HostEntry | None = None
|
|
126
|
+
current_lines: list[str] = []
|
|
127
|
+
|
|
128
|
+
for line in lines:
|
|
129
|
+
m = _HOST_RE.match(line)
|
|
130
|
+
if m:
|
|
131
|
+
# Finish previous host
|
|
132
|
+
if current_host is not None:
|
|
133
|
+
current_host.raw_trailing = _extract_trailing(current_lines)
|
|
134
|
+
hosts.append(current_host)
|
|
135
|
+
else:
|
|
136
|
+
preamble.extend(current_lines)
|
|
137
|
+
|
|
138
|
+
current_lines = []
|
|
139
|
+
current_host = HostEntry(pattern=m.group(1))
|
|
140
|
+
continue
|
|
141
|
+
|
|
142
|
+
if current_host is not None:
|
|
143
|
+
# Parse option line inside a Host block
|
|
144
|
+
stripped = line.strip()
|
|
145
|
+
if stripped == "" or stripped.startswith("#"):
|
|
146
|
+
current_lines.append(line)
|
|
147
|
+
else:
|
|
148
|
+
kv = _LINE_RE.match(line)
|
|
149
|
+
if kv:
|
|
150
|
+
key = kv.group(1).lower()
|
|
151
|
+
val = _parse_value(kv.group(2))
|
|
152
|
+
current_host.options[key] = val
|
|
153
|
+
else:
|
|
154
|
+
current_lines.append(line)
|
|
155
|
+
else:
|
|
156
|
+
current_lines.append(line)
|
|
157
|
+
|
|
158
|
+
# Finish up
|
|
159
|
+
if current_host is not None:
|
|
160
|
+
current_host.raw_trailing = _extract_trailing(current_lines)
|
|
161
|
+
hosts.append(current_host)
|
|
162
|
+
else:
|
|
163
|
+
preamble.extend(current_lines)
|
|
164
|
+
|
|
165
|
+
# Split preamble vs postamble — everything before first host is preamble,
|
|
166
|
+
# anything after the last host is postamble (already empty since we append
|
|
167
|
+
# trailing lines to the host). For simplicity, postamble is empty.
|
|
168
|
+
config.preamble = preamble
|
|
169
|
+
config.hosts = hosts
|
|
170
|
+
config.postamble = postamble
|
|
171
|
+
|
|
172
|
+
return config
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _extract_trailing(lines: list[str]) -> list[str]:
|
|
176
|
+
"""Extract trailing comment/blank lines that belong to a host block."""
|
|
177
|
+
result: list[str] = []
|
|
178
|
+
# Collect trailing blank lines and comments
|
|
179
|
+
for line in reversed(lines):
|
|
180
|
+
stripped = line.strip()
|
|
181
|
+
if stripped == "" or stripped.startswith("#"):
|
|
182
|
+
result.insert(0, line)
|
|
183
|
+
else:
|
|
184
|
+
break
|
|
185
|
+
return result
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def save_config(config: SSHConfig, path: Path | None = None) -> Path:
|
|
189
|
+
"""Write an SSHConfig back to disk atomically.
|
|
190
|
+
|
|
191
|
+
Creates a backup of the existing file with a .bak timestamp suffix.
|
|
192
|
+
Returns the path written.
|
|
193
|
+
"""
|
|
194
|
+
if path is None:
|
|
195
|
+
path = Path.home() / ".ssh" / "config"
|
|
196
|
+
|
|
197
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
198
|
+
|
|
199
|
+
# Backup existing file
|
|
200
|
+
if path.exists():
|
|
201
|
+
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
202
|
+
backup = path.with_suffix(f".config.bak.{ts}")
|
|
203
|
+
shutil.copy2(path, backup)
|
|
204
|
+
|
|
205
|
+
# Build output
|
|
206
|
+
out_lines: list[str] = []
|
|
207
|
+
|
|
208
|
+
if config.preamble:
|
|
209
|
+
out_lines.extend(config.preamble)
|
|
210
|
+
if out_lines and out_lines[-1].strip() != "":
|
|
211
|
+
out_lines.append("")
|
|
212
|
+
|
|
213
|
+
for host in config.hosts:
|
|
214
|
+
out_lines.append(f"Host {host.pattern}")
|
|
215
|
+
for key, val in host.options.items():
|
|
216
|
+
# Capitalize first letter for ssh_config convention
|
|
217
|
+
display_key = key[0].upper() + key[1:]
|
|
218
|
+
out_lines.append(f" {display_key} {val}")
|
|
219
|
+
if host.raw_trailing:
|
|
220
|
+
out_lines.extend(host.raw_trailing)
|
|
221
|
+
out_lines.append("")
|
|
222
|
+
|
|
223
|
+
if config.postamble:
|
|
224
|
+
out_lines.extend(config.postamble)
|
|
225
|
+
|
|
226
|
+
content = "\n".join(out_lines)
|
|
227
|
+
if content and not content.endswith("\n"):
|
|
228
|
+
content += "\n"
|
|
229
|
+
|
|
230
|
+
# Atomic write: temp file in same dir, then rename
|
|
231
|
+
fd, tmp_path = tempfile.mkstemp(
|
|
232
|
+
dir=str(path.parent), prefix=".config.", suffix=".tmp"
|
|
233
|
+
)
|
|
234
|
+
try:
|
|
235
|
+
# Set restrictive permissions (ssh requires config to not be group/world writable)
|
|
236
|
+
os.fchmod(fd, 0o600)
|
|
237
|
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
238
|
+
f.write(content)
|
|
239
|
+
os.replace(tmp_path, path)
|
|
240
|
+
except Exception:
|
|
241
|
+
if os.path.exists(tmp_path):
|
|
242
|
+
os.unlink(tmp_path)
|
|
243
|
+
raise
|
|
244
|
+
|
|
245
|
+
return path
|
gssh/connect.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"""Connection launcher — spawns SSH sessions either in-app or in an external terminal."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import shutil
|
|
6
|
+
import socket
|
|
7
|
+
import subprocess
|
|
8
|
+
import time
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
|
|
11
|
+
from .config import HostEntry
|
|
12
|
+
from .terminal import TerminalWidget
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class ConnectionTestResult:
|
|
17
|
+
"""Result of a connection test."""
|
|
18
|
+
success: bool
|
|
19
|
+
latency_ms: float
|
|
20
|
+
error: str = ""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def build_ssh_command(host: HostEntry) -> list[str]:
|
|
24
|
+
"""Build the ssh command line for a host entry.
|
|
25
|
+
|
|
26
|
+
Uses the alias so that all ssh_config options (ProxyJump, IdentityFile,
|
|
27
|
+
etc.) are applied automatically by the ssh client.
|
|
28
|
+
"""
|
|
29
|
+
cmd = ["ssh"]
|
|
30
|
+
# Use the first pattern that isn't a wildcard
|
|
31
|
+
alias = host.alias
|
|
32
|
+
if "*" in alias:
|
|
33
|
+
# For wildcard hosts, build explicit connection
|
|
34
|
+
if host.hostname:
|
|
35
|
+
target = host.hostname
|
|
36
|
+
if host.user:
|
|
37
|
+
target = f"{host.user}@{target}"
|
|
38
|
+
cmd.append(target)
|
|
39
|
+
if host.port and host.port != "22":
|
|
40
|
+
cmd.extend(["-p", host.port])
|
|
41
|
+
if host.identity_file:
|
|
42
|
+
cmd.extend(["-i", host.identity_file])
|
|
43
|
+
if host.proxy_jump:
|
|
44
|
+
cmd.extend(["-J", host.proxy_jump])
|
|
45
|
+
return cmd
|
|
46
|
+
else:
|
|
47
|
+
# Can't connect to a wildcard without a hostname
|
|
48
|
+
return []
|
|
49
|
+
cmd.append(alias)
|
|
50
|
+
return cmd
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def launch_in_app(host: HostEntry) -> TerminalWidget | None:
|
|
54
|
+
"""Spawn SSH in an in-app terminal widget. Returns the widget."""
|
|
55
|
+
cmd = build_ssh_command(host)
|
|
56
|
+
if not cmd:
|
|
57
|
+
return None
|
|
58
|
+
widget = TerminalWidget()
|
|
59
|
+
widget.start(cmd)
|
|
60
|
+
return widget
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def launch_external(host: HostEntry) -> subprocess.Popen | None:
|
|
64
|
+
"""Spawn SSH in an external terminal emulator.
|
|
65
|
+
|
|
66
|
+
Tries x-terminal-emulator, then common emulators.
|
|
67
|
+
"""
|
|
68
|
+
cmd = build_ssh_command(host)
|
|
69
|
+
if not cmd:
|
|
70
|
+
return None
|
|
71
|
+
|
|
72
|
+
# Find a terminal emulator
|
|
73
|
+
term = _find_terminal_emulator()
|
|
74
|
+
if term is None:
|
|
75
|
+
# Fallback: try x-terminal-emulator
|
|
76
|
+
return None
|
|
77
|
+
|
|
78
|
+
# Build the full command
|
|
79
|
+
if term in ("gnome-terminal", "xfce4-terminal", "mate-terminal", "lxterminal"):
|
|
80
|
+
full_cmd = [term, "--", *cmd]
|
|
81
|
+
elif term == "konsole":
|
|
82
|
+
full_cmd = [term, "-e", *cmd]
|
|
83
|
+
elif term == "xterm":
|
|
84
|
+
full_cmd = [term, "-e", *cmd]
|
|
85
|
+
elif term == "kitty":
|
|
86
|
+
full_cmd = [term, *cmd]
|
|
87
|
+
elif term == "alacritty":
|
|
88
|
+
full_cmd = [term, "-e", *cmd]
|
|
89
|
+
elif term == "foot":
|
|
90
|
+
full_cmd = [term, *cmd]
|
|
91
|
+
elif term == "wezterm":
|
|
92
|
+
full_cmd = [term, "start", "--", *cmd]
|
|
93
|
+
else:
|
|
94
|
+
full_cmd = [term, "-e", *cmd]
|
|
95
|
+
|
|
96
|
+
return subprocess.Popen(full_cmd, start_new_session=True)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _find_terminal_emulator() -> str | None:
|
|
100
|
+
"""Find an available terminal emulator."""
|
|
101
|
+
# Check x-terminal-emulator first (Debian alternative system)
|
|
102
|
+
xte = shutil.which("x-terminal-emulator")
|
|
103
|
+
if xte:
|
|
104
|
+
return "x-terminal-emulator"
|
|
105
|
+
|
|
106
|
+
candidates = [
|
|
107
|
+
"konsole", # KDE
|
|
108
|
+
"gnome-terminal", # GNOME
|
|
109
|
+
"xfce4-terminal", # XFCE
|
|
110
|
+
"mate-terminal", # MATE
|
|
111
|
+
"lxterminal", # LXDE
|
|
112
|
+
"qterminal", # LXQt
|
|
113
|
+
"kitty",
|
|
114
|
+
"alacritty",
|
|
115
|
+
"foot",
|
|
116
|
+
"wezterm",
|
|
117
|
+
"xterm", # fallback
|
|
118
|
+
]
|
|
119
|
+
for t in candidates:
|
|
120
|
+
if shutil.which(t):
|
|
121
|
+
return t
|
|
122
|
+
return None
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def test_connection(host: HostEntry, timeout: float = 5.0) -> ConnectionTestResult:
|
|
126
|
+
"""Test TCP connectivity to a host without opening an SSH session.
|
|
127
|
+
|
|
128
|
+
Does a TCP connect to host:port and measures latency. For wildcard
|
|
129
|
+
hosts with no HostName, returns an error immediately.
|
|
130
|
+
"""
|
|
131
|
+
hostname = host.hostname
|
|
132
|
+
if not hostname:
|
|
133
|
+
if "*" not in host.alias:
|
|
134
|
+
hostname = host.alias
|
|
135
|
+
else:
|
|
136
|
+
return ConnectionTestResult(
|
|
137
|
+
success=False, latency_ms=0,
|
|
138
|
+
error="Wildcard host with no HostName — cannot test.",
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
port = int(host.port) if host.port else 22
|
|
142
|
+
|
|
143
|
+
try:
|
|
144
|
+
start = time.monotonic()
|
|
145
|
+
sock = socket.create_connection((hostname, port), timeout=timeout)
|
|
146
|
+
elapsed = (time.monotonic() - start) * 1000
|
|
147
|
+
sock.close()
|
|
148
|
+
return ConnectionTestResult(success=True, latency_ms=round(elapsed, 1))
|
|
149
|
+
except socket.gaierror as e:
|
|
150
|
+
return ConnectionTestResult(success=False, latency_ms=0, error=f"DNS error: {e}")
|
|
151
|
+
except TimeoutError:
|
|
152
|
+
return ConnectionTestResult(success=False, latency_ms=0, error=f"Timeout after {timeout}s")
|
|
153
|
+
except ConnectionRefusedError:
|
|
154
|
+
return ConnectionTestResult(success=False, latency_ms=0, error="Connection refused")
|
|
155
|
+
except OSError as e:
|
|
156
|
+
return ConnectionTestResult(success=False, latency_ms=0, error=str(e))
|
gssh/data/__init__.py
ADDED
|
File without changes
|
gssh/data/gssh.desktop
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[Desktop Entry]
|
|
2
|
+
Type=Application
|
|
3
|
+
Name=gssh
|
|
4
|
+
GenericName=SSH Connection Manager
|
|
5
|
+
Comment=Manage SSH connections and configuration
|
|
6
|
+
Exec=gssh
|
|
7
|
+
Icon=gssh
|
|
8
|
+
Terminal=false
|
|
9
|
+
Categories=Network;System;TerminalEmulator;
|
|
10
|
+
Keywords=ssh;terminal;remote;connection;config;
|