Friday, August 30, 2013

Multiple output files with dd utility: example

I just used this method to create a file MD5 "on the fly" meaning, one read operation when moving the file to a destination.

dd if=test.txt | tee >(md5 >> tmp/test.txt.md5) | dd of=tmp/test.txt or you can reverse the order of the destination and MD5
dd if=test.txt | tee >(dd of=tmp/test.txt) | md5 >> tmp/test.txt.md5

This worked for me.  The usage case would be when copying a file from "local" media to "remote" media (ex: local HDD to SAN volume)

Wednesday, August 21, 2013

Code Sample - BASH - FTP Server Management Script - IvansTool

I've created some scripts for managing an FTP server (for allowing non command line people a way to remotely manage the service via SSH)

While posting this script I see places that could be improved, simplified, and commented.  I may or may not come back and dress it up a bit... time permitting.

This script is called "IvansTool":

#!/bin/bash
#IvansTool
#
#------------------------------------------
#
#       Ivan Lawrence
#       written: Jan 26, 2010
#
#------------------------------------------
#
#   >>FUNCTIONS<<
function if0 {
if [ -z $1 ]
then
 echo "No empty variables!"
 echo "$2 was empty!"
 exit 0
#else
# echo "$2 = $1"
fi
}
function pause {
read -p "Press the [ENTER] or [RETURN] key to continue or CTR+C to cancel..."
}
#
# See what Productions have a mounted volume
function listProd {
GetProd=$(df -H | grep /data/ | awk -F" " '{print $5}' | awk -F"/" '{print $3}' | sort -d)
select ProdName in $GetProd
do
if [[ -n $ProdName ]]
then
 break
else
 echo "Select a number..."
fi
done
}
#
# See which users have been created
function listUser {
GetUser=$(grep /data/ /etc/passwd | awk -F":" '{print $1}' | sort -d)
select username in $GetUser
do
if [[ -n $username ]]
then
 break
else
 echo "Select a number..."
fi
done
}
#
# create a password from a limited "alphabet" excluding confusing chars
function passwordGen {
unset PASS
MATRIX="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz!@#$%&*?-_=+)(}{][><"
LENGTH="8"
n=1
while [ "$n" -le "$LENGTH" ]
do
        RealRand=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -f1 -d" ")
        PASS="$PASS${MATRIX:$(($RealRand%${#MATRIX})):1}"
 #echo "$PASS"
        let n+=1
done
password=$PASS
}
#
# create a password from a limited "alphabet" excluding confusing chars and in a human friendly pattern
function custPassGen {
unset PASS
MATRIX="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz!@#$%&*?-_=+)(}{][><"
Cap="ABCDEFGHJKLMNPQRSTUVWXYZ"
Low="abcdefghijkmnpqrstuvwxyz"
Punc="!@#$%&*?-_=+)(}{][><"
Num="123456789"
LENGTH="9"
n=1
while [ $n -le "$LENGTH" ]
do
 RealRand=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -f1 -d" ")
#       PASS="$PASS${MATRIX:$(($RealRand%${#MATRIX})):1}"
 if [[ (($n > 3)) && (($n < 7)) ]]
 then
  PASS="$PASS${Cap:$(($RealRand%${#Cap})):1}"
#  echo "$PASS"
 fi
 if [[ (($n < 3)) ]]
 then
  PASS="$PASS${Low:$(($RealRand%${#Low})):1}"
#  echo "$PASS"
 fi
 if [[ (($n == 3)) || (($n == 7)) ]]
 then
  PASS="$PASS${Punc:$(($RealRand%${#Punc})):1}"
#  echo "$PASS"
 fi
 if [[ (($n > 7)) ]]
 then
  PASS="$PASS${Num:$(($RealRand%${#Num})):1}"
#  echo "$PASS"
 fi
 let n+=1
# echo $n
done
password=$PASS
}
function emailRootAdminemail {
# experimental: I have not tested this loop for sending email to root per account!
emailDir=Scripts
address=root
if [ -f $emailDir/ftp_email_body ]
then
 sh $emailDir/ftp_email_body $1 $2 > $emailDir/ftpemail
 mail $address -s "FTP account created: $1" < $emailDir/ftpemail
 rm $emailDir/ftpemail
else
 echo "can't find $emailDir/ftp_email_body"
fi
}
function passGen {
echo "Either a totally random password:"
passwordGen
echo $password
echo "Or a formulaic password:"
echo "(lower x2, special x1, capital x3, special x1, integer x2)"
custPassGen
echo $password
}
function diskUsage {
echo "Display usage for"
echo "A - All"
echo "P - a Production"
echo "L - Logins in a Production"
read usageChoice
case $usageChoice in
 A | a ) echo "Dir  Size  Used  Avail  Use% "
 df -h | grep /data/ | awk -F" " '{print $5,$1,$2,$3,$4}' | sort -d
 ;;
 P | p ) listProd
 echo "Dir  Size  Used  Avail  Use% "
 df -h /data/$ProdName | grep /data/ | awk -F" " '{print $5,$1,$2,$3,$4}'
 ;;
 L | l ) listProd
 echo "*note: This could take a long time depending on the volume size"
 du -ch --max-depth=1 /data/$ProdName | awk -F"/" '{print $1,$4}' | grep -v lost+found
 ;;
 * ) echo "Enter your selection." ;;
esac
}
function rawSpace {
PEtotal=$(pvdisplay /dev/sdb1 | grep Total | awk '{print $3}')
PEfree=$(pvdisplay /dev/sdb1 | grep Free | awk -F" " '{print $3}')
PEsize=$(pvdisplay /dev/sdb1 | grep "PE Size" | awk -F" " '{print $4}')
GBfree=$(((($PEfree*32)-30627)/1024))
GBtotal=$(((($PEtotal*32)-30627)/1024))
echo "$GBfree GB free out of $GBtotal GB"
}
function usersFromCSV {
echo "If you used scp to get the file here I can move the file from your home dir to root's"
GetSSHuserhome=$(grep /home/ /etc/passwd | grep nologin -v | awk -F":" '{print $1}' | sort -d)
select SSHuserhome in $GetSSHuserhome root
do      
if [[ -n $SSHuserhome ]]
then
        break
else
        echo "Select a number..."
fi
done
if [[ $SSHuserhome == "root" ]]
then
 UserHomeCSV=$(find /$SSHuserhome -name *.csv)
else
 UserHomeCSV=$(find /home/$SSHuserhome -name *.csv)
 if [[ -f $UserHomeCSV ]]
 then
  read -p "Move $UserHomeCSV to /root for use? (y/n) " moveCSV
  if [[ $moveCSV == "y" || $moveCSV == "Y" ]]
  then
   mv $UserHomeCSV /root/
  else
   echo "Try again."
   exit
  fi
 else
  echo "No CSV file found in $SSHuserhome Try Again"
  exit
 fi
fi
echo "CSV must have no headers and be formatted: ProductionName, size (# in GB only), username, password, (additional info ignored)"
echo "Make sure there are no empty fields, empty lines, or unwanted spaces in lines!"
echo "*NOTE* You will enter VIM to edit the file, to remove lines navigate to the line to be deleted and strike the letter d twice."
echo "To exit VIM and continue strike ESC the type :q to quit, if changes have been made strike ESC then type :wq."
FindCSV=$(find /root -name *.csv)
read -p "I've found $FindCSV, would you like to use it? [y/n] " useResult
if [[ $useResult == "y" || $userResult == "Y" ]]
then
 vim $FindCSV
 CSVFILE=$FindCSV
else
 read -p "Path to CSV for user creation:" CSVFILE
fi
if0 $CSVFILE "CSVFILE"
read -p "Would you like a form email for each account sent to the admin account? [y/n] " sendRootEmail
echo "About to use File: $CSVFILE to create volumes and user accounts"
pause
if [ -f $CSVFILE ]
then
 exec < $CSVFILE
 while read line
 do
  TODAY=$(date +"%Y%m%d")
  TIME=$(date +"%H%M")
  ProductionName=$(echo $line | awk -F, '{print $1}')
  size=$(echo $line |awk -F, '{print $2}')
  username=$(echo $line | awk -F, '{print $3}')
  password=$(echo $line | awk -F, '{print $4}')
  if0 $ProductionName "ProductionName"
  if0 $size "size"
  if0 $username "username"
  if0 $password "password"
  echo "File entry = $ProductionName, $size G, $username, $password"
  if [ -d /data/$ProductionName ]
  then
   CHKVolGroup=$(lvdisplay | grep $ProductionName | awk -F" " '{print $3}')
   echo "Logical Volume already exists! ($CHKVolGroup)"
  else
   echo "Creating the volume for $ProductionName"
   lvcreate -L $size"g" -n $ProductionName VolGroupData 
   mkfs.ext3 /dev/mapper/VolGroupData-$ProductionName
   mkdir /data/$ProductionName
   mount /dev/VolGroupData/$ProductionName /data/$ProductionName
   cp /etc/fstab /etc/fstab.backups/fstab.$TODAY$TIME
   echo "/dev/VolGroupData/$ProductionName /data/$ProductionName ext3 defaults 0 0" >> /etc/fstab
   echo "Created volume: $ProductionName = $size G"
  fi
  id -un $username
  if [ $? == 0 ]
  then
   echo "User $username already exists!"
   echo "$password" | passwd $username --stdin
   echo "The password for $username has been updated to $password"
   # experimental: I have not tested this loop for sending email to root per account!
   if [[ $sendRootEmail == "y" || $sendRootEmail == "Y" ]]
   then
    emailRootAdminemail $username $password
   fi
  else
   useradd -m -d /data/$ProductionName/$username $username
   echo "$password" | passwd $username --stdin
   echo "User created: $username / $password"
   # experimental: I have not tested this loop for sending email to root per account!
   if [[ $sendRootEmail == "y" || $sendRootEmail == "Y" ]]
   then
    emailRootAdminemail $username $password
   fi
  fi
 #end while loop
 done
 if [[ $sendRootEmail == "y" || $sendRootEmail == "Y" ]]
 then
  echo "Accounts have been created and emails sent"
 else
  echo "Accounts have been added but no email sent"
  echo "NOTE: if you had answered Y to the erlier email question but are seeing this there was a problem"
 fi
 read -p "Delete $CSVFILE? (y/n) " CSVFileDEL
 echo "$CSVFILE and $CSVFileDEL"
 if [[ $CSVFileDEL == "y" || $CSVFileDEL == "Y" ]]
 then
  rm $CSVFILE
 else
  echo "*WARNING*: CSV file ($CSVFILE) still exists!" 
 fi
else
 echo "file not found"
 exit 0
fi
}
function userAdd {
echo "Add a Single User to an existing Production Partition"
echo "Select the Production:"
listProd
if0 $ProdName "ProdName"
read -p "Username: " username
if0 $username "username"
custPassGen
read -p "Use password $password [y] or create your own? [(c)reate]" genpass
if0 $genpass "genpass"
if [ $genpass == "c" ] || [ $genpass == "create" ]
then
 read -p "Password: " password
 if0 $password "password"
fi
read -p "Add $username to $ProdName using password $password ? [y/n]: " adduser
if0 $adduser "adduser"
if [ $adduser != "y" ]
then
 echo "No user added"
 exit 0
else
if [ -d /data/$ProdName ]
then
 id -un $username
 if [ $? == 0 ]
 then
  echo "User $username already exists!"
  exit 0
 else
  useradd -m -d /data/$ProdName/$username $username
  echo "$password" | passwd $username --stdin
  echo "User created: $username / $password"
  emailDir=Scripts
  address=dl-bur-serveradmin@starz.com
  if [ -f $emailDir/ftp_email_body ]
  then
   sh $emailDir/ftp_email_body $username $password > $emailDir/ftpemail
   mail $address -s "FTP account created: $username" < $emailDir/ftpemail
   rm $emailDir/ftpemail
  else
   echo "can't find $emailDir/ftp_email_body"
  fi
 fi
 else
  echo "Production does not exist!"
  exit 0
 fi
fi
}
function userProdAdd {
TODAY=$(date +"%Y%m%d")
TIME=$(date +"%H%M")
echo "Add a new Production Partition and user."
echo "ProductionName (all one word)"
echo "Capitalize the 1st letter of each word:"
echo "(ex: PostProd, IT, or Simpsons) "
read ProductionName
if0 $ProductionName "ProductionName"
read -p "Usable Gigs for $ProductionName: " size
if0 $size "size"
read -p "Username: " username
if0 $username "username"
#read -p "Password: " password
#if0 $password "password"
custPassGen
read -p "Use password $password [y] or create your own? [(c)reate]" genpass
if0 $genpass "genpass"
if [ $genpass == "c" ] || [ $genpass == "create" ]
then
 read -p "Password: " password
 if0 $password "password"
fi
#Check to see if the Production has a DIR
if [ -d /data/$ProductionName ]
then
 CHKVolGroup=$(lvdisplay | grep $ProductionName | awk -F" " '{print $3}')
 echo "Logical Volume / Production already exists! ($CHKVolGroup)"
 echo "Use a different Production name or choose 'Add a user to a Production'"
 exit 0
else
 echo "create the volume"
 #create the Logical Volume
 lvcreate -L $size"g" -n $ProductionName VolGroupData 
 mkfs.ext3 /dev/mapper/VolGroupData-$ProductionName
 mkdir /data/$ProductionName
 mount /dev/VolGroupData/$ProductionName /data/$ProductionName
 cp /etc/fstab /etc/fstab.backups/fstab.$TODAY$TIME
 echo "/dev/VolGroupData/$ProductionName /data/$ProductionName ext3 defaults 0 0" >> /etc/fstab
 echo "Created volume: $ProductionName = $size G"
fi
id -un $username
if [ $? == 0 ]
then
 echo "User $username already exists!"
 echo "Try Again!"
 exit 0
else
 useradd -m -d /data/$ProductionName/$username $username
 echo "$password" | passwd $username --stdin
 echo "User created: $username / $password"
 emailDir=Scripts
 address=dl-bur-serveradmin@starz.com
 if [ -f $emailDir/ftp_email_body ]
 then
  sh $emailDir/ftp_email_body $username $password > $emailDir/ftpemail
  mail $address -s "FTP account created: $username" < $emailDir/ftpemail
  rm $emailDir/ftpemail
 else
  echo "can't find $emailDir/ftp_email_body"
 fi
fi
}
function userDel {
echo "This will remove the FTP share and it's data"
listUser
usernameHome=$(grep $username /etc/passwd | cut -d ':' -f6)
id -un $username
if [ $? == 0 ]
then
 echo $username"'s home dir is " $usernameHome
 echo "If you want to also remove the Production partition then abort and choose that option."
 read -p "Continue removing only this user and it's data? [y/n]: " continue
 if [ $continue != "y" ]
 then
  echo "Nothing removed"
 else
  userdel -r $username
  echo "User $username has been deleted!"
 fi
else
 echo "User $username does not exist!"
 exit 0
fi
}
function userProdDel {
TODAY=$(date +"%Y%m%d")
TIME=$(date +"%H%M")
echo "Which production will you be destroying today?"
echo "Remember, this can not be undone!"
listProd
if0 $ProdName "ProdName"
ProdNameCK=$(grep "/data/$ProdName" /etc/fstab)
if [ -z "$ProdNameCK" ]
then
 echo "Production does not exists!"
 exit 0
else
 echo "The production has an entry in /etc/fstab of:"
 grep $ProdName /etc/fstab | cut -d ' ' -f1,2
 ProdNameFSTAB1=$(grep $ProdName /etc/fstab | cut -d ' ' -f1)
 ProdNameFSTAB2=$(grep $ProdName /etc/fstab | cut -d ' ' -f2)
 echo "The usernames associated with that Volume are:"
 ProdNamePASSWD=$(grep $ProdName /etc/passwd | cut -d ':' -f1)
 for u in $ProdNamePASSWD
 do
  echo "$u"
 done
 echo "Are you sure you want to continue?"
 echo "*** If there is no entry in /etc/fstab for $ProdName then do not continue!"
 echo "Check your spelling and try again (use the command df -H to list all volumes in use)"
 echo "Continuing will remove all the data in the FTP share(s) under $ProdName."
 read -p "Delete $ProdNameFSTAB2 **note: Once done you can not go back! [y/n] " answer1
 if [ $answer1 != "y" ]
 then
  exit
 else
  echo "Now deleteing $ProdNameFSTAB2 from the server"
  read -p "Remove users: $ProdNamePASSWD [y/n]: " userdel
  if [ $userdel != "y" ]
  then
   echo "user not deleted"
  else
   for u in $ProdNamePASSWD
   do
   userdel -r $u
   echo "User $u has been deleted!"
   done
  fi
  cp /etc/fstab /etc/fstab.backups/fstab.$TODAY$TIME
  grep $ProdNameFSTAB2 /etc/fstab >> /etc/fstab.removed
  echo $TODAY-$TIME >> /etc/fstab.removed
  grep -v $ProdNameFSTAB2 /etc/fstab > /etc/fstab.tmp
  cp /etc/fstab.tmp /etc/fstab
  umount $ProdNameFSTAB2
  lvremove $ProdNameFSTAB1
  read -p "Remove the DIR (rm -r $ProdNameFSTAB2) [y/n]: " dirrm
  if [ $dirrm != "y" ]
  then
   echo "dir not deleted"
  else
   rm -r $ProdNameFSTAB2
  fi
  echo "******** DONE ********"
 fi
fi
}
function volExtend {
echo "*Warning - Use caution, there is no checking so be sure your math is correct!*"
echo "Which production needs more space?"
listProd
LVPath=$(lvdisplay | grep $ProdName | awk '{print $3}')
CurrentSize=$(df -h | grep /data/$ProdName | awk '{print $1}' | sed s/.$//)
echo "You've selected $ProdName which is currently $CurrentSize G in size."
read -p "Please specify the new total (in Gigs)? " NewSize
echo "About to expand $ProdName (currently $CurrentSize G) to $NewSize G"
pause
#if [[ $CurrentSize -lt $NewSize ]]
#then
 lvextend -L $NewSize"g" $LVPath
 resize2fs $LVPath
#else
# echo "Error: Cannot srink the volume, the New size must be larger then the current size!"
#fi
}
function passReset {
custPassGen
read -p "Use password $password [y] or create your own? [(c)reate]" genpass
if [[ $genpass == c ]]
then
 read password
else
 echo "using $password"
fi
echo "List by production or user"
echo "P - Production"
echo "U - User"
read resetChoice
case $resetChoice in
 P | p ) listProd
 ProdNamePASSWD=$(grep $ProdName /etc/passwd | cut -d ':' -f1)
 echo "Reset all the users passwords?"
 for u in $ProdNamePASSWD
 do
  echo "Reset $u to $password"
 done
 pause
 for u in $ProdNamePASSWD
 do
  echo "$password" | passwd $u --stdin
  echo "User $u password = $password"
 done
 ;;
 U | u ) listUser
 read -p "reset $username password to $password [y/n]" ResetPass
 if [[ $ResetPass == y ]]
 then
  echo "$password" | passwd $username --stdin
  echo "user / pass is now $username / $password"
 else
  exit
 fi
 ;;
 * ) echo "Enter your selection." ;;
esac
}
function emailtest {
echo "enter the username/password you would like included in the FTP Account form email"
read -p "username:" emailtestuser
read -p "password:" emailtestpass
emailRootAdminemail $emailtestuser $emailtestpass
}
#Be sure it's running as root!
if [ "$(whoami)" != "root" ]
then
        echo "Error: You must be ROOT to add users!"
        exit 1
else
# selection=
# until [ "$selection" = "0" ]; do
  echo ""
  echo "#==> Make your choice! <==#"
  echo "1 - Display Production/disk usage"
  echo "2 - Display free raw disk space"
  echo "------------------------------"
  echo "3 - New Production and User"
  echo "4 - Add a User to a Production"
  echo "------------------------------"
  echo "5 - Remove a Production and User(s)"
  echo "6 - Remove a single User"
  echo "------------------------------"
  echo "7 - Add users or reset all passwords via CSV file"
  echo "8 - Remove users via CSV file"
  echo "------------------------------"
  echo "9 - Enlarge a production volume"
  echo "10 - Password Creator"
  echo "11 - Password Reset"
  echo "------------------------------"
  echo "12 - Generate FTP user/pass Admin email"
  echo ""
  echo "q - exit program"
  echo ""
  read -p "Enter selection: " selection
  case $selection in
   1 ) diskUsage ;;
   2 ) rawSpace ;;
   3 ) userProdAdd ;;
   4 ) userAdd ;;
   5 ) userProdDel ;;
   6 ) userDel ;;
   7 ) usersFromCSV ;;
   8 ) echo "under construction" ;;
   9 ) volExtend ;;
   10 ) passGen ;;
   11 ) passReset ;;
   12 ) emailtest ;;
   q ) exit ;;
   * ) echo "You're that stupid huh... Try Again!"
  esac
# done
fi
End

Friday, August 16, 2013

Read a Compressed files without the mess

From Commands I Always Forget...

>Read a text file in a zip inplace


use less to read a text file in a zip:
unzip -p [archive.zip] [inner/zip/path/to/file.txt] | less
unzip -c [archive.zip] [inner/zip/path/to/file.txt] | less
In a tar.gz
tar --to-stdout -zxf file.tar.gz | less
or just a file that is gzipped
gzip -c file.gz | less

GNU Screen Info

From Commands I Always Forget... See also Linux - GNU Screen Instructions

>GNU Screen

GNU screen gives a server admin the ability to connect to a multi-user terminal/shell or just a persistent shell session that can be disconnected from and re connected to later without interrupting the commands being run.  Lets say I am going to parse a bunch of files looking for duplicates and I'm working on bonding a few NICs together for additional functionality, and I have a little loop running to monitor uptime on a switch, etc, etc, all within a network I access through a ssh server.  I connect and launch screen session and use a bunch of screen windows (or multiple session) to manage all those tasks... then, when I'm heading home and want to pick it all up once I'm there, I disconnect before I leave and reconnect to that same screen session(s) later as if I never left!

Of course screen is all configurable with options for making life a little easier:

#CODE: .screenrc
   #All single lines
   #shutoff the start up message
startup_message off
   #allow a big scroll back buffer so you don't "loose" anything
defscrollback 5000
   #allows screen redraw for some apps like VIM or LESS
altscreen on
   #give me my normal prompt
shell -/bin/bash
   #give me some "tabs" at the bottom of the screen to help me visualize where I am
caption splitonly "%{= wK}%-w%?%F%{= bw}%:%{= Wk}%? %n %t %{-}%+w %-= "
   #same but not just when split
hardstatus alwayslastline "%{= B}%H (ctl-a) %{= r}[ %{= d}%Y-%m-%d %c:%s %{= r}]%{= d} - %{= wk}%-Lw%{= Bw} %n$f*  %t %{-}%+Lw %-= :)"
   #pre-launch screen windows with different options#
   #ssh to my home and launch screen there
screen -t ssh-home 0 ssh -t -D 8080 user@host -p xxxxxx screen -dR Ivan
   #ssh to a VM on my laptop
screen -t ssh-VM 1 ssh -t user@192.168.10.101 screen -dR Ivan
   #ssh to a work system in the lab as a comon host but use my .screenrc config
screen -t ssh-lab 2 ssh -t user@host screen -dR Ivan -c .screenrc.Ivan
   #ssh to a freelance location to work on their systems
screen -t freelance 4 ssh -t user@host screen -dR Service
   #give me a normal shell prompt
screen -t bash 5

So I can just launch screen and let it connect to all the places I "usually" go via ssh.  It could also be running scripts or other apps so learn about the .screenrc file.

SSH tricks

From Commands I Always Forget...

>SSH tricks

ssh is one of the best nerd tools around!  I use it for connecting to my home when on the road and as a poor mans vpn to my home for web browsing:

ssh -DN 8080 user@host -p xxxxx
The -D dynamically links the local port 8080 (this is on my laptop) through ssh to the host (my home server).  The I configure my browser to use a SOCKS proxy on 8080 and BLAM, I'm on my local network surfing the internet or any other web based service I have at my home (XBMC, DD-WRT, development web pages, etc)
The -N means don't bother giving me an interactive shell, meaning, the terminal prompt on my laptop can't use the ssh connection through the terminal (ie, although I have an ssh connection open I can't do normal ssh stuff through this window) which is nice because it keeps the tunnel from closing because of a time-out.

This also works for connecting any to any TCP port within the network of the host (home) so if I want to use VNC I can
ssh user@host -NL 5900:additional_host:5900
The -L is for linking a single port, this passes traffic from my laptop port 5900 through "host" into "additional_host" port 5900 allowing my laptop to VNC to localhost 5900 (screen 0) and see the remote system behind the network accessible via "host".

Another common use is to remotely connect and use screen for continued administration/development

ssh -t user@host -p xxxxx screen -dR Ivan
This tells ssh to make a connection to the host system (home) as "user" (me) on port xxxxxx and launch screen connecting to or creating a session called Ivan.  The -t forces the creation of a "terminal" which means, if you do not have -t having ssh launch some applications fail.  Lots of awesome tricks with ssh, I'll put more in the future.
If you are in need of sending command substitution variables to get the remote system to add the current time to the name of a file that you are creating so need to put an escape character  before the $ or you will get the command substitution of the local system.  [ex: ssh root@ "zip -r /path/to/dir/\$(date +%F-%H_%M)-\$(hostname | grep -i something | awk '{print \$2;}')-logs.zip /path/to/things/to/zip"] [hint: notice the \ in front of all the $'s]

Rsync

From Commands I Always Forget...

>Rsync

rsync is a cool little app that has been around for ages, it allows you to copy stuff from one system to another using ssh.
rsync -avhPn --del -e 'ssh -p xxxxx' --exclude 'Dir_of_BigFiles' ~/Personal/ user@remote_host:Personal/
the -n means "dry-run" so it pretends to copy stuff but doesn't. -a means archive (most likely what you want for files like personal photos), -v for verbose incase something goes wrong, -h for human readable sizes, -P for progress, and --del for delete stuff on the destination that is not on the source (so be careful with paths, hence -n).
   Note: the --exclude directories are paths relative to the files being moved, not from where you execute the command.

I run this on my work laptop to sync the personal files (mostly pictures of my kid) from there to my home server.  My home server is allowing ssh incoming on a high port (to keep the script kiddies away) and I keep forgetting the syntax to rsync over ssh on a non standard port.  Oh, I also exclude the directory with big files (movies) since my bandwidth at home is kinda limited.