Monday, September 16, 2013

Using a Raspberry Pi as a media server

Just got a Raspberry Pi. Pretty sweet. So far I like it.

I've had this post as a draft for quite a while, planning to make some great interesting post, but I've run into quite a bit of trouble trying to find a reliable setup that I liked, and that worked.

Here's what I've got that works ok so far:

First off. I don't have that big of an sd card(which is like the hard drive) for my pi. and all of my music is on my desktop computer. First thing I do is use sshfs to gain access to my music.
$sshfs desktop:Music/ Music/
I use public/private rsa keys to connect so I don't need a password. I also have the desktop computer's ip defined in my /etc/hosts file. For any help check here.

Now that I have access to my music, my most used tool is cmus. It is a music player that supports a music library, has vi like commands, and runs inside a terminal. There are others out there, the first I tried was MOC, but after trying cmus I switched.

After logging into my pi via ssh. I run tmux.(more info about tmux here.), then run cmus, find the music I want to listen to, start playing, then detach tmux, and I can then exit ssh.

Another option. If you wanted to control your music from say... your phone. Then I would use:
$vlc -I http ~/Music/
This will run vlc as an http service, the the Music/ folder as the main library. I was using this but I have a large music library, and it was hard to navigate very well. If you specified the folder with all the music you would like to listen to I think this would probably work better.

Currently working on running the nuvola music player, then using nuvolaplayer-client to control it from another computer. If I get it working I will definitely update.
=================
UPDATE: I just installed OpenELEC on my raspberry pi (It runs xbmc). It's easy to set up. I would recommend this method if you are going to be using it for different types of media.

Shell history search

Found at http://lifehacker.com/supercharge-your-command-lines-history-search-with-fou-478683529
I have found this to be very useful. 
create file ~/.inputrc and add these lines:

"\e[A": history-search-backward
"\e[B": history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on

This will make it so that when you use the up arrow to go through your search history, if you have anything already typed, it will only search anything with that term. e.g. if you want to run a sudo command from before you can type sudo, then use the up arrow to search commands that were run with sudo.

Monday, September 2, 2013

Using your ssh config file to connect to a computer through a gateway.

At my school I have some public IP addresses and virtual machines for them to use for class projects. I enjoy connecting from home but I have to go through the school's ssh server to connect to a vm(virtual maching). Having to hop through the school's server every single time I want to connect can be cumbersome, and also makes it more difficult to send files to my vm, since I have to send the file to the ssh server, then connect to it and send it again to the vm.
The other day I started looking for a way to modify my ~/.ssh/config file to have it automatically go to the vm. I tried a few different things. Some didn't work and some weren't working because of a  mistake of my own. Finally I found how to make it work. My config file is set up like so:
Host school
HostName domain.name.of.school.ssh.server
User my_school_username

Host ns1
HostName <ip of my master nameserver>
ProxyCommand ssh school -W %h:%p

Host ns2
HostName <ip of slave nameserver>
ProxyCommand ssh school -W %h:%p

The computers ns1 and ns2 are the ones that are behind the school's ssh server, and school I set as the ssh server. the ProxyCommand lines let ssh know that every time I want to connect to ns1 or ns2 that I need to go through 'school'.
One problem I ran into was that I tried to use the HostName ns1 or ns2(since they are designated as such in the ssh config on the school server). That doesn't work. You have to put in the ip of the machine.

Enabling the right click on an hp pavilion dm4 with ubuntu 12.04

I have a 14" HP Pavilion dm4 laptop and like most of my laptops, the mousepad always seems to have some type of problem at the beginning. On the dm4 my problem was that the right click didn't work. after finding an askubuntu.com question about it I found that I needed to run these three commands.
synclient RightButtonAreaRight=1
synclient RightButtonAreaBottom=1
synclient AreaBottomEdge=3500
Sadly, I can not expound really on what the synclient command does, I just know that I needed to run all three before it would work and I have to run them each time I reboot (I'll try and figure out how have it stick even after rebooting and edit this post when I do.)

Send a message to another computer via ssh

I don't know if everyone else has the same problem as me, but I have two computers in different rooms, and sometimes my wife and I are on each computer. Sometimes I want to tell my wife something or send her a message, but I'm just too lazy to get up and go into the other room to talk to her. One thing I do have is ssh access to all of my computers. After searching a little (there's references to this just about everywhere), I found how to send a message to my wife via ssh.
ssh
export DISPLAY=:0
notify-send "Message"
Now I had a computer before that for some reason or another, the default monitor/display wasn't 0 and it took some searching to find out which was the right one. Although the easiest way would probably be to increment the display number until you find the right one.
notify-send will send a notification to the screen which will time out and can easily be missed if the person on the other computer is not watching. You can also put a message in a text file, then open it up with something like gvim textfile.txt which will open a new window with the text file (gvim can also be replaced with the more common gedit)
Once you export the display in fact, you can open up any graphical program, and it will open up on that computer.
If you open up a program and then want to close after, you can run
killall <program_name>
to close it.

Sunday, August 25, 2013

Using sshfs to set up a filesystem via ssh

I stumbled upon this because I wanted to use my Raspberry Pi as a media device. (There'll be a great post on some ways to do that but I haven't figured out how I want to do it and gotten it to work yet.) Basically, my raspberry pi doesn't have enough room for all of my media. (mainly just music is all I was working with) and all of my music is on another computer on the network. I tried setting up an ftp server on that computer, which will work, I just didn't like it very much. Eventually I found sshfs which was great for what I wanted to do.

To install sshfs if you don't already have it. - on ubuntu/similar.
sudo apt-get install sshfs

If you have used ssh or scp then sshfs should be pretty simple It's set up like this:
sshfs [user@]host:[dir] mountpoint [options]

So, lets say I'm logged into my raspberry pi, and I want, let's say to be able to access all of my music from my desktop computer through a folder in my home directory(on my raspberry pi). My desktop is named sigsauer.

From my raspberry pi:
mkdir ~/Music/
(if it doesn't already exist)
then,
sshfs sigsauer:Music/ ~/Music/
One thing I learned at this point, is that when you install new system you need to remember to keep you /etc/hosts file up-to-date. Because if you don't you might run into problems when trying to use the computer name. Once I got past that little hangup, then things started to work perfectly.
Now I could go into my Music folder which I just created and access all of the files as if they were local.
This has been great for me since I have 2 computers and a raspberry pi, that stay at home and are always on the same network, I don't like having my music library all over the place, adding music here and there then making a mess when I try to compile it. Now I have everything reading off and editing the same library, making it much easier to keep my library organized.
One of the computers has ubuntu server on it and I didn't feel like dealing with installing dropbox on it, so I simply added a command to my crontab to mount the dropbox folder from my desktop computer to my server computer. More about crontab here.
Keep in mind that to add it to the crontab, since it can't ask you for a password you need to have passwordless ssh authentication. I have a little bit of instruction about how to do that which I may have to turn into a new post, but I found the easiest way for me is to simply copy the whole ~/.ssh/ folder from one computer to the other, then delete any offending keys from ~/.ssh/known_hosts file.

Once you are done with sshfs and don't need access to the files anymore, you can unmount the directory by simply:
fusermount -u <mount_point>
so to unmount my Music -
fusermount -u ~/Music/

And now if you went into the folder it should be empty.

Wednesday, July 10, 2013

Adding cronjobs using crontab in Ubuntu 12.04/Mint 13

Cronjobs are a good way to run a script every so often (hour, day, week, at reboot, etc.) Some things I use it for are:
To keep my public ip address updated and saved in my Dropbox folder,
To mount my external hard drive on my ubuntu server every time it reboots,
And to update my psmouse-alps driver which needs to be updated at reboot as well.

I'm sure someone else can find many other possible more useful applications for this.

First think you need to do is find out what privileges you need to do what you want to do. Since all i need to update my public ip is access to the internet and also my dropbox folder, I only needed my regular user privileges.

Crontab has some pretty good explanations and even gives you an example in the comment in the file. The first 5 fields (separated by spaces) are to tell it when/how often you want to run a certain command/script. Right after the first five fields you just put the command/script you want to run.
The example used in the file:

0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

I found some pretty good examples to help you to understand the schedule notation that crontab uses. Wikipedia has a useful article here. Also this site was very useful. What I found was also very useful was some of the shortcuts, listed here.
They are:
@reboot
@yearly
@annually
@monthly
@weekly
@daily
@midnight
@hourly 

So on my ubuntu server, since I need root privileges to mount my external drive, I edit the crontab by executing:
sudo crontab -e
Then I choose to edit it with vim since it is my preferred text editor, and add this line to have my external hard drive mount at boot:
@reboot mount -t ntfs-3g /dev/sdb1 /media/external
And to have my ip update every day I run:
crontab -e
(as a regular user, I don't need root for this)
and have it run my script to update the ip daily:
@daily /home/user/bin/ip_script
For any interested my script is very simple(3 lines), and only needs the curl utility installed.
sudo apt-get install curl
also dropbox if you don't already have it.
then open a text editor and write

#! /bin/bash
curl ifconfig.me >> public_ip
mv public_ip ~/Dropbox/

Then name and save the file, make it executable and add it to crontab.