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

自作多情 提交于 2019-12-13 16:22:18

问题


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

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

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


回答1:


It is indeed required to be a superuser to run wall with an input file, man says:

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
>>> 



回答2:


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

echo hello | wall


来源:https://stackoverflow.com/questions/25282661/how-do-i-broadcast-messages-to-all-bash-terminal-in-python-using-wall-command-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!