def public_port_exists(rline, authorized_keys_file, restrictions, public_key, port):
port = str(port)
new_restrictions = []
replaced = False
for restriction in restrictions.split("\n"):
if public_key in restriction:
if ":" + port + '"' not in restriction:
new_opens = 'no-pty,permitopen="localhost:{0}",permitopen="127.0.0.1:{0}",'
restriction = restriction.replace("no-pty,", new_opens.format(port))
replaced = True
else:
print("public_key and port already exists in", authorized_keys_file)
return
new_restrictions.append(restriction)
print("Adding key+port rule to file")
if replaced:
result = "\n".join(new_restrictions)
else:
result = rline
with open(authorized_keys_file, "w") as f:
f.write(result + "\n")
评论列表
文章目录