XTerminal 用久了,服务器列表往往攒了几十台。换到 Motty 或 Termark 时,最怕的不是重新装软件,而是主机、端口、用户、密钥和代理关系要一台台重填。更稳的做法是先转成 OpenSSH config,再让两个工具都从同一份 config 导入。
一、先说结论
不要指望 XTerminal 的本地备份一定带连接列表。Motty 官方文档说明它可导入 OpenSSH config,也可导入 Xterminal 数据目录,但 Xterminal 加密保存的密码和密钥不会被带过去;Termark 官方说明可从 ~/.ssh/config 可直接导入,批量迁移可从 CSV 导入。Motty 文档链接:https://motty.page/en/docs/connections/hosts-and-authentication;Termark 官方介绍链接:https://go.uukk.de/termark。
二、准备或先查
1. 先确认备份里有没有连接
如果 XTerminal 导出的本地备份解密后只有 CPU、内存、磁盘等状态记录,没有主机列表,就不要继续在备份文件上耗时间。连接迁移要从当前能打开的 XTerminal 会话列表入手。
2. 准备一个本地转换文件夹
在电脑上新建文件夹,例如 xterminal-convert,里面只放转换脚本、启动文件和从 XTerminal 复制出的会话文本。会话文本只保存在本机,不要发到聊天、网盘或在线转换网站。
3. 先想清楚密码怎么处理
SSH config 只保存 HostName、User、Port 这类连接信息,不保存密码。密码登录的主机导入后,需要在 Motty 或 Termark 里重新填密码,并保存到它们自己的加密保险库。
三、操作步骤
1. 从 XTerminal 复制会话文本
在 XTerminal 服务器列表里选中要迁移的主机,使用复制/导出会话功能,把文本保存到本地转换文件夹。文本格式通常是一行一台,包含 host=、port=、user=、authType=、title= 等字段。

XTerminal 中选中主机后导出/复制会话(主机信息已脱敏)
2. 本地转成 SSH config
转换脚本只读取 host、port、user、title、authType、sshProxy、groupId,故意不保存、不输出 password。输出的 config 示例只用文档保留地址演示:
Host demo-vps
HostName 203.0.113.10
User root
Port 22如果脚本提示找不到 sessions.txt,先看文件真实扩展名。Windows 隐藏扩展名时,记事本可能保存成 sessions.txt.txt;脚本应同时识别 sessions.txt 和 sessions.txt.txt,或把文件改名后再运行。
转换文件夹只放脚本、启动文件和会话文本(文件名已脱敏)
脚本:convert_sessions.py
把下面代码保存为 convert_sessions.py,放在转换文件夹里。脚本只在本机运行,输出不包含 password。
#!/usr/bin/env python3
"""把 XTerminal 复制出的会话文本转换成 SSH config。
输入文件默认是同目录的 sessions.txt,每行类似:
host=203.0.113.10 port=22 user=root authType=password password=... title="HK"
安全说明:本脚本只在本机运行;输出文件不包含 password 字段。
转换完成后请删除 sessions.txt。
"""
import argparse
import csv
import json
import re
import sys
from pathlib import Path
KV_RE = re.compile(r'(\w+)=("[^"]*"|\S+)')
def parse_line(line: str) -> dict:
out = {}
for key, value in KV_RE.findall(line):
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
out[key] = value
return out
def slugify(name: str, idx: int, used: set) -> str:
s = re.sub(r"[^A-Za-z0-9._-]+", "-", name.strip()).strip("-._").lower()
if not s or s.isdigit():
s = f"host-{idx:03d}"
base = s
n = 2
while s in used:
s = f"{base}-{n}"
n += 1
used.add(s)
return s
def resolve_input(path_arg: str | None) -> Path:
here = Path(__file__).resolve().parent
if path_arg:
p = Path(path_arg)
return p if p.is_absolute() else here / p
for name in ("sessions.txt", "sessions.txt.txt"):
p = here / name
if p.exists():
return p
for p in sorted(here.glob("*.txt")):
try:
if "host=" in p.read_text(encoding="utf-8-sig", errors="ignore")[:2000]:
return p
except OSError:
continue
return here / "sessions.txt"
def main() -> int:
ap = argparse.ArgumentParser(description="XTerminal 会话文本 -> SSH config")
ap.add_argument("input", nargs="?", default=None, help="会话文本文件,默认自动找 sessions.txt / sessions.txt.txt")
ap.add_argument("-o", "--outdir", default="sessions-converted", help="输出目录")
args = ap.parse_args()
here = Path(__file__).resolve().parent
in_path = resolve_input(args.input)
if not in_path.exists():
print(f"找不到输入文件:{in_path.name}")
print("请把 XTerminal 复制出的会话文本保存为 sessions.txt,放在本脚本同一文件夹。")
return 1
hosts = []
for raw in in_path.read_text(encoding="utf-8", errors="ignore").splitlines():
line = raw.strip()
if not line or "host=" not in line:
continue
d = parse_line(line)
host = (d.get("host") or "").strip()
if not host:
continue
try:
port = int(d.get("port") or 22)
except ValueError:
port = 22
hosts.append({
"name": (d.get("title") or host).strip(),
"host": host,
"port": port,
"username": (d.get("user") or "").strip(),
"auth_type": (d.get("authType") or "").strip(),
"ssh_proxy_id": (d.get("sshProxy") or "").strip(),
"group_id": (d.get("groupId") or "").strip(),
# password 字段故意不保存、不输出
})
if not hosts:
print("没有识别出主机。请确认 sessions.txt 里每行都有 host=... port=... user=... title=...")
return 1
outdir = Path(args.outdir)
if not outdir.is_absolute():
outdir = here / outdir
outdir.mkdir(parents=True, exist_ok=True)
used = set()
for i, h in enumerate(hosts, 1):
h["alias"] = slugify(h["name"], i, used)
lines = [
"# 由 XTerminal 会话文本转换,供 Motty / Termark 从 ~/.ssh/config 导入",
"# 注意:SSH config 不保存密码;密码登录主机导入后需在软件里重新填密码。",
"# sshProxy 只有 ID、没有代理定义的主机已在注释里标出,导入后需手动设置代理或跳板。",
"",
]
for h in hosts:
comment = h["name"]
if h["auth_type"]:
comment += f" | 认证: {h['auth_type']}"
if h["ssh_proxy_id"]:
comment += f" | sshProxy待手动设置: {h['ssh_proxy_id']}"
if h["group_id"]:
comment += f" | groupId: {h['group_id']}"
lines.append(f"# {comment}")
lines.append(f"Host {h['alias']}")
lines.append(f" HostName {h['host']}")
if h["username"]:
lines.append(f" User {h['username']}")
lines.append(f" Port {h['port']}")
lines.append("")
config_text = "\n".join(lines)
(outdir / "config").write_text(config_text, encoding="utf-8")
(outdir / "ssh_config.txt").write_text(config_text, encoding="utf-8")
json_hosts = []
for h in hosts:
json_hosts.append({
"name": h["name"],
"alias": h["alias"],
"host": h["host"],
"port": h["port"],
"username": h["username"],
"authType": h["auth_type"],
"sshProxyId": h["ssh_proxy_id"],
"groupId": h["group_id"],
})
(outdir / "config.json").write_text(json.dumps(json_hosts, ensure_ascii=False, indent=2), encoding="utf-8")
with open(outdir / "hosts.csv", "w", newline="", encoding="utf-8-sig") as f:
w = csv.writer(f)
w.writerow(["name", "alias", "host", "port", "username", "auth_type", "ssh_proxy_id", "group_id"])
for h in hosts:
w.writerow([h["name"], h["alias"], h["host"], h["port"], h["username"], h["auth_type"], h["ssh_proxy_id"],
h["group_id"]])
pwd_hosts = [h["name"] for h in hosts if h["auth_type"] == "password"]
key_hosts = [h["name"] for h in hosts if h["auth_type"] == "privateKey"]
proxy_hosts = [h["name"] for h in hosts if h["ssh_proxy_id"]]
checklist = [
f"共识别主机:{len(hosts)} 台",
f"密码登录(导入后需在软件里重填密码):{len(pwd_hosts)} 台",
f"密钥登录(导入后需选择私钥):{len(key_hosts)} 台" + (f":{', '.join(key_hosts)}" if key_hosts else ""),
f"带 sshProxy ID(导入后需手动设置代理/跳板):{len(proxy_hosts)} 台" + (f":{', '.join(proxy_hosts)}" if proxy_hosts else ""),
"",
"输出文件:config(复制到 C:\\Users\\你的用户名\\.ssh\\config)、config.json(主机清单,不含密码)、ssh_config.txt(备用)、hosts.csv(核对用)",
"安全提醒:sessions.txt 含明文密码,转换完成后请删除。",
]
(outdir / "checklist.txt").write_text("\n".join(checklist), encoding="utf-8")
print("\n".join(checklist))
print(f"\n下一步:把 {outdir / 'config'} 复制到 C:\\Users\\你的用户名\\.ssh\\config,再在 Motty 或 Termark 里导入
~/.ssh/config。")
return 0
if __name__ == "__main__":
raise SystemExit(main())启动文件:run_convert_sessions.bat
Windows 用户再保存一个启动文件,和 convert_sessions.py、sessions.txt 放在同一文件夹,双击运行。
@echo off
chcp 65001 >nul
cd /d "%~dp0"
python convert_sessions.py
echo.
echo 转换完成。sessions.txt 含明文密码,确认导入成功后请删除它。
pause
转换成功后会生成 config、config.json 和核对清单(主机名与个人路径已脱敏)
3. 放到 SSH 默认位置
把生成的 config 放到用户目录下的.ssh 文件夹:Windows 是 C:\Users\你的用户名\.ssh\config,macOS/Linux 是 ~/.ssh/config。如果原 config 里已有主机,不要直接覆盖,先备份,再把新主机段追加进去。
4. 导入 Motty
打开 Motty,进入主机页,点击“导入/导出”,选择“导入 SSH Config”。Motty 也可以从 Xterminal 数据目录导入连接:Windows 数据目录通常是 %APPDATA%\XTerminal,免安装版在 xt_data\userData。无论走哪条路,Xterminal 加密保存的密码都不会被导入,连接导入后仍要补凭据。Motty 文档:https://motty.page/en/docs/connections/hosts-and-authentication
5. 导入 Termark
打开 Termark,在资产导入菜单里选择“从 ~/.ssh/config 导入”。如果主机很多,也可以用 CSV 做批量迁移;导入预览时检查名称、地址、端口和用户是否对齐,再绑定已有凭据或新建凭据。Termark 官方介绍:https://go.uukk.de/termark
6. 逐台补凭据并测试
密码登录:在 Motty/Termark 里重填密码,保存到保险库。
密钥登录:选择对应私钥;私钥有口令时,在软件里单独保存口令。
代理/跳板:会话文本里只有
sshProxyID 时,新工具不知道代理地址,必须手动补 HTTP/SOCKS5 代理或跳板机。首次连接:核对主机密钥指纹,不要无脑接受未知指纹。
四、关键设置或判定
五、结果验证
转换成功时,脚本会报识别主机台数,并生成 config、config.json、hosts.csv 和检查清单。config.json 只作主机清单核对,不含密码;真正导入 Motty 和 Termark 的是 SSH config。
导入后至少抽查三类主机:一台密码登录、一台密钥登录、一台带代理或跳板的主机。三类都能连上,再删除本地会话文本和临时输出。
六、注意事项
不要把带 password= 的会话文本发到聊天、论坛或在线工具。
不要把明文密码写进 config.json、CSV 或 SSH config。
转换完成后删除会话文本;如果曾经外发过重要服务器密码,尽快轮换。
Motty 从 Xterminal 数据目录导入时,只带连接,不带加密保存的密码和密钥。
Termark 的凭据应保存在 Termark 本地加密存储里,不要用明文 JSON 长期保存。
来源
Motty 官方文档:Hosts and authentication — https://motty.page/en/docs/connections/hosts-and-authentication
Motty GitHub — https://github.com/t8y2/motty
Termark 官方介绍 — https://www.termark.app/zh/blog/termark-ssh-terminal-workbench
Termark 官网 — https://www.termark.app/zh
欢迎关注 Seedloc,后续继续更新 SSH 工具迁移和服务器运维实操。
评论