52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
import subprocess
|
|
import os
|
|
|
|
# 写入密钥文件
|
|
key = "\n".join([
|
|
"-----BEGIN OPENSSH PRIVATE KEY-----",
|
|
"b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW",
|
|
"QyNTUxOQAAACD6GquAk5vBEfbtaJCTSfnQEhjzC8e5GF60hSbPP0BJbAAAAJiQxGNokMRj",
|
|
"aAAAAAtzc2gtZWQyNTUxOQAAACD6GquAk5vBEfbtaJCTSfnQEhjzC8e5GF60hSbPP0BJbA",
|
|
"AAAED2muzuU4BAiCqbg0ayGxgiDvfS/xI1SFvb32oLzTnn8/oaq4CTm8ER9u1okJNJ+dAS",
|
|
"GPMLx7kYXrSFJs8/QElsAAAAFHJ1bm5lci1hZG1pbkB4aWFveGlhAQ==",
|
|
"-----END OPENSSH PRIVATE KEY-----",
|
|
"",
|
|
])
|
|
|
|
key_path = "/tmp/ns_key"
|
|
with open(key_path, "w") as f:
|
|
f.write(key)
|
|
os.chmod(key_path, 0o600)
|
|
|
|
# 验证
|
|
r = subprocess.run(["ssh-keygen", "-y", "-f", key_path], capture_output=True, text=True)
|
|
print("Key valid:", "YES" if r.returncode == 0 else "NO")
|
|
if r.returncode != 0:
|
|
print("Error:", r.stderr.strip())
|
|
|
|
# 测试内网IP
|
|
print("\n=== Test 172.30.18.199 ===")
|
|
r = subprocess.run(
|
|
["ssh", "-i", key_path, "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=10",
|
|
"root@172.30.18.199", "hostname && whoami"],
|
|
capture_output=True, text=True
|
|
)
|
|
print("stdout:", r.stdout.strip())
|
|
if r.stderr.strip():
|
|
print("stderr:", r.stderr.strip())
|
|
print("exit:", r.returncode)
|
|
|
|
# 测试公网IP
|
|
print("\n=== Test 116.62.226.203 ===")
|
|
r = subprocess.run(
|
|
["ssh", "-i", key_path, "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=10",
|
|
"root@116.62.226.203", "hostname"],
|
|
capture_output=True, text=True
|
|
)
|
|
print("stdout:", r.stdout.strip())
|
|
if r.stderr.strip():
|
|
print("stderr:", r.stderr.strip())
|
|
print("exit:", r.returncode)
|
|
|
|
os.unlink(key_path)
|
|
print("\n=== DONE ===") |