特殊:Badtitle/NS100:BroadcastAlertWithSound:修订间差异
小无编辑摘要 |
小无编辑摘要 |
||
第3行: | 第3行: | ||
This page describes how to send pop-up messages to NX and X-windows sessions. With the NX sessions, these messages can be accompanied by sound. This was developed using Ubuntu 7.04 (Feisty). | This page describes how to send pop-up messages to NX and X-windows sessions. With the NX sessions, these messages can be accompanied by sound. This was developed using Ubuntu 7.04 (Feisty). | ||
=== Introduction === | === Introduction === | ||
The Python script below was developed to send pop-up messages with sound to NX and X sessions running on a Ubuntu server. This was developed on a home network consisting of a Ubuntu 7.04 server running FreeNX, and Windows PCs and Ubuntu/Xubuntu PCs running the nomachine NX client or X-Windows. The NX clients are configured to support sound, but the X-windows clients receive only a silent pop-up message. | The Python script below was developed to send pop-up messages with sound to NX and X sessions running on a Ubuntu server. This was developed on a home network consisting of a Ubuntu 7.04 server running [[UbuntuHelp:FreeNX|FreeNX]], and Windows PCs and Ubuntu/Xubuntu PCs running the nomachine NX client or X-Windows. The NX clients are configured to support sound, but the X-windows clients receive only a silent pop-up message. | ||
=== Dependancies === | === Dependancies === | ||
The script uses gxmessage to send the pop-up messages, and esdplay to transmit a .wav sound file. Gxmessage is a requirement to run the script. Esound needs to be installed if sound functionality is required. | The script uses gxmessage to send the pop-up messages, and esdplay to transmit a .wav sound file. Gxmessage is a requirement to run the script. Esound needs to be installed if sound functionality is required. | ||
第11行: | 第11行: | ||
=== Setting up NX Sessions for Sound === | === Setting up NX Sessions for Sound === | ||
This section is not required to get the script running without sound. If you want to get the script running quickly, you can skip this section, and come back to it later. | This section is not required to get the script running without sound. If you want to get the script running quickly, you can skip this section, and come back to it later. | ||
Follow the [[UbuntuHelp:FreeNX]] instructions to install FreeNX on a Ubuntu server. | Follow the [[UbuntuHelp:FreeNX]] instructions to install [[UbuntuHelp:FreeNX|FreeNX]] on a Ubuntu server. | ||
Ensure that esound is installed (see above), and that the esd service is running like so .. | Ensure that esound is installed (see above), and that the esd service is running like so .. | ||
<pre><nowiki> | <pre><nowiki> |
2007年12月4日 (二) 10:17的版本
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/BroadcastAlertWithSound }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/af | • {{#if: UbuntuHelp:BroadcastAlertWithSound|Afrikaans| [[::BroadcastAlertWithSound/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/ar | • {{#if: UbuntuHelp:BroadcastAlertWithSound|العربية| [[::BroadcastAlertWithSound/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/az | • {{#if: UbuntuHelp:BroadcastAlertWithSound|azərbaycanca| [[::BroadcastAlertWithSound/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/bcc | • {{#if: UbuntuHelp:BroadcastAlertWithSound|جهلسری بلوچی| [[::BroadcastAlertWithSound/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/bg | • {{#if: UbuntuHelp:BroadcastAlertWithSound|български| [[::BroadcastAlertWithSound/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/br | • {{#if: UbuntuHelp:BroadcastAlertWithSound|brezhoneg| [[::BroadcastAlertWithSound/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/ca | • {{#if: UbuntuHelp:BroadcastAlertWithSound|català| [[::BroadcastAlertWithSound/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/cs | • {{#if: UbuntuHelp:BroadcastAlertWithSound|čeština| [[::BroadcastAlertWithSound/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/de | • {{#if: UbuntuHelp:BroadcastAlertWithSound|Deutsch| [[::BroadcastAlertWithSound/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/el | • {{#if: UbuntuHelp:BroadcastAlertWithSound|Ελληνικά| [[::BroadcastAlertWithSound/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/es | • {{#if: UbuntuHelp:BroadcastAlertWithSound|español| [[::BroadcastAlertWithSound/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/fa | • {{#if: UbuntuHelp:BroadcastAlertWithSound|فارسی| [[::BroadcastAlertWithSound/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/fi | • {{#if: UbuntuHelp:BroadcastAlertWithSound|suomi| [[::BroadcastAlertWithSound/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/fr | • {{#if: UbuntuHelp:BroadcastAlertWithSound|français| [[::BroadcastAlertWithSound/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/gu | • {{#if: UbuntuHelp:BroadcastAlertWithSound|ગુજરાતી| [[::BroadcastAlertWithSound/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/he | • {{#if: UbuntuHelp:BroadcastAlertWithSound|עברית| [[::BroadcastAlertWithSound/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/hu | • {{#if: UbuntuHelp:BroadcastAlertWithSound|magyar| [[::BroadcastAlertWithSound/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/id | • {{#if: UbuntuHelp:BroadcastAlertWithSound|Bahasa Indonesia| [[::BroadcastAlertWithSound/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/it | • {{#if: UbuntuHelp:BroadcastAlertWithSound|italiano| [[::BroadcastAlertWithSound/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/ja | • {{#if: UbuntuHelp:BroadcastAlertWithSound|日本語| [[::BroadcastAlertWithSound/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/ko | • {{#if: UbuntuHelp:BroadcastAlertWithSound|한국어| [[::BroadcastAlertWithSound/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/ksh | • {{#if: UbuntuHelp:BroadcastAlertWithSound|Ripoarisch| [[::BroadcastAlertWithSound/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/mr | • {{#if: UbuntuHelp:BroadcastAlertWithSound|मराठी| [[::BroadcastAlertWithSound/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/ms | • {{#if: UbuntuHelp:BroadcastAlertWithSound|Bahasa Melayu| [[::BroadcastAlertWithSound/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/nl | • {{#if: UbuntuHelp:BroadcastAlertWithSound|Nederlands| [[::BroadcastAlertWithSound/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/no | • {{#if: UbuntuHelp:BroadcastAlertWithSound|norsk| [[::BroadcastAlertWithSound/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/oc | • {{#if: UbuntuHelp:BroadcastAlertWithSound|occitan| [[::BroadcastAlertWithSound/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/pl | • {{#if: UbuntuHelp:BroadcastAlertWithSound|polski| [[::BroadcastAlertWithSound/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/pt | • {{#if: UbuntuHelp:BroadcastAlertWithSound|português| [[::BroadcastAlertWithSound/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/ro | • {{#if: UbuntuHelp:BroadcastAlertWithSound|română| [[::BroadcastAlertWithSound/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/ru | • {{#if: UbuntuHelp:BroadcastAlertWithSound|русский| [[::BroadcastAlertWithSound/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/si | • {{#if: UbuntuHelp:BroadcastAlertWithSound|සිංහල| [[::BroadcastAlertWithSound/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/sq | • {{#if: UbuntuHelp:BroadcastAlertWithSound|shqip| [[::BroadcastAlertWithSound/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/sr | • {{#if: UbuntuHelp:BroadcastAlertWithSound|српски / srpski| [[::BroadcastAlertWithSound/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/sv | • {{#if: UbuntuHelp:BroadcastAlertWithSound|svenska| [[::BroadcastAlertWithSound/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/th | • {{#if: UbuntuHelp:BroadcastAlertWithSound|ไทย| [[::BroadcastAlertWithSound/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/tr | • {{#if: UbuntuHelp:BroadcastAlertWithSound|Türkçe| [[::BroadcastAlertWithSound/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/vi | • {{#if: UbuntuHelp:BroadcastAlertWithSound|Tiếng Việt| [[::BroadcastAlertWithSound/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/yue | • {{#if: UbuntuHelp:BroadcastAlertWithSound|粵語| [[::BroadcastAlertWithSound/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/zh | • {{#if: UbuntuHelp:BroadcastAlertWithSound|中文| [[::BroadcastAlertWithSound/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/zh-hans | • {{#if: UbuntuHelp:BroadcastAlertWithSound|中文(简体)| [[::BroadcastAlertWithSound/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BroadcastAlertWithSound | UbuntuHelp:BroadcastAlertWithSound | {{#if: | :}}BroadcastAlertWithSound}}/zh-hant | • {{#if: UbuntuHelp:BroadcastAlertWithSound|中文(繁體)| [[::BroadcastAlertWithSound/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:BroadcastAlertWithSound|:BroadcastAlertWithSound|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :BroadcastAlertWithSound/zh | | {{#ifexist: BroadcastAlertWithSound/zh | | {{#ifeq: {{#titleparts:BroadcastAlertWithSound|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:BroadcastAlertWithSound|1|-1|}} | zh | | }}
This page describes how to send pop-up messages to NX and X-windows sessions. With the NX sessions, these messages can be accompanied by sound. This was developed using Ubuntu 7.04 (Feisty).
Introduction
The Python script below was developed to send pop-up messages with sound to NX and X sessions running on a Ubuntu server. This was developed on a home network consisting of a Ubuntu 7.04 server running FreeNX, and Windows PCs and Ubuntu/Xubuntu PCs running the nomachine NX client or X-Windows. The NX clients are configured to support sound, but the X-windows clients receive only a silent pop-up message.
Dependancies
The script uses gxmessage to send the pop-up messages, and esdplay to transmit a .wav sound file. Gxmessage is a requirement to run the script. Esound needs to be installed if sound functionality is required.
sudo aptitude install gmessage sudo aptitude install esound
Setting up NX Sessions for Sound
This section is not required to get the script running without sound. If you want to get the script running quickly, you can skip this section, and come back to it later. Follow the UbuntuHelp:FreeNX instructions to install FreeNX on a Ubuntu server. Ensure that esound is installed (see above), and that the esd service is running like so ..
ps -e | grep 'esd' 17249 ? 00:00:00 esd
Download the 'NX Client DEB for Linux' or 'NX client for Windows' from http://www.nomachine.com/download.php and install on your Ubuntu and Windows PCs. In the configuration of the NX client, click on the 'Services' tab, and tick 'Enable Multimedia Support' - this enables sound for the NX client. Each user needs to log onto your Ubuntu server using their NX client, and start System/Preferences/Sound from the panel. They need to choose 'ESD - Enlightened Sound Daemon' for the various sound options. They can test the sound while making these selections.
Creating and Running the Script
Copy the script code below into a file called bcast.py. Ensure that this script is executable by using the chmod command.
chmod 755 bcast.py
The script needs to be run using the sudo prefix if you are not logged in as root.
sudo ./bcast --message "Message" --title "Title" --timeout 0 --sound "wavefile.wav"
The script can be called from within cron jobs that are running using the root account. This is the text that needs to be copied into bcast.py ..
#!/usr/bin/python # Author: David Collins, 2007 # Note: Changing values in os.environ does not affect calling process's environment """ Broadcast a pop-up message to X and NX sessions. A .wav can be specified to send sound with the message, but this will only be received by NX sessions. Usage: sudo ./bcast.py --message "message" [--title "title"] [--timeout secs] [--sound "wavefile.wav"] """ import os, subprocess, sys, getopt, string # ------------------------------------------------------------- def usage(msg=None): sys.stdout = sys.stderr if msg != "": print msg print <u>doc</u> sys.exit(2) # ------------------------------------------------------------- def processargs(): global title, message, wavefile, timeout # Default values .. title = "Alert" message = "" wavefile = "" timeout = 0 optionlist = [[UbuntuHelp:message=","title=","timeout=","sound=]] try: opts, args = getopt.getopt(sys.argv[1:], "", optionlist) if len(opts) < 1: raise getopt.error, "" except getopt.error, msg: usage(msg) try: for opt, val in opts: if val[0:2] == "--": # 2 options with no value between raise Exception, "Option '" + opt + "' has no associated value." # print opt, "=", val if opt == "--message": message = val elif opt == "--title": title = val elif opt == "--sound": wavefile = val elif opt == "--timeout": try: timeout = string.atoi(val) except: raise Exception, "Timeout value '" + val + "' is not an integer." except Exception, msg: usage(msg) for arg in args: print "***" + arg # ------------------------------------------------------------- def checkdependancies(): # Make sure that gxmessage is available .. # PIPE was not defined - so used -1 p = subprocess.Popen(['which gxmessage'], stdout=-1, shell=True) result = p.wait() if result != 0: print "Gxmessage needs to be installed." print "On ubuntu, run 'sudo apt-get install gxmessage'." exit(-2) # Make sure that esdplay is available .. p = subprocess.Popen(['which esdplay'], stdout=-1, shell=True) result = p.wait() if result != 0: print "esdplay is not installed - therefore sound will not function." print "On ubuntu, run 'sudo apt-get install esound' to enable sound." # ------------------------------------------------------------- def sendalerts(): # To hold list of displays - to avoid duplication. displaylist = [] # Get PID list in /proc .. pidlist = os.listdir("/proc") for pid in pidlist: # All number directories are PIDs .. if pid.isdigit(): try: envfile = "/proc/" + str(pid) + "/environ" env = open(envfile) environ = env.readline() except: pass #readenv=False else: # print envfile envlist = environ.split("\0") # print len(envlist) espeaker = "" display = "" user = "" shlvl = "" # Get $USER, $DISPLAY, $HOME, $ESPEAKER .. for en in envlist: if en[0:5] == "USER=": user = en[5:] # Skip, if this is a system process .. if (user == "root") or (user.isdigit()): break if en[0:8] == "DISPLAY=": display = en[8:] os.environ['DISPLAY'] = display elif en[0:5] == "HOME=": home = en[5:] os.environ['HOME'] = home elif en[0:9] == "ESPEAKER=": espeaker = en[9:] os.environ['ESPEAKER'] = espeaker subprocess.Popen(['export ESPEAKER'], shell=True) elif en[0:6] == "SHLVL=": shlvl = en[6:] try: # Some filtering .. # eg. exclude $DISPLAY=nx/nx,options=/home/sinead/.nx/C-Buddy-1001 .. if (display.find(",") != -1): continue # eg. exclude $DISPLAY=:1.0 if (display[0] == ":"): continue # Add .0 - eg. unix:1000->unix:1000.0 (else get duplication) if (display.find(".") == -1): display=display + ".0" except: continue # Send Popup Message to User's Screen using gxmessage .. # Use shlvl to identify real users (for example, excludes gdm) # Shell=True is important - else doesn't work .. if (user != "") and (display != "") and (shlvl != ""): try: # Only send to the display if it hasn't already been done. result=displaylist.index(display) except: if (espeaker != "") and (wavefile != ""): print "%s - Display='%s' Speaker='%s'" % (user, display, espeaker) else: print "%s - Display='%s'" % (user, display) subprocess.Popen(['sudo -u ' + user + ' gxmessage -center ' \ + '-timeout ' + str(timeout) + ' -display ' + display \ + ' -title "' + title + '" "' + message + '"'], shell=True) # If user's session supports ESD sound - eg. an NX session and # a wave file is specified, play a sound/music, also .. if (espeaker != "") and (wavefile != ""): subprocess.Popen(['sudo -u ' + user + ' esdplay ' + wavefile], shell=True) # Register this display as alerted. displaylist.append(display) # ------------------------------------------------------------- # Do it ! ... processargs() checkdependancies() sendalerts()