Hints and tips

These are some helpful hints on various topics.

 

1 2 3 4 5 6

One-liner for transfering SSH public key to remote host

cat ~/.ssh/id_rsa.pub | ssh hostname "cat >> ~/.ssh/authorized_keys"

Rsync over ssh with non-default port

If you use rsync over ssh to a server running on port different from 22, for example 555, use the following format:

rsync -a -e "ssh -p 555" rsyncuser@remoteserver:/data/to/sync /archive/

More on rsync

Mounting Windows shared folder in Linux

We have Windows resource under \\otherhost\share which we want to to see on the Linux machine as /mnt/winshare. Windows credentials are user/pass. So we use:

mkdir /mnt/winshare

mount -t smbfs -o username=user,password="pass" //otherhost/share /hmnt/winshare

or

mount -t cifs -o username=user,password="pass" //otherhost/share /hmnt/winshare

Capturing windows command output directly into clipboard

Windows command prompt is famous for plenty of disadvantages. One very annoying "feature" is that it breaks longer lines and if you want to copy them you need to manually glue them back. Here's one convenient way of capturing command output directly into clipboard: just type
| clip 

after the command. For example
netstat -an | clip

Now you can paste it in your favorite text editor.

Connecting to the console session on Windows 2003 Server

To connect to the console session on Windows Server via Remote Desktop, add /console:
mstsc /console
This is very useful in cases such as "terminal server has exceeded max number of allowed connections" - it gives you additional session to fix the mess.

HTTP headers for disabling browser cache

<?php
    header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
    header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
    header( 'Cache-Control: no-store, no-cache, must-revalidate' );
    header( 'Cache-Control: post-check=0, pre-check=0', false );
    header( 'Pragma: no-cache' );
?>

Prefix each output line with timestamp

Imagine the case: you were writing a program or a script that outputs some information in the console. Just when everything is almost complete you realize that you actually need timestamp on each message output. For example you need to know how long it takes to backup each of your users' home dirs. The solution is to go through all output commands and wrap them in a function that appends timestamps. Believe it or not, there's a lazier way to do this - it's a simple bash one-liner:
 
> ./your_program | while read line; do stamp=`date`; echo "$stamp $line"; done
 

You can try it with
 
> du -sc /home | while read line; do stamp=`date`; echo "$stamp $line"; done

1 2 3 4 5 6