Showing posts with label ssh. Show all posts
Showing posts with label ssh. Show all posts

Wednesday, December 4, 2013

Access and stream a webcam over ssh to vlc

Ever had the desire to see if someone was on your computer? Wished you could get on there and just see through your webcam? Me neither, but one day I got curious and wanted to see if/how it could be done. It's kind of a beast of a command, but I just put it in a script which makes it very easy to use.

What you need:
ffmpeg
vlc (or mplayer)

Here's the script.( I actually got it from here. Then just changed it to use vlc because it seems to work better for me.


#! /bin/bash

B="100K"
F="ogg"
ssh <remote_host> ffmpeg -b $B -an -f video4linux2 \
    -s 320x240 -r 10 -i /dev/video0 -b $B -f $F - \
    | vlc -


I would break it down for you but I haven't gotten familiar enough with the ffmpeg command. So all I really do is copy and paste, and I only have a general idea of what it is doing. It worked when I copied it so I didn't need to figure it out to change it. If it isn't working, the first thing I would check is the device name in case it's not /dev/video0
vlc -
just tells vlc to read from stdin.(the pipe)

In my script I substituted <remote_host> for $1 so from the command line I can just run
$./script_name <remote_host>
since I wanted to be able to use it on different computers/webcams instead of just one (if I get more than one webcam on one computer then I may also have /dev/video0 passed as a parameter.

You can change the size with the -s flag I changed mine from 320x240 to 640x480 and it streamed fine.

It may take a few seconds for the picture to actually come through and be received, (I know it does for me) so once you run it then give it a little minute to get that data through to vlc.

So there it is. Just for fun.

Wednesday, November 27, 2013

Useful networking commands

I started to begin a post about useful linux commands and command line tricks. It didn't take long for me to realize that would make a very very long post.

I'm going to start with some more common commands and some I've looked at recently. I expect this list to grow. For most of this I will list the command, my most common use(s) for it, and maybe some examples.

netstat
Useful for seeing what ports are open on your computer and what process is manning those ports. I might use this to make sure something like and ssh-server is running. Very useful on servers - dns,mail,web - for making sure your programs are configures to use the right ports.
netstat -natp
sudo netstat -natp|grep 'sshd'

ssh
If you don't know what this is for or how to use it, I don't see how anything else in this post will help you much.

traceroute
Traces the route a packet takes to get to a destination. I haven't found a really great regular/common use for it, but it is fun to run every once to a while to see where things are going, and what route they are taking.
traceroute www.google.com

mtr
It does the same as traceroute but it consistently pings each hop, and keeps track of packets - dropped, etc - can be useful for seeing where a connection may be getting hung up.
mtr www.google.com

xtightvncviewer
I use this all the time on my personal computers. I have one laptop that the battery doesn't work in, so it's always connected to the tv. It's nice to be able to connect to it from any other computer to change stuff. Or if I am trying to follow something my wife is doing on the computer and guide her through it I can connect in view only mode to always have a visual.
vncviewer remote.ip.addr[:port]
vncviewer -viewonly remote.ip.addr[:port]

whois
This can be useful, but like traceroute is mostly a curiosity thing. It will tell you who a particular ip address is registered to in the whois database.
whois ip.addr

nmap
This is a powerful great program that has many many uses. My most command is seeing things like what other computers are on the network and what services they are running. It scans the network and displays information.
nmap -v -sT -rF 192.168.0.0/24
The above command runs an nmap scan in verbose mode (-v -sT), scans the ip addresses between 192.168.0.0-255 (192.168.0.0/24), Scans in fast mode the most common ports (-F) and scans ports consecutively - not randomly (-r).
I use this with grep to show only the info on open ports.
If all I want to see is the open ssh ports on my network I can use -p22 instead of -rF

sshuttle
This is one great program. I had never heard of it until recently, but it's pretty much the bomb. It can do more but the first thing I've used it fore was to set connect to another computer (via ssh) and route all network traffic through the tunnel. (pretty much sets up a VPN for you via ssh). You can use it to get around dns blocks. Or if someone wants to join your LAN game who is not on your LAN sshuttle will put you on their network.
sshuttle -r <server> --dns 0/0
I did run into a little bit of trouble on ubuntu 12.04 my first time trying it and I actually had to run these comands on the client and server (both ubuntu 12.04)
sudo dpkg-reconfigure resolvconf (select yes on any prompts)
sudo resolvconf --enable-updates
sudo reboot

sshfs
mount an ssh filesystem, I already have this one in another post but here it is for redundancy. You can mount a folder from one computer to an empty folder on your computer. syntax is similar to scp
sshfs <server>:/path/to/source /path/to/destination

Well that's it for now, I'm sure the list will grow.

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.

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.

Monday, April 15, 2013

Useful IT commands

SSH
to connect to computer via ssh:
ssh username@host
to create tunnel

ssh username@host -L p1:h:p2

p1=local port (the port it is being directed to on local machine)
h=host machine, can be localhost(which will be remote machine) or machine accesible from original remote host(host)
p2=remote port, port on host machine(h) that is sending info

to be able to open up graphical applications on local computer

ssh -X username@host

to use ssh without needing a password:
first create an RSA key -

ssh-keygen

keep hitting enter for everything
will save key in ~/.ssh/
do this on both computers
then you need to send id_rsa.pub between computers. save in .ssh as authorized_keys
my preferred method (just one way, needs to be done on both)

cd ~/.ssh/
scp id_rsa.pub username@host:.
ssh username@host
cat id_rsa.pub >> ~/.ssh/authorized_keys

ARP
shows current/recent connections
arp -a

PING
sends packets (useful for checking network)
ping -c <#> <domain/ip>
-c for count without specific count will contiue until you tell it to stop

TRACEROUTE
traceroute <domain/ip>
will trace the route to specified destination and return ip's from each point along the way

WHOIS
whois <ip>
will return information about ip (like registration info)

NAT - to set up NAT on a gateway computer
sudo iptables -t nat -A POSTROUTING -o <network card:e.g. eth0> -j MASQUERADE
echo '1' | sudo tee /proc/sys/net/ipv4/ip_forward

DIG
shows dns servers, makes it possible to trace path one by one.

NMAP
nmap has various uses. my most recent = finding what other computers are on the network with me (and ip, or more specifically, just the ip)

nmap -sP 192.168.1.1-254

Worked great for me. you could also use

nmap -sP 192.168.1.1/24

These will show you the ip's that are up on your network. (well, on the 192.168.1.x network)