歡迎您光臨本站 註冊首頁

使用netcat命令網路同步複製系統

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  本記錄思想是通過乙太網絡,配合簡單的3個命令,實現把配置好了的完整系統複製到其他機器。以便於區域網上的快速安裝,並減少單台機器系統配置的過程。

使用了3個命令:nc,dump,restore。

代碼:
netcat 簡稱 nc. Netcat 是一把非常簡單易用的基於 TCP/IP 協議(C/S模型的)的「瑞士軍刀」(man裡面,第一句就是這樣說的),它能允許我們通過網路創建管道(pipe)。

dump 和 restore 是用於備份和恢復系統的常用命令。位於dump包中。可以apt-get install dump。


實驗的目的:我們將源機器的根分區,通過 dump/restore 和 nc 來傳送到目標機器的指定分區。 先實驗將源機器的/var/cache/apt/archives/複製到目標機器。

1:在接收端(目標電腦),創建一個 netcat 的監聽常式(-l),這個監聽常式將管道輸出到 restore。 把接受到的數據寫到目標機器。
nc -l -p 2000 -q 1 | restore -r -f -

這裡-p是建立一個埠。-q 選項是讓 nc 在到達文件結束(EOF)時延時n秒后,自動停止運行。

2:在 源電腦, 創建另一個 netcat 的常式,這個常式將它從管道里得到的輸入(也就是dump備份的數據)發給目標電腦。
dump -0 -f - /dir | nc <target-ip> 2000

這裡 target-IP 是目標電腦的 IP 地址。 2000是埠號。

以下是man裡面的原版說明,可以參考。
代碼:
restore說明:
 -f file
    Read  the  backup  from  file; file may be a special device file like /dev/st0 (a tape drive), /dev/sda1 (a disk drive), an ordinary file, or - (the standard input). If the name of the file isof the form host:file or user@host:file, restore reads from  the named file on the remote host using rmt(8).

 -r     Restore (rebuild) a file system. The target file  system  should be made pristine with mke2fs(8), mounted, and the user cd』d into the pristine file system before starting the restoration of  the initial  level  0  backup. If the level 0 restores successfully, the -r flag may be used to  restore  any  necessary  incremental backups on top of the level 0. The -r flag precludes an interac? ive file extraction and can be detrimental to one』s health (not to mention the disk) if not used carefully. An example:

                     mke2fs /dev/sda1
                     mount /dev/sda1 /mnt
                     cd /mnt
                     restore rf /dev/st0

   Note  that  restore  leaves  a  file restoresymtable in the root directory  to  pass  information  between  incremental   restore passes.   This  file should be removed when the last incremental has been restored.


代碼:
dump說明:
-f file
    Write the backup to file; file may be a special device file like /dev/st0 (a tape drive), /dev/rsd1c (a floppy  disk  drive),  an ordinary  file,  or - (the standard output). Multiple file names may be given as a single argument separated by commas. Each file will  be  used  for  one dump volume in the order listed; if the dump requires more volumes than the number of names  given,  the last file name will used for all remaining volumes after prompt?ing for media changes. If the name of the file is  of  the  form host:file or user@host:file dump writes to the named file on the remote host (which should already exist, dump doesn』t  create  a new  remote  file)  using  rmt(8).  The default path name of the remote rmt(8) program is /etc/rmt; this can be overridden by the environment variable RMT.

[-level#]


代碼:
nc - TCP/IP swiss army knife說明:
       -l           listen mode, for inbound connects
       -n           numeric-only IP addresses, no DNS
       -o file      hex dump of traffic
       -p port      local port number  (port  numbers  can  be  individual  or ranges: lo-hi [inclusive])
       -q seconds   after  EOF  is detected, wait the specified number of seconds and then quit.


-------------------------
實驗結果,數據送傳可以,只是目標路徑有些偏差。2邊執行了
代碼:
sudo dump -0 -f - /var/cache/apt/archives/ | nc 10.23.1.100 2000
~$ nc -l -p 2000 -q 1 | restore -r -f -


結果數據全部傳送到了~/var/cache/apt/archives/。
當時考慮失誤,安裝目標機器系統時,圖簡單,安裝時選了格式全盤,安裝的server,然後安裝了dump包。結果搞得沒有空閑分區測試。直接根目錄對根目錄傳送,提示失敗,被中斷。想來也想得到。掛載了並且在運行中的根目錄怎麼會讓你覆蓋哦。

總體來說,實驗還是成功的。數據傳送正常。以後就只要找個帶了nc/dump/restore命令的live cd。就可以在裸機或者其他任何有空間的機器上,複製其他機器上配置好了的linux系統了。我想乙太網絡的速度應該比usb硬碟之類的要好得多。 省去了獨立安裝系統時,系統檢測,解包,下載包,配置軟體的時間,這些也是佔用很長的時間的。

忘記了,複製以後,需要按照機器設置,修改/etc/fstab(分區結構), /etc/network/interfaces(網路ip,dhcp的就可以不要修改), /boot/grub/menu.lst(多數不要修改,除開目標機器有其他不同的系統), 然後就是顯卡和音效卡(這個我以前寫過),硬體的驅動。


[火星人 ] 使用netcat命令網路同步複製系統已經有522次圍觀

http://coctec.com/docs/net/show-post-68403.html