Trying to send system notifications via SSH to computers on LAN.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Trying to send system notifications via SSH to computers on LAN. [SOLVED]
I have two computers on my LAN, one running Ubuntu Jaunty. I want to be able to send notifications to the Ubuntu computer using the notify-send command, so that messages I write appear on screen in the little bubbles.
The basic structure is:
Code:
notify-send "MESSAGE HERE."
with the message you intend to send wrapped in double-quotes.
I can SSH into any computer with X forwarding on, as such
Code:
ssh -X user@host
and I can send system notifications on the remote host by setting the display in front of the command, like so:
Code:
DISPLAY=:0 notify-send "MESSAGE HERE."
Now, on both computers, I created a bash alias:
Code:
alias chat='DISPLAY=:0 notify-send'
and when I log in, all I have to type at the remote prompt is
Code:
chat "MESSAGE HERE."
and everything works as advertised.
Rather than having a two-step process (logging in via SSH and then sending the message), I'm trying to write a one-liner script that will do the job all in one shot. If I'm at my local terminal prompt, I can issue the command
with everything from DISPLAY to the end of the double-quoted text wrapped in single quotes.
The problem I'm having is setting the double-quoted text as a variable so that I can change it as necessary and still use the same structure as the alias that I showed above. The basic idea here, in case I haven't elaborated enough, is to type
Code:
chat "MESSAGE HERE."
at my local prompt and have a system notification bubble appear on a remote screen.
Can anyone point me in the right direction?
Last edited by uncertain; 08-15-2009 at 02:47 PM.
Reason: solved
$ ./chat.sh "This is a test."
No summary specified.
I keep getting the same error. It's as if notify-send isn't being passed the input from the variable. Probably because of the double-quotes nested inside the single quotes.
Trying to send a blank notify-send locally produces the same error
The $message variable needs to be evaluated before the command is sent, and not literally as '$message'. Also the message part of the notify-send command needs to be a single argument.
examples:
message="this is only a test"
ssh qosmio "DISPLAY=:0 notify-send -i gtk-icon-warning \"$message\""
The $message variable needs to be evaluated before the command is sent, and not literally as '$message'. Also the message part of the notify-send commands needs to be one argument.
examples:
message="this is only a test"
ssh qosmio "DISPLAY=:0 notify-send -i gtk-icon-warning \"$message\""
I see what you're getting at, but the message variable has to itself be a variable of user-inputted text, wrapped in quotes. I don't see how setting a variable and calling it with another variable will make it work.
Also, the message changes every time I want to send one. It might be as simple as "Please reboot," or, "Internet will be offline 15 mins formaintenance." Having to open, edit, save, and then execute a script is just as bad (if not worse) than having to log in remotely, send a message, and then sign back out.
I guess I could write a second script (or work it into this one) using echo & sed that both changes the message and executes the chat script.. but that seems a convoluted way of doing it.
Here the user doesn't need to surround the text in quotes unless you change the program to use more arguments.
Aahhhhh.... I see now. I guess I was just cross-eyed after fighting it for a few hours. For the record, $@ has to be used - $1 still returns errors when too many words get used in the message.
Thanks for the help... I can't believe how sometimes the simplest of things cause the most problems.
Also, the message changes every time I want to send one. It might be as simple as "Please reboot," or, "Internet will be offline 15 mins formaintenance."
Doesn't this only work for sending a message to yourself? If you want to send a message to all users running X wouldn't you have to cycle through all the active DISPLAY's? And I thought you can't write to another users screen unless you authenticate with xauth.
Have you been able to write another users screen besides yourself with this method?
Doesn't this only work for sending a message to yourself? If you want to send a message to all users running X wouldn't you have to cycle through all the active DISPLAY's? And I thought you can't write to another users screen unless you authenticate with xauth.
Have you been able to write another users screen besides yourself with this method?
Maybe the command your looking for is:
Code:
echo "hello" | wall
Here is the final version:
Code:
#!/bin/bash
#
#Send messages through SSH to remote hosts' notify-osd
#
message="$@"
ssh -X user@host "DISPLAY=:0 notify-send \"$message\""
exit
Works as advertised. For some reason it will not work if placed in /usr/bin.. it just freezes the terminal I'm in. But if I put it in a folder and make an alias that points to it, it does work.
To answer your question, micxz, you can use SSH to execute single commands on remote hosts. The beauty of a simple script like this is that you an use aliases inside of it. For example, say I have the following aliases:
alias ssh1='ssh -X user@host1'
alias ssh2='ssh -X user@host2'
and so on...
Now I can create separate chat scripts that use those aliases to send pop-up notifications to the users' screens while they're in their GUI. Script one (chat1) is the same as above, only instead of "ssh -X user@host1", it actually contains the aliased command I created, "ssh1". Script two (chat2) contains the alias "ssh2", and so on. An Nth script can be created that contains them all and sends a LAN-wide system notification inside all the systems running X and having the notify-send command and either libnotify or notify-osd or whatever notification system installed.
"bob" will see the nifty little popup message as planned because you are "bob".
If you try to send this same message to user "joe" on "host1" "joe" may have a different $DISPLAY set and does have different xauth cookies set. So "joe" won't see the message. The reason the first example works is because "bob" has the same xauth cookies set as user "bob" so of course he can write to his own screen.
I think the only way to be able to display this popup to all users on all screens is to have a script find out who's logged in and get the env for all users running X or logged via ssh -X and get they're xauth cookie and DISPLAY from env and send a message to each using this info. I know sound like allot but I think wall does this and will notify users even if they are not using X.
Oh, ok, yeah, I see what you're saying now. I suppose that's the reason it does work - because bob is logged in and I ssh in as bob. I haven't tried it as a different user. I would imagine it would work as root no matter what, but having RSA & DSA SSH keys laying around for a root account probably isn't the smartest thing a person could do. I think a limited user group and/or account could be created with specific permissions for accessing notify-osd or zenity or wall or whatever on any display, regardless of user. (Probably the group already exists in Ubuntu - admin? plugdev? Beats me...)
I don't know about all that, man. All I do know is this one does what I need and it works the way I want it to.
My wife is the main recipient of the usefulness of this. She's running Ubuntu 9.04. If she has a terminal open for anything, chances are I'm looking over her shoulder giving her tips on what to do. She never shuts down X or uses the TTYs. I'll be using this to send her messages like "I just updated XXXXX, please restart the program," or, "I just installed your new kernel. Please reboot," and such things.
As far as anything else, if you'd like, I'd help you put together something (as much as I could, anyway..). Just start a new thread with an idea and basis and we'll go from there.
I would imagine it would work as root no matter what,
Actually no as root would still need the correct DISPLAY
Quote:
but having RSA & DSA SSH keys laying around for a root account probably isn't the smartest thing a person could do.
As long as the permissions are tight this should be safe.
Quote:
I think a limited user group and/or account could be created with specific permissions for accessing notify-osd or zenity or wall or whatever on any display, regardless of user. (Probably the group already exists in Ubuntu - admin? plugdev? Beats me...)
I don't think this is the way it works. When you create an X session and want other user to be able to write to it they need to authenticate first regardless of user/group.
Quote:
I don't know about all that, man. All I do know is this one does what I need and it works the way I want it to.
Then your good. I was just taking it to the next level of discussion.
Quote:
As far as anything else, if you'd like, I'd help you put together something (as much as I could, anyway..). Just start a new thread with an idea and basis and we'll go from there.
micxz: Yes you are right. It assumes DISPLAY :0 is being used by the user, and you need ssh <user>@<host> if you are sending it to someone else. My main point was that you need to be careful how you escape the variable with the message. Otherwise there are too many arguments for ssh or the notify command. The OP was having a problem sending a command in one-shot, using ssh with a command argument. In other words, not having to log in and log out. This is tricky because the variable needs to be evaluated before ssh sends it, but you can't have the wrong number of arguments to ssh. When run in the target, the double quotes are needed so that the shell running on the remote machine doesn't break up the message.
Using two variables (and arguments to the send script) $summary and $body might look better.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.