Rancid+Subversion (SVN)管理Cisco配置

Rancid+Subversion (SVN)管理Cisco配置(1)

Ranci全稱爲Really Awesome New Cisco config Differ是可以與CVS(Concurrent Version System)Subversion (SVN) 協同工作來監控Cisco設備的配置、軟件包括硬件變化的工具。
這篇文章主要來介紹用Rancid來做Cisco路由、交換機配置文件變更控制。軟件可從這裏獲得:目前正式版本2.3.1。另外兩個alpha版本rancid-2.3.2a7.tar.gz 和 rancid-wcgallar.tar.gz
  • 安裝SVN
這個依發行版本而定,CentOS 5 爲例,系統默認已經安裝了SVN
例如在Fedora下則:
$ yum install subversion
在Debian下則:
$ sudo apt-get install subversion
進入你保存的位置如 ‘cd /usr/ports/devel/subversion
$ make install
或者
安裝過程很快很簡單。
  • 配置SVN
創建用戶rancid以及一個SVN庫(假設路徑爲/home/svn/cisco)
$ useradd -g netadm -c “Networking Backups” -d /usr/local/rancid rancid         #創建用戶rancid 屬於netadm 這個組並且指定用戶主目錄爲/usr/local/rancid,此處與後文配置rancid有關聯。
$ svnadmin create /home/svn/cisco 
 
啓動svn服務 
$ svnserve -d -r /home/svn/cisco
修改/home/svn/cisco下的conf/svnserve.conf,改爲
[general]
anon-access = none # 不允許匿名登錄
auth-access = write # 可以用戶讀寫
password-db = passwd
修改文件passwd,增加用戶(假設用戶和密碼均爲rancid),如
[users]
rancid = rancid
  • 用戶svn+ssh訪問
因爲passwd文件中的密碼爲明文處於安全性考慮使用svn+ssh的訪問模式。
1.安裝OpenSSH。CentOS 5 已經缺省安裝。
2.用cisco用戶登錄,生成密鑰對。
$ ssh-keygen -b 1024 -t dsa -N passphrase -f mykey #認證方式利用DSA公開密匙加密算法進行加密,兩把密鑰的配對認證方式使服務器運作更安全。其中passphrase爲密鑰密碼,必須修改成你需要的密碼;mykey爲文件名。
這時生成兩個文件私鑰mykey和公鑰mykey.pub。
3.$ cd /home/cisco
$ mkdir .ssh
$ cp mykey.pub /home/cisco/.ssh/authorized_keys #拷貝公鑰到.ssh/authorized_keys,文件名必須是authorized_keys,以上針對服務器端。
4.啓動svn+ssh服務
$ svnserve -t -r ~/cisco &
5.私鑰mykey根據需要轉移到你的客戶端:
如果是linux客戶端,需要將mykey放置在登陸用戶家目錄的.ssh/目錄下面。
如果是Windows客戶端下載putty,下載這幾個執行文件”Putty”, “Plink”, “Pageant” and “Puttygen”
下載網址:[url]http://www.chiark.greenend.org.uk/~sgtatham/putty/[/url]
下載到如    E:\Program Files\putty
6.轉換私鑰格式
1) 將mykey拷貝到windows客戶端,雙擊運行puttygen。
2) 選擇菜單conversions->Import Key,選擇文件mykey。
點擊按鈕Save private key,保存文件名爲mykey.PPK。
7.在putty裏創建session,如下
Session->Host Name: IP地址
Session->Protocol: SSH
Session->Saved Sessions: MyConnection
SSH->Prefered SSH Protocol version: 2
SSH->Auth->Private Key file for auth: E:\Program Files\putty\mykey.ppk,可以替換爲實際目錄。
測試MyConnection連接是否成功。
8.TortoiseSVN中測試SSH
1) 鼠標右鍵選擇TortoiseSVN->RepoBrowser
2) 輸入URL
svn+ssh://svn@MyConnection/home/svn/svnroot
3) 會提示輸入密碼兩次,每進入一個目錄都需要輸入兩次密碼。
9.方法二。自動登錄用戶名
putty的MyConnection設置裏connection->Data->Auto Login username: svn
svn+ssh://MyConnection/home/svn/svnroot
10.方法三。不使用putty的session。
1) 設置TortoiseSVN->Settings->Network->SSH client: E:\Program Files\TortoiseSVN\bin\TortoisePlink.exe
2) 運行pageant,點擊右鍵->Add Key,將私鑰文件加入。
3) svn+ssh://cisco@svn服務器/home/svn/cisco
安裝、配置完SVN之後,我們開始完成剩下的步驟:
  • 安裝Rancid
1.創建以下Rancid目錄,(在上一個主題裏面已經創建了用戶Rancid並且屬於組netadm)並且下載Rancid 軟件包到該目錄:

  #mkdir /usr/local/rancid/tar
  # cd /usr/local/rancid/tar
  # wget [url]ftp://ftp.shrubbery.net/pub/rancid/rancid-2.3.2a2.tar.gz[/url]
–01:14:26– [url]ftp://ftp.shrubbery.net/pub/rancid/rancid-2.3.2a2.tar.gz[/url]
  => `rancid-2.3.2a2.tar.gz’



100%[==============================>] 280,435 153.28K/s
01:14:58 (152.78 KB/s) - `rancid-2.3.2a2.tar.gz' saved [280,435]
2.解包編譯Rancid

# tar -xvzf rancid-2.3.2a2.tar.gz
rancid-2.3.2a2/bin/Makefile.am
rancid-2.3.2a2/bin/Makefile.in
rancid-2.3.2a2/bin/alogin.in
...
...
...
rancid-2.3.2a2/man/lg.conf.5.in
rancid-2.3.2a2/man/rancid.conf.5.in
rancid-2.3.2a2/man/lg_intro.1.in

3.進入壓縮後的目錄,該目錄下面有一個READMEt提供很詳細的安裝說明,可以看一下。這裏我採用默認的安裝路徑進行安裝。

# cd rancid-2.3.2a2
[root@test rancid-2.3.2a2]#
[root@test rancid-2.3.2a2]# less README

4. 開始編譯Rancid 包,–prefix 是用來設置安裝路徑的,如果不設置–prefix 那麼就按照默認的安裝路徑來安裝Rancid. 我們的安裝路徑是/usr/local/rancid/ ,是Rancid 這個用戶的家目錄.

[root@test rancid-2.3.2a2]# ./configure –prefix=/usr/local/rancid/
checking for a BSD-compatible install… /usr/bin/install -c
checking whether build environment is sane… yes
checking for gawk… gawk



config.status: creating include/config.h
config.status: include/config.h is unchanged
config.status: executing depfiles commands
[root@test rancid-2.3.2a2]#

5.正式開始安裝

[root@test rancid-2.3.2a2]# make install
Making install in .
gmake[1]: Entering directory `/usr/local/rancid/tar/rancid-2.3.2a2′
gmake[2]: Entering directory `/usr/local/rancid/tar/rancid-2.3.2a2′
gmake[2]: Nothing to be done for `install-exec-am’.
test -z “/usr/local/rancid//share/rancid” || mkdir -p — “/usr/local/rancid//share/rancid”



/usr/bin/install -c ‘downreport’ ‘/usr/local/rancid//share/rancid/downreport’
gmake[2]: Leaving directory `/usr/local/rancid/tar/rancid-2.3.2a2/share’
gmake[1]: Leaving directory `/usr/local/rancid/tar/rancid-2.3.2a2/share’
[root@test rancid-2.3.2a2]#

6.拷貝實例password文件cloginrc.sample 爲 /usr/local/rancid/.cloginrc.

[root@test rancid-2.3.2a2]# cp cloginrc.sample /usr/local/rancid/.cloginrc
[root@test rancid-2.3.2a2]#

7.由於這個password文件的密碼是明文形式保存的,出於安全性考慮設定該password文件的訪問權限,對rancid和netadm組成員可讀,另外改變rancid目錄的ownership和權限.

[root@test rancid-2.3.2a2]# chmod 0640 /usr/local/rancid/.cloginrc
[root@test rancid-2.3.2a2]# chown -R rancid:netadm /usr/local/rancid/
[root@test rancid-2.3.2a2]# chmod 770 /usr/local/rancid/
  • 配置Rancid
1.配置rancid.conf 文件,在例子當中我們創建一個Switch的設備組,這樣獲得的Cisco配置文件會被保存在usr/local/rancid/var/Switch目錄下面。並且需要在這個文件中指定管理方式爲:SVN,因爲在老的版本中只支持CVS模式,其他不需要設置,Rancid會自動切換保存到SVN的目錄中去。

[root@bigboy rancid-2.3.2a2]# vi /usr/local/rancid/etc/rancid.conf
#
# Sample rancid.conf
#
LIST_OF_GROUPS=”Switch”#這裏可以批量創建多個組,中間用空格分開
FILTER_PWDS=NO; export FILTER_PWDS
NOCOMMSTR=NO; export NOCOMMSTR

2.一下步驟需要su到rancid用戶下進行。

[root@test rancid-2.3.2a2]# su - rancid

3.執行rancid-cvs 命令可以自動創建目錄 /usr/local/rancid/var/CVS/Switch 並且生成相應的數據庫文件設備清單文件等等。在執行rancid-run 之前需要修改router.db文件指定設備的地址等等.

[rancid@test ~]$ /usr/local/rancid/bin/rancid-cvs
No conflicts created by this import
cvs checkout: Updating Switch
cvs checkout: Updating Switch/configs
cvs add: scheduling file `router.db’ for addition
cvs add: use ‘cvs commit’ to add this file permanently
RCS file: /usr/local/rancid/var/CVS/Switch/router.db,v
done
Checking in router.db;
/usr/local/rancid//var/CVS/networking/router.db,v <– router.db
initial revision: 1.1
done
[rancid@test ~]$

4.配置router.db文件

vi /local/usr/rancid/var/CVS/Switch/router.db
192.168.1.1:cisco:up #router.db 的格式爲 dns-name-or-ip-address:device-type:status,我們舉例一個地址爲192.168.0.1的交換機

5.配置.clogin.rc文件

vi /local/usr/rancid/.clogin.rc
#
# Sample .clogin.rc file
#
####################################################################
#
# Device 192.168.1.16 has a unique username and password, but
# doesn't logins do not get the enable prompt.
#
# If the device prompts for a username, Rancid will use the Linux
# "rancid" username and the first password in the list. If only a
# login password is requested, rancid uses the first password in the
# list. The second password is the "enable" password.
#
####################################################################
add password 192.168.0.1 {telnet-password} {enable-password}####################################################################
#
# Devices with DNS names ending in my-web-site.org in the router.db
# file or beginning with 172.16. have a different set of passwords.
#
# If the device prompts for a username, Rancid will use the Linux
# "rancid" username and the first password in the list. If only a
# login password is requested, rancid uses the first password in the
# list. The second password is the "enable" password.
#
####################################################################
add password *.my-web-site.org {telnet-password} {enable-password}
add password 172.16.* {telnet-password} {enable-password}
####################################################################
#
# Everything else uses these passwords. Rancid will attempt to use
# telnet then SSH for logins
#
####################################################################
add password * {telnet-password} {enable-password}
add method * telnet ssh
  • 簡單測試
1.執行clogin可以用來測試連接是否成功:

[rancid@test ~]$ bin/clogin 192.168.0.1
192.168.0.1
spawn telnet 192.168.0.1
Trying 192.168.0.1…
Connected to (192.168.0.1).
Escape character is ‘^]’.
User Access VerificationPassword:
Type help or '?' for a list of available commands.
pixfirewall> enable
Password: ********
pixfirewall#
pixfirewall# exit
Logoff
Connection closed by foreign host.
[rancid@test ~]$
2.執行rancid-run,和rancid-cvs可以用來測試備份是否成功:

[rancid@test ~]$ bin/rancid-run
[rancid@test ~]$ bin/rancid-cvs

然後你可以去尋找備份文件是否產生。
  • 添加到crontab使之週期性執行比較備份

[rancid@test ~]$ crontab -e
#
# Rancid user's crontab file
#
# Run config differ hourly
1 * * * * /usr/local/rancid/bin/rancid-run
# Clean out config differ logs
50 23 * * * /usr/bin/find /usr/local/rancid/var/logs -type f -mtime +2 -exec rm {} \;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章