Approach this in either of two ways, depending on the size of your old drive and your new drive. The second is quite involved and requires a lot of technical know how.
Buy a new drive with an enclosure. Replace the failing drive with the new one. Put your old drive in the enclosure, install Mac OS X on the fresh drive and transfer your data from the old drive.
However that is not what you requested.
The second method is to use the dd command and ssh via terminal. The two machines will need to be on the same network, and will need to be able to see each other.
I've looked up both your machines on everymac.com and will assume that your dying drive is 500GB and the new one has 1TB.
You will need more than 600GB of free space on the new machine for this to work and you need to know what you are doing.
You need to download and burn a copy of any selfbooting Linux distribution (I have tested this with an old copy of Ubuntu 12.04 I have lying around).
Once you have the disk burned you need to boot your dying machine with the DVD/CD you just created by holding the alt key while your machine reboots, then selecting the media in your DVD drive to boot from.
You will need to set up your old iMac on your network, I would use cables rather than wifi (because there is less to go wrong and it is easier).
Make sure both your old and new machines can "see" each other via ping.
On your mac open
Configure the IPs:
I used the IP 10.10.0.1 and subnet mask of 255.255.0.0, on the "Linux" mac I used 10.10.0.2 with the same subnet mask. From the command line on either machine you should be able to ping 10.10.0.1 and 10.10.0.2 before you proceed to the next step.
Enable remote login on your new machine.
Find the partition on your old mac you want to copy via Linux command line. This is most probably going to be /dev/sda
You will need to be root on your "Linux" machine so do
From your old machine (Linux) issue the command (via the shell):
dd if=/dev/sda | gzip -1 - | ssh user@hostname dd of=image.gz
This assumes that the drive on your old machine you want to copy is called
/dev/sda, you will need to replace user with your username on your new machine, and hostname is the IP address of your new machine.
The command will take a long time as it copies your old disk perfectly to a new image on your working iMac.
Once you have replaced the old disk on the failing machine you can restore the image by booting Linux again and issuing the following command:
ssh user@hostname dd if=image.gz | gunzip -1 - | dd of=/dev/sda
This will take the created image and put it on your new harddrive.