Update server.py
Browse files
server.py
CHANGED
|
@@ -124,10 +124,39 @@ def ssh():
|
|
| 124 |
# ssh()
|
| 125 |
clear_output()
|
| 126 |
|
| 127 |
-
def
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
def sync():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
commands = [
|
| 132 |
"rm -rf /rsync.py /sync.sh",
|
| 133 |
"wget -O /rsync.py https://huggingface.co/datasets/Qilan2/st-server/resolve/main/rsync.py",
|
|
|
|
| 124 |
# ssh()
|
| 125 |
clear_output()
|
| 126 |
|
| 127 |
+
def generate_ssh_key():
|
| 128 |
+
# 生成 SSH 密钥对,并捕获输出和错误信息
|
| 129 |
+
os.system("rm -f ~/.ssh/id_rsa ~/.ssh/id_rsa.pub")
|
| 130 |
+
keygen_command = "ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa -N ''"
|
| 131 |
+
try:
|
| 132 |
+
result = subprocess.run(keygen_command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 133 |
+
print("SSH key pair generated.")
|
| 134 |
+
print(result.stdout.decode()) # 打印标准输出信息(如成功消息)
|
| 135 |
+
except subprocess.CalledProcessError as e:
|
| 136 |
+
print(f"Failed to generate SSH key: {e.stderr.decode() if e.stderr else e}")
|
| 137 |
+
|
| 138 |
+
def copy_public_key_to_remote(host, port, user, password):
|
| 139 |
+
# 使用 pexpect 自动输入密码来复制公钥
|
| 140 |
+
try:
|
| 141 |
+
command = f"ssh-copy-id -p {port} {user}@{host}"
|
| 142 |
+
child = pexpect.spawn(command)
|
| 143 |
+
|
| 144 |
+
# 等待密码提示并发送密码
|
| 145 |
+
child.expect("password:")
|
| 146 |
+
child.sendline(password)
|
| 147 |
+
|
| 148 |
+
# 等待命令完成并输出结果
|
| 149 |
+
child.expect(pexpect.EOF)
|
| 150 |
+
|
| 151 |
+
print(child.before.decode()) # 打印输出结果
|
| 152 |
+
|
| 153 |
+
except Exception as e:
|
| 154 |
+
print(f"An error occurred: {e}")
|
| 155 |
def sync():
|
| 156 |
+
# 生成 SSH 密钥对
|
| 157 |
+
generate_ssh_key()
|
| 158 |
+
# 复制公钥到远程服务器
|
| 159 |
+
copy_public_key_to_remote(frp_url, ssh_port, "root", "qilan")
|
| 160 |
commands = [
|
| 161 |
"rm -rf /rsync.py /sync.sh",
|
| 162 |
"wget -O /rsync.py https://huggingface.co/datasets/Qilan2/st-server/resolve/main/rsync.py",
|