LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-14-2009, 07:27 PM   #1
uncertain
Member
 
Registered: Oct 2008
Location: Katowice, Poland
Distribution: Ubuntu, Backtrack, FC10
Posts: 40

Rep: Reputation: 15
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
Code:
ssh -X user@host 'DISPLAY=:0 notify-send "MESSAGE HERE"'
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
 
Old 08-15-2009, 03:23 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,159
Blog Entries: 1

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
A bash script?
Code:
#!/bin/sh

/usr/bin/ssh -X user@host 'DISPLAY=:0 /path/to/notify-send "$1"'
And run it like ./script.sh "MESSAGE HERE."
 
Old 08-15-2009, 03:33 AM   #3
uncertain
Member
 
Registered: Oct 2008
Location: Katowice, Poland
Distribution: Ubuntu, Backtrack, FC10
Posts: 40

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by bathory View Post
A bash script?
Code:
#!/bin/sh

/usr/bin/ssh -X user@host 'DISPLAY=:0 /path/to/notify-send "$1"'
And run it like ./script.sh "MESSAGE HERE."
Code:
$ ./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
Code:
$ notify-send ""
No summary specified.
 
Old 08-15-2009, 04:20 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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\""

also

ssh qosmio 'DISPLAY=:0 notify-send -i gtk-icon-warning ''"'$message'"'

The single & double quotes may be hard to make out:
...warning sq sq dq sq $message sq dq sq

Last edited by jschiwal; 08-15-2009 at 04:26 AM.
 
Old 08-15-2009, 04:59 AM   #5
uncertain
Member
 
Registered: Oct 2008
Location: Katowice, Poland
Distribution: Ubuntu, Backtrack, FC10
Posts: 40

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
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\""

also

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.
 
Old 08-15-2009, 07:32 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I just used $message for my testing. You could use $1 instead.
Code:
#!/bin/sh
message="$@"
ssh qosmio "DISPLAY=:0 notify-send -i gtk-icon-warning \"$message\""
Here the user doesn't need to surround the text in quotes unless you change the program to use more arguments.
 
Old 08-15-2009, 08:08 AM   #7
uncertain
Member
 
Registered: Oct 2008
Location: Katowice, Poland
Distribution: Ubuntu, Backtrack, FC10
Posts: 40

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
I just used $message for my testing. You could use $1 instead.
Code:
#!/bin/sh
message="$@"
ssh qosmio "DISPLAY=:0 notify-send -i gtk-icon-warning \"$message\""
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.
 
Old 08-15-2009, 01:46 PM   #8
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Quote:
Originally Posted by uncertain View Post
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?

Maybe the command your looking for is:
Code:
echo "hello" | wall
 
Old 08-15-2009, 02:04 PM   #9
uncertain
Member
 
Registered: Oct 2008
Location: Katowice, Poland
Distribution: Ubuntu, Backtrack, FC10
Posts: 40

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by micxz View Post
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.
 
Old 08-15-2009, 02:26 PM   #10
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
I don't think you understand what I'm asking. For example if you are sending a message to user "bob" on "host1" and you send the command:
Code:
ssh -X bob@host1 "DISPLAY=:0 notify-send "message"
"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.
 
Old 08-15-2009, 02:46 PM   #11
uncertain
Member
 
Registered: Oct 2008
Location: Katowice, Poland
Distribution: Ubuntu, Backtrack, FC10
Posts: 40

Original Poster
Rep: Reputation: 15
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.

Last edited by uncertain; 08-15-2009 at 02:52 PM.
 
Old 08-15-2009, 03:24 PM   #12
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Quote:
Originally Posted by uncertain View Post
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.
I'd rather not we got into it pretty deep here:
http://www.linuxquestions.org/questi...o-work-741489/
 
Old 08-15-2009, 04:05 PM   #13
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 08-15-2009, 04:56 PM   #14
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
jschiwal your right as well. Maybe I was talking the thread to far to cover other factors that I now realize the OP does not need.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
RedHat: Monitor specific security events and send notifications about them rstev39147 Linux - Security 8 08-03-2008 08:44 PM
[exim4] how to send mail to local computers (on LAN) jabka Linux - Server 4 05-20-2008 12:26 AM
Can I setup my system to send me an email everytime an ssh session starts? ille.pugil42 Linux - Networking 3 03-14-2007 11:24 AM
Two computers behind router, how do I ssh from outside the LAN? kdnewton Linux - Networking 13 08-30-2006 01:38 AM
Need a way for slack server automatically send various notifications cav Slackware 3 04-20-2006 05:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration