2

我刚刚编写了一个脚本来自动将我们的 Cisco 设备备份到指定文件夹。/home/myusername/备份

我想要完成的是让脚本每周运行一次,将输出转储到 Backups 文件夹中,而不是覆盖它们,而是将旧配置存储在单独的目录中。如果可能,我们希望保留 1 个月的备份。当脚本运行并写入新输出时,前一个副本将沿文件树向下移动。文件树看起来像这样:

/Backups(newest backup)
--1_Week_Old
--2_Weeks_Old 
--3_Weeks_Old
--4_Weeks_Old

这是我编写的现有脚本的示例。它运作良好,但我知道可以通过一些获得的经验来缩短它。我是脚本新手,所以轻松一点....

#!/usr/bin/expect

set timeout 20

# These are all the IP's:
set ip1 "X.X.X.X"

#These are all the hostnames:
set hostname1 "ASA_Firewall"

#These are the usernames
set username   "RickyBobby"

# These are all the passwords:
set password         "xxxxxxx"
set enableasa        "xxxxxxxxx"

#This is the ASA Firewall - Point TFTP Directory to C:\cygwin\home\RickyBobby\Backups
spawn ssh $ip1
expect "password:"
send "$password\r"

  expect ">" {
    send "en\n"
    expect "Password:"
    send "$enableasa\r"
  }
expect "#"
send "copy run tftp://X.X.X.X/Backups/$hostname1-confg\r"
expect "Source filename?"
send "\r"
expect "Address or name of remote host?"
send "X.X.X.X\r"
expect "Destination filename?"
send "\r"
expect "#"
send "exit\r"

我仍在学习过程中,所以任何帮助将不胜感激。谢谢!

4

1 回答 1

0

我会用一系列目录重命名命令来做到这一点。假设您可以删除“第 4 周”目录中的所有内容:

cd /home/username/Backups
file delete -force ./4_Weeks_Old
file rename 3_Weeks_Old 4_Weeks_Old
file rename 2_Weeks_Old 3_Weeks_Old
file rename 1_Week_Old  2_Weeks_Old 

# move files in Backups to 1 week dir
file mkdir 1_Week_Old
file rename -- {*}[glob -types f -- *] 1_Week_Old

如果您的 expect 版本的 Tcl 版本早于 8.5 版,您必须使用

for file [glob -types f -- *] {
    file rename $f 1_Week_Old/$f
}

或丑陋的

set cmd [linsert [glob -types f -- *] 0 file rename --]
eval [lappend cmd 1_Week_Old]

我使用--以防碰巧有一个以破折号开头的文件名,因此该文件名将被视为文件名而不是选项。

您的期望脚本看起来不错。一个提示:在开发一个期望脚本时,要么使用“expect -d”运行它,要么在开头附近添加这一行:exp_internal 1-- 这会打开详细的调试输出,这样你就可以看到你的模式是如何匹配的。

于 2013-10-16T01:36:59.020 回答