Tuesday, July 31, 2007

Magic Audio & Video tricks using SSH and X

Think of this: Your friend is working on your Linux desktop machine, and you are working on another Linux machine either at home or office. Your friend is editing some files and BOOM - speakers start making horrifying sounds, some horror video starts playing as full-screen mode... And if you have access to your home's electricity connection from that computer then your friend is going to have the worst experience with ghost!!!

I will let you think of more horrifying tricks later on after you know how to setup environment like this. All you need is SSH daemon running on destination machine and access to that machine using internet. Of course you must have access to user account that has all the permissions to running X server, a root access will grow two horns on your head !!!

If you are new to SSH, you should see this tutorial on SSH.
Setting up the SSH daemon in server machine is simple, install 'sshd' package and as root give following command:
root@servername# sshd

It will start a ssh daemon and you are all set to login from remote machine.

Login using SSH with -Y option:
user@localname$ ssh -Y username@servername

Using -Y you can securely forward all X11 connections to your local machine. So now fire-up your loved MP3 music player (i prefer amarok, great player with great interface) and your familiar window will greet you. So when you start playing music, it will use server machine's speakers, not local machine's !!! You can download some horrifying sounds and set up a script to play them one by one to be prepared for horror attack...

Now as you can see that we can open almost any GUI window in local machine, lets start playing some video using vlc or mplayer. But whats this, the video is played in local machine... Well to play the video in server's monitor screen, which is screen no 0 (or in pure X term display :0.0 ). To make sure that your video plays on server's monitor not in local, change your DISPLAY environment variable from localhost:10.0 (somthing like that) to :0.0 .

Check your DISPLAY variable value in your ssh session:
user@servername$ echo $DISPLAY
If its not set to :0 simply change it using following commands:
user@servername$ export $DISPLAY=:0.0
or if you are using 'csh':
user@servername$ setenv $DISPLAY :0

To make sure that the X display number is :0.0 , give the following command in your ssh session:
user@servername$ ps -ax | grep 'X'
It will show something like this:
user@servername$ 3744 tty7 Ss+ 2:01 /usr/bin/X :0 -audit 0 -br -auth /var/lib/gdm/:0.Xauth vt7

Here, the value after '/usr/bin/X' is ':0' so your display number is 0. If there are more than one display running than you may have to do trial and error to find the display number.

Now you are set to play videos in server machine's monitor. Use vlc with following options:
user@servername$ vlc -f videoname.avi
or if you like mplayer:
user@servername$ mplayer -fs videoname.avi

Now you have learn the dark side tricks of SSH go and practice it to be master of it. And when you successfully execute this mission don't forget to tell me your friend's reaction!!!

NOTE: If you try to use firefox over SSH, when we give normal 'firefox' command it runs locally not remotely, so music will be played in local machine. To fix this problem start the firefox with following options:
user@servername$ firefox -no-remote

This will start the firefox in remote machine so any music played in it will play in remote machine's speakers.... enjoy.

- Avadh Patel