inverminx
inverminx

Reputation: 65

Create a script that write a local image file to override hard drive on a remote server using dd and netcat

I am struggling for a while trying to create a script that write a local image file to override hard drive on a remote server. for that i am trying to use Linux dd over netcat with gzip compression. will ssh a remote server, execute a remote dd over netcat command for listening on a specific port, and then launch a command to write an image for this remote server. Im not sure why it is not working for me, I have many assumptions and I tried to do it on many ways, including running remote scripts on the background, or having the ssh session itself on the background - but it does not work for me from within a script. the commands i am trying to run:

  1. ssh remote server:

    ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i id_rsa (SERVER_IP)
    
  2. on the remote server start lisen session of dd over nc on port 9023 and decompress using gunzip:

    /bin/nc -l -p 9023|/bin/gunzip -c|/bin/dd bs=64k of=/dev/sda &
    
  3. exit to the main server and execute:

    dd if=/var/tmp/ADT/Server-full/image.gz bs=64k |pv|nc (SERVER_IP) 9023
    

When trying to run the commands one by one it works and the dd sessions is working. when trying to run it from a script the dd session hangs immediately.

Upvotes: 1

Views: 335

Answers (1)

janos
janos

Reputation: 124656

You can redirect local input through a compressed ssh session, and use that input on the other side. You can do this directly without netcat:

ssh -C user@server 'dd of=/dev/sda' < /path/to/local.image

Add the other necessary options you need for ssh and dd.

The CompressionLevel option in man ssh should be interesting too for your use case.

Upvotes: 1

Related Questions