<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "自動備份 Cisco 3750G-24TS-S1U 網路設定檔"]]></title>
		<link>https://forum.andowson.com/posts/list/26.page</link>
		<description><![CDATA[Latest messages posted in the topic "自動備份 Cisco 3750G-24TS-S1U 網路設定檔"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>自動備份 Cisco 3750G-24TS-S1U 網路設定檔</title>
				<description><![CDATA[ 由於公司資安規定需要定期備份網路設備的網路設定檔，如果每個月都要進到console介面去操作一次，輸入一堆指令也是滿花時間的； 
<br>
另外一個作法是登入命令列界面，然後手動去下copy startup-config tftp://${tftp.host} 這個指令，雖然是比較快速了些，但總是感覺多一件事情在身上，到時要是別的事忙起來，還是怕會忘記。 
<br>
<br>
由於懶惰是人的天性，所以總是會想如何把事情自動化，於是我就想用ant的telnet task來自動登入Cisco Switch並執行copy startup-config指令，方法大致如下： 
<br>
1.在備份用的主機上架設TFTP Server 
<br>
2.下載及安裝JRE、Ant 1.7.1及telnet task所需之commons-net套件。 
<br>
3.編輯一個build.xml及一個build.properties檔案 
<br>
4.執行 ant cisco看是否可正常備份 
<br>
5.寫一個shell script檔將該ant指令包裝起來 
<br>
6.設定/etc/crontab自動執行該shell script檔 
<br>
<br>
由於手邊只有Linux主機可以當備份主機使用，底下再簡單說明一下上述步驟 
<br>
1.安裝TFTP Server可以參考這篇 
<br>
[url]http://www.andowson.com/posts/list/544.page[/url]，安裝完成後，手動建立一個/tftpboot的目錄，並把tftpboot的根目錄(Base Directory)設定777 
<br>
<br>
2.將把下載來的ant zip檔解壓縮到/root/cisco底下，設定環境變數ANT_HOME指向安裝的目錄，並在PATH變數加上$ANT_HOME/bin。然後把commons-net的jar檔放到ANT_HOME的lib目錄下。 
<br>
<br>
3.可以參考底下兩個範例，將這兩個檔案存到/root/cisco目錄下 
<br>
build.xml範例如下： 
<br>
[code=xml] 
<br>
&lt;?xml version="1.0" encoding="Big5" ?&gt; 
<br>
<br>
&lt;project name="backup" default="cisco" basedir="."&gt; 
<br>
 &lt;description&gt;Network Switch Config Backup&lt;/description&gt; 
<br>
<br>
 &lt;!-- Enable access to build.properties variables --&gt; 
<br>
 &lt;property file="build.properties" /&gt; 
<br>
 &lt;property name="backup.root.dir" value="/root/cisco" /&gt; 
<br>
 &lt;property name="cisco.backup.dir" value="${backup.root.dir}/${cisco.ip}" /&gt; 
<br>
 &lt;property name="tftp.root" value="/tftpboot" /&gt; 
<br>
<br>
 &lt;!-- Init --&gt; 
<br>
 &lt;target name="init" description="Create backup directory"&gt; 
<br>
 &lt;tstamp&gt; 
<br>
 &lt;format property="TODAY" pattern="yyyyMMdd" /&gt; 
<br>
 &lt;/tstamp&gt; 
<br>
 &lt;property name="cisco.cfg" value="cisco_${TODAY}.cfg" /&gt; 
<br>
 &lt;mkdir dir="${backup.root.dir}" /&gt; 
<br>
 &lt;mkdir dir="${backup.root.dir}/${cisco.ip}" /&gt; 
<br>
 &lt;touch file="${tftp.root}/${cisco.cfg}"/&gt; 
<br>
 &lt;chmod file="${tftp.root}/${cisco.cfg}" perm="ugo+rwx"/&gt; 
<br>
 &lt;/target&gt; 
<br>
<br>
 &lt;!-- Backup cisco Config --&gt; 
<br>
 &lt;target name="cisco" depends="init" description="Backup cisco config"&gt; 
<br>
 &lt;property name="cisco.cfg" value="cisco_${TODAY}.cfg" /&gt; 
<br>
 &lt;telnet server="${cisco.ip}"&gt; 
<br>
 &lt;read timeout="5"&gt;Username: &lt;/read&gt; 
<br>
 &lt;write&gt;${cisco.username}&lt;/write&gt; 
<br>
 &lt;read timeout="5"&gt;Password: &lt;/read&gt; 
<br>
 &lt;write&gt;${cisco.password}&lt;/write&gt; 
<br>
 &lt;read timeout="5"&gt;&gt;&lt;/read&gt; 
<br>
 &lt;write&gt;enable&lt;/write&gt; 
<br>
 &lt;read timeout="5"&gt;Password: &lt;/read&gt; 
<br>
 &lt;write&gt;${enable.password}&lt;/write&gt; 
<br>
 &lt;read timeout="5"&gt;#&lt;/read&gt; 
<br>
 &lt;write&gt;copy startup-config tftp://${tftp.host}/${cisco.cfg}&lt;/write&gt; 
<br>
 &lt;read timeout="30"&gt;Address or name of remote host [${tftp.host}]?&lt;/read&gt; 
<br>
 &lt;write&gt;${tftp.host}&lt;/write&gt; 
<br>
 &lt;read timeout="30"&gt;Destination filename [${cisco.cfg}]?&lt;/read&gt; 
<br>
 &lt;write&gt;${cisco.cfg}&lt;/write&gt; 
<br>
 &lt;read timeout="30"&gt;bytes/sec)&lt;/read&gt; 
<br>
 &lt;write&gt;exit&lt;/write&gt; 
<br>
 &lt;/telnet&gt; 
<br>
 &lt;move file="${tftp.root}/${cisco.cfg}" todir="${cisco.backup.dir}" /&gt; 
<br>
 &lt;/target&gt; 
<br>
<br>
&lt;/project&gt; 
<br>
[/code] 
<br>
<br>
build.properties範例如下：請視您真正的情況修改對應的參數值，下列例子中我們的TFTP主機IP是172.16.38.1，Cisco Switch的IP是172.16.38.254。 
<br>
[code] 
<br>
tftp.host=172.16.38.1 
<br>
cisco.ip=172.16.38.254 
<br>
cisco.username=admin 
<br>
cisco.password=admin 
<br>
enable.password=cisco 
<br>
[/code] 
<br>
<br>
4.開啟一個shell命令列視窗，然後切換到build.xml及build.properties所存放的目錄/root/cisco下，接著執行 
<br>
ant cisco 
<br>
<br>
5.將上述動作編輯成一個backup_config.sh 
<br>
backup_config.sh: 
<br>
[code=bash] 
<br>
#!/bin/bash 
<br>
ANT_HOME=/root/cisco/apache-ant-1.7.1 
<br>
JAVA_HOME=/usr/java/latest 
<br>
PATH=$PATH:$ANT_HOME/bin:$JAVA_HOME/bin 
<br>
ant cisco 
<br>
[/code] 
<br>
<br>
6.編輯 /etc/crontab，並設定backup_config.sh執行的頻率。 
<br>
# 每個月備份網路設備的網路設定 (2011.11.18 by Andowson) 
<br>
30 5 23 * * root /root/cisco/backup_config.sh &gt; /var/log/backup_config.log 2&gt;&amp;1]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/566/1178.page</guid>
				<link>https://forum.andowson.com/posts/preList/566/1178.page</link>
				<pubDate><![CDATA[Fri, 18 Nov 2011 19:19:21]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>