开发者

SSH Force Disconnect as an Error

I am working on a script that runs when a person ssh's. What it does is detects if the command is authorized fo开发者_JS百科r that account. In my authorized_keys file I have

command="/path/to/script" key...

In the script I check for commands that are allowed such as uptime. What I want to do is if the command isn't allowed such as mkdir I want the session to disconnect. I am trying to prevent most commands from being run, and not allowing an open connection to interact with the remote server.

Is there something I can call that will force a disconnect of the current connection as an error so I can write to stderr?


kill -HUP 0, maybe? (Send SIGHUP to the process group.) The remote will relay the signal as an error, usually as some form of "connection lost".


I was way over thinking this problem. Here is the solution and a bit of explanation.

When initiating an SSH connection it runs a script automatically. Since that script is running and will be the only thing running through the ssh connection, unless you call exec, then when the script ends so does the connection to the server.

To end the connection prematurely you just need to run the exit command. Here are 3 ways.

Bash

exit

Python

import sys
sys.exit

Ruby

exit

In python and ruby it raises an exception and if uncaught exits. Hope that helps someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜