| | Stumble It! | Add to Mixx! | | diigo it | | Slashdot |

Thursday, February 19, 2009

Streaming Webcam over SSH - Part 2

Last week I posted an article detailing how to get your laptop's integrated webcam to stream live video via SSH. That solution depended on you being able to initiate the SSH connection into your lappy which isn't really realistic since should your precious lappy be 'misplaced', chances are that if it were to get back online, it would be done from a private IP address and the necessary ports wouldn't be forwarded.

Today, however, I was doing some research on SSH VPNs and I stumbled across the solution.
ssh -nNT -R [rp]:localhost:[lp] [myDesktop]

[rp] = port on [myDesktop] to open
[lp] = lappy SSH server port
[myDesktop] = desktop server at my house
This version of the solution will have lappy automatically build a tunnel to the SSH server on myDesktop that can then be used to connect back to the SSH server on lappy. Once I can connect back into lappy I (or you) can then initiate and stream the webcam feed.

Here's the step-by-step starting from zero, but assuming a certain level of technical experience:
  1. On myDesktop (WinXP):
  2. On lappy (EeePC 701 w/ Ubuntu):
    • Install VLC.
    • Install OpenSSH.
      sudo apt-get install openssh

      # be sure to edit /etc/ssh/sshd_config to suit your needs
    • Configure Key Based Auth with myDesktop.
    • Create the script to automatically build the tunnel (./tun.sh).
      #!/bin/bash
      CONNECTED=NO
      while true
      do
      if [ $CONNECTED=NO ]
      then
      if [[ `ifconfig ath0 | grep "inet addr:"` || `ifconfig eth0 | grep "inet addr:"` ]]
      then
      CONNECTED=YES
      ssh -nNT -R [rp]:localhost:[lp] [myDesktop]
      fi
      fi
      sleep 600
      done
      Make this script executable and put it in your startup list so that it will automatically initiate the tunnel when the script detects a connection on eth0 or ath0.
      System > Preferences > Sessions > (add the ./tun.sh script)
    • Create the script to send the webcam stream (./vlcspy.sh)
      #!/bin/bash
      vlc v4l2:// :v4l2-dev=/dev/video0 :v4l2-adev=/dev/dsp :v4l2-standard=0 :sout="#transcode{vcodec=mp4v,vb900,scale=1,acodec=mpga,ab=129,channels=2}:duplicate{dst=std{access=http,mux=ts,dst=localhost:9091}}"
      Make this executable also since this is the script you'll run when you connect to your lappy from your desktop.
    • Connect to the internet.
  3. On myDesktop:
    • Open an SSH tunnel connection to localhost:9090...
      PuTTY
      Hostname = localhost
      port = 9090
      connection > ssh > tunnels >
      source port = 9091
      destination = localhost:9091
      Linux
      ssh -L 9091:localhost:9091 localhost -p 9090
    • Run your ./vlcspy.sh script.
    • VLC Player > Media > Open Network ... > HTTP > localhost:9091
    • Enjoy!
Note that if you try using this with DynDNS and lappy and myDesktop are behind the same router, it won't work. You'll need to use the IP address instead of the DNS.

1 comments:

pendragon said...

you can just use

ssh -L 9091:localhost:9091 localhost -p 9090 'vlc v4l2:// :v4l2-dev=/dev/video0 :v4l2-adev=/dev/dsp :v4l2-standard=0 :sout="#transcode{vcodec=mp4v,vb900,scale=1,acodec=mpga,ab=129,channels=2}:duplicate{dst=std{access=http,mux=ts,dst=localhost:9091}}"'