I was presented with a problem today. How can we send the commands typed from a
linux shell, live, to a HipChat. This
document is my attempt to make that happen.
This guide assumes you have hipchat set up. If you need help with that I wrote a guide at
http://www.whiteboardcoder.com/2014/08/hipchat-getting-stared.html
Some research
What, if anything, is out there that can do this? Or do it in part?
I found three tools
- Commands.com
- script
- bash-preexec
Commands.com
This one seems to fit the bill for the needs we have, but I
don't see us using it for a few reasons. One being our big use of HipChat.
https://commands.com/
[1] has tools in Linux and OSX that you can download that allow you to captures
your command line outputs.
Here is an example from https://commands.com/monitor-source-code-directory
I am not going to dive into how to set it up, since I am not
going to use it myself, I just wanted to make note of it.
Script
Here is another one of those Linux tools I wonder why I have
never heard about it. The man page for it can be found at http://linux.die.net/man/1/script
[2]
Script will open a new terminal and record all
the input and outputs of commands. It
will record it all to a file and you can force it to record on any changes.
Here is a quick example I tried.
> script filename.txt
|
And then if I run a few commands in the new terminal
Then exit from the script and cat the new file
> exit
> cat filename.txt
|
It recorded the entire session. Even the command prompt.
Use flush to force updates as you type
> script -f filename.txt
|
(OK that feature does not work in OSX but does in linux)
This seems like an interesting tool and may be the one I can
use to get the job done.
bash-preexec
I found this tool at https://github.com/rcaloras/bash-preexec
[3] It’s a tool for bash to give it some tools that the zsh shell has, see this page http://zsh.sourceforge.net/Doc/Release/Functions.html#Hook-Functions
[4]
Install this script, from the github repo
> curl
https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh
> ~/.bash-preexec.sh
|
Note: This script
does not work as is on OSX but seems to work fine on Ubuntu.
Add the command to run this to your .bashrc file
Here is a simple command
to accomplish this is.
> echo '[[ -f
~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >>
~/.bashrc
|
Run the following command to add a prexec() command
> preexec() {
echo "just typed $1"; }
|
Then run a simple command to see it in action
> echo hi
|
It worked!
Now how to incorporate it into a script and get it to send
messages to HipChat.
Simple Script
I am just going to explore here a little bit and see what I
can figure out.
Create a script
> touch
send_commands_to_hipchat.sh
> chmod u+x
send_commands_to_hipchat.sh
|
Here is my starter script (just to prove it out)
#!/bin/bash
initial_commands+="PS1=\"HIPCHAT_MODE::\$PS1\";"
initial_commands+="ls
-alh;"
initial_commands+="du
-hs;"
bash --init-file <(echo
"$initial_commands")
|
It opens a new bash shell and runs all the
initial_commands. The PS1 command
changes the prompt so the user is aware they are in HIPCHAT mode.
If I run it
> ./send_commands_to_hipchat.sh
|
I can see the commands ran and I see HIPCHAT_MODE.
More complex script
Here is the second version of the script I came up with.
#!/bin/bash
initial_commands+="source
~/.bashrc;"
initial_commands+="source
~/.profile;"
initial_commands+="PS1=\"HIPCHAT_MODE::\$PS1\";"
initial_commands+="echo \"=================================\";"
initial_commands+="echo
\" \";"
initial_commands+="echo
\" ENTERING HIPCHAT MODE\";"
initial_commands+="echo
\" \";"
initial_commands+="echo \"=================================\";"
initial_commands+="preexec()
{ echo \"just typed \$1\"; };"
bash --init-file <(echo
"$initial_commands")
|
If I run this command
> ./send_commands_to_hipchat.sh
|
Then run the following command
> echo hi
|
It starts a new bash shell, reloads .bashrc and .profile,
prepends "HIPCHAT_MODE::" to the command line prompt, and sets a preexec() command.
Now whenever a command is run it will run the preexec
command listed. Now I just need to
figure out how to use the restful service from HipChat to get it working.
HipChat
I am going to create
a room and a token so I can send message to that room using v2 of the Hipchat
RESTful service. You must be an admin
on your HipChat account to do this.
Go to https://www.hipchat.com/
Click Log in
Enter your credentials and click Log In.
Click Launch the Web App
Click Create Room
give it a name, I am calling mine "command line"
and click create room.
Go back to your HipChat.com/home (tools)
And click on Group Admin.
Click Rooms.
Open the Command Line room.
Make note of the API ID
In my case 1128937
Think of this as the rooms number
Now get your token (key for the room)
Click on Tokens.
Give the token a name and click Create.
Your token has been made.
Mine is
fRPTle17Oy64efGWey6XYRVZFybiAcO0oyPGq0nu
(I am going to erase this token before I finish this
document, to erase one from the tokens page click the trash can to the right of
your token)
OK now what do I do with this?
RESTful service
The details for HipChat's room notification RESTful service
can be found at
I found this post showing how to put together a curl for v2
of HipChat https://gist.github.com/danriti/7345074
[6].
> curl -H "Content-type:
application/json" \
-H "Authorization: Bearer $AUTH_TOKEN" \
-X POST \
-d "{\"color\":
\"purple\", \"message_format\": \"text\",
\"message\": \"$MESSAGE\"}" \
https://api.hipchat.com/v2/room/$ROOM_ID/notification
|
Which when you put in my room number and token you get
> curl -H
"Content-type: application/json" -H "Authorization: Bearer fRPTle17Oy64efGWey6XYRVZFybiAcO0oyPGq0nu"
-X POST -d "{\"color\": \"yellow\",
\"notify\": true, \"message\": \"Test message From
Patrick that is yellow\"}" https://api.hipchat.com/v2/room/1128937/notification
|
Running this command a few times I see it works!
Now to
incorporate it into my script
Script with HipChat
Here is the script I
came up with replace the room_id and token with your own values. Change the color to the color you want, valid
values are yellow, green, red, purple, gray, and random.
room_id=1128937
token="fRPTle17Oy64efGWey6XYRVZFybiAcO0oyPGq0nu"
color='purple'
initial_commands+="source
~/.bashrc;"
initial_commands+="source
~/.profile;"
initial_commands+="PS1=\"HIPCHAT_MODE::\$PS1\";"
initial_commands+="echo
\"=================================\";"
initial_commands+="echo
\" \";"
initial_commands+="echo
\" ENTERING HIPCHAT MODE\";"
initial_commands+="echo
\" \";"
initial_commands+="echo
\"=================================\";"
initial_commands+="preexec()
{ curl -H \"Content-type: application/json\" -H
\"Authorization: Bearer $token\" -X POST -d
\"{\\\"color\\\": \\\"$color\\\", \\\"notify\\\": true, \\\"message\\\":
\\\"\$1\\\"}\"
https://api.hipchat.com/v2/room/$room_id/ notification; };"
bash --init-file <(echo
"$initial_commands")
|
And it works!
If I run this command
> ./send_commands_to_hipchat.sh
|
Then run the following command
> echo hi
|
It worked!
Hope this helps someone out there :)
References
[1] Commands.com web site
Accessed 01/2015
[2] Script Linux Man Page
Accessed 01/2015
[3] bash-preexec github page
Accessed 01/2015
[4] Zsh hook functions
Accessed 01/2015
[5] HipChat Room Notification API
Accessed 01/2015
[6] HipChat API v2 - Send a message to a room using cURL
Accessed 01/2015
This is really helpful, it's really really great script, we're using local HipChat server, I'll try it today. Thank you~~~~~~~~~~~~~~~~~
ReplyDeleteWow! I am amazed. I figured it was just a mad scientist project. Nice to see it's useful to someone, tell me how it goes :)
DeleteYour script is only for the cloud Hipchat, we are using our local server, it's little different, I'm still testing, and I want this script can send the message to all the user not only for one chat room. Like in command when I type hello world @username, then the user will receive the message, one to one chat in command.
Delete