如何检查是否通过 ssh 远程调用 python 脚本 [英] how to check if python script is being called remotely via ssh

查看:33
本文介绍了如何检查是否通过 ssh 远程调用 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在编写一个命令行实用程序,但遇到了一个我很好奇的问题.

So I'm writing a command line utility and I've run into a problem I'm curious about.

该实用程序可以使用文件参数调用,也可以从 sys.stdin 中读取.最初我使用 sys.stdin.isatty() 来确定数据是否通过管道输入,但是,我发现如果我通过 ssh 服务器实用程序 远程调用该实用程序, sys.stdin.isatty() 将返回 false,尽管事实上没有实际数据被输送进来.

The utility can be called with file arguments or can read from sys.stdin. Originally I was using sys.stdin.isatty() to figure out if data is being piped in, however, I found that if I call the utility remotely via ssh server utility, sys.stdin.isatty() will return false, despite the fact that there is no actual data being piped in.

作为一种解决方法,我使用 - 作为文件参数来强制从 stdin 读取(例如:echo "data_here" | Utility -f -),但我很想知道是否有一种可靠的方法可以区分从进程获取数据的管道和仅因为调用是通过 ssh 而打开的管道.

As a workaround I'm using - as the file argument to force reading from stdin (eg: echo "data_here" | utility -f -), but I'm curious to know if there's a reliable way to tell the difference between a pipe getting data from a process and a pipe that's only open because the call is via ssh.

系统编程不是我的强项,所以我很感激你们能提供的任何帮助.

Systems programming is not my forte, so I'm grateful for any help I can get from you guys.

推荐答案

您可以通过检查您的环境来判断您是否通过 SSH 被调用.如果您通过 SSH 连接被调用,环境变量 SSH_CONNECTIONSSH_CLIENT 将被设置.您可以测试它们是否已设置,例如:

You can tell if you're being invoked via SSH by checking your environment. If you're being invoked via an SSH connection, the environment variables SSH_CONNECTION and SSH_CLIENT will be set. You can test if they are set with, say:

if "SSH_CONNECTION" in os.environ:
    # do something

另一种选择,如果您只想坚持使用 sys.stdin.isatty() 的原始方法,那就是为 SSH 连接分配一个伪 tty.通常,如果您只是通过 SSH 进行交互式会话,SSH 会默认执行此操作,但如果您提供命令,则不会.但是,您可以在提供命令时通过传递 -t 标志来强制它这样做:

Another option, if you wanted to just stick with your original approach of sys.stdin.isatty(), would be to to allocate a pseudo-tty for the SSH connection. Normally SSH does this by default if you just SSH in for an interactive session, but not if you supply a command. However, you can force it to do so when supplying a command by passing the -t flag:

ssh -t server utility

但是,我会告诫你不要做这些.如您所见,尝试根据是否是 TTY 来检测是否应该接受来自 stdin 的输入可能会导致一些令人惊讶的行为.如果用户想要一种在调试某些内容时以交互方式向您的程序提供输入的方式,这也可能会令用户感到沮丧.

However, I would caution you against doing either of these. As you can see, trying to detect whether you should accept input from stdin based on whether it's a TTY can cause some surprising behavior. It could also cause frustration from users if they wanted a way to interactively provide input to your program when debugging something.

添加显式 - 参数的方法使您获得的行为更加明确且不那么令人惊讶.一些实用程序也只是使用缺少任何文件参数来表示从标准输入读取,因此这也是一个不那么令人惊讶的替代方案.

The approach of adding an explicit - argument makes it a lot more explicit and less surprising which behavior you get. Some utilities also just use the lack of any file arguments to mean to read from stdin, so that would also be a less-surprising alternative.

这篇关于如何检查是否通过 ssh 远程调用 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆