Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 40 total  RSS 

Copy Public Key To Host In One Line

ssh username@host "echo `cat ~/.ssh/id_dsa.pub` >> ~/.ssh/authorized_keys"

Using Ruby to list directory names from a remote machine

The first line of code executes an external shell command which retrieves directory names into an array. The second line of code displays each directory making sure to chop the '/', and '\n' from the name.
dir_listing = `ssh 192.168.1.10 'ls */ -d'`
dir_listing.each { |directory| puts directory.chop.chop }

Note: In this example ssh access to the remote machine does not prompt the user for a password as the hosts know each other. see ssh-keygen.

copy files using rsync and ssh

source: http://www.mikerubel.org/computers/rsync_snapshots/#Abstract ; switches: -a = archive mode -e specifies the remote shell to use

rsync -a -e ssh source/ username@remotemachine.com:/path/to/destination/

Compress file in SSH

The below will compress a file or folder in .tar.gz format in SSH.

tar -zcvf <new_tar_filename>.tar.gz <filename/directory>

Mount SSH using MacFUSE

This will also list the voume in the Finder, thanks to the volname option (I think)

mkdir -p ~/mnts/formandfx
sshfs kmarsh@formandfx.com: ~/mnts/formandfx -oping_diskarb,volname=formandfx

Connecting to a local database on a remote machine

I had a MySQL instance on a remote server that had only local access. I wanted to connect to that database via my local copy of CocoaMysql. Using ssh portforwarding you can do it like this:

ssh -N -L <port_number>:127.0.0.1:3306 remote_machine


so you can now open CocoaMysql and connect to localhost, bla, bla but if you use the <port_number> assigned above you should see your remote database.

ssh and scp without typing a password each time

* You can create and exchange ssh keys to avoid repeatedly typing your password.

Source: ssh-keygen -t dsa Type this once per log-in session
Source: Now copy the .ssh/id_dsa.pub file to <Destination>.
Source: scp .ssh/id_dsa.pub userid@<Destination>:tempid we've renamed it tempid.
Destination: cat tempid >> .ssh/authorized_keys
add the contents of tempid to .ssh/authorized_keys.
Destination: rm tempid.

SSH Key Pairs Stuff

//allows for copying of ssh keys

ssh-keygen -t dsa
ssh user@host 'cat >> .ssh/authorizedkeys' < .ssh/id_dsa.pub

Migrate a site to another server

Use the code below in SSH to migrate all the files of a site from one server to another, without having to download/upload them.

wget -rc --level=20 ftp://username:password@olddomain.net/public_html

Migrate a MySQL Database to another server

Use the below in SSH to move a MySQL database between servers. Super handy if the database is larger than 7MB and you can't use phpMyAdmin.

mysqldump -h oldhost -u oldusername -poldpassword olddbname | mysql -h newhost -u newusername -pnewpassword newdbname     


http://wiki.dreamhost.com/index.php/Migrate_MySQL
« Newer Snippets
Older Snippets »
Showing 1-10 of 40 total  RSS