如何使用带有 stdin 的 wall 命令向 python 中的所有 bash 终端广播消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25282661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:07:24  来源:igfitidea点击:

How do I broadcast messages to all bash terminal in python using wall command with stdin?

pythonlinuxbashcommandexecution

提问by SoGeeky

I wanted to broadcast message to all the bash terminal on my raspbian.

我想向我的 raspbian 上的所有 bash 终端广播消息。

I understand that there's wall command to perform the step and I could use os.system python module to execute the command.

我知道有 wall 命令来执行该步骤,我可以使用 os.system python 模块来执行该命令。

However, running the command "wall text.txt" requires sudo privilege. Is there any way to use wall command with stdin from python?

但是,运行命令“wall text.txt”需要 sudo 权限。有什么办法可以在 python 的 stdin 中使用 wall 命令吗?

回答by piokuc

It is indeed required to be a superuser to run wallwith an input file, mansays:

确实需要成为超级用户才能wall使用输入文件运行,man说:

NAME
     wall - write a message to users

SYNOPSIS
     wall [file]

DESCRIPTION
     Wall displays the contents of file or, by default, its standard input, on the terminals of all currently logged in users.

     Only the super-user can write on the terminals of users who have chosen to deny messages or are using a program which automatically denies messages.

     Reading from a file is refused when the invoker is not superuser and the program is suid or sgid.

But you can do this:

但是你可以这样做:

$ echo hello hello >text.txt
$ python                    
Python 2.7.1 (r271:86832, Mar 18 2011, 09:09:48) 
[GCC 4.5.0 20100604 [gcc-4_5-branch revision 160292]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system('cat text.txt | wall')

Broadcast Message from mak@vader                                             
        (/dev/pts/14) at 10:31 ...                                             

hello hello                                                                    


Broadcast Message from mak@vader                                            
        (/dev/pts/14) at 10:31 ...                                             

hello hello                                                                    

0
>>> 

回答by ayhan

you can use "echo" and pipe "|" it to wall.it's nit necessary to echo to fill first.

你可以使用“echo”和管道“|” 它到墙。它没有必要先回声来填充。

echo hello | wall