【问题标题】:Need Cygwin expect script to output files based on Date to specified folders需要 Cygwin 期望脚本根据日期将文件输出到指定文件夹
【发布时间】:2013-10-15 18:25:25
【问题描述】:

我刚刚编写了一个脚本来自动将我们的 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"

我仍在学习中,因此我们将不胜感激。谢谢!

【问题讨论】:

  • 好问题:对问题的描述非常清晰,并展示了您到目前为止所做的工作。

标签: bash date for-loop cygwin expect


【解决方案1】:

我会使用一系列目录重命名命令来做到这一点。假设您可以删除“第 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——这会打开详细的调试输出,这样你就可以看到你的模式是如何匹配的。

【讨论】:

  • 非常感谢格伦的回复。我正在尝试上述一些命令。我可以删除“第 4 周”的内容,事实上这就是我想要的。我继续往里面添加了 exp_internal 1 。我会让你知道会发生什么。再次感谢!
  • 还有 Glenn 的问题,对于新手的问题很抱歉,但这应该附加到我的期望脚本的末尾吗?
  • 是的。另外,在发送“退出”后添加expect eof。这将等待 ssh 进程结束。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-20
  • 2020-09-21
  • 1970-01-01
相关资源
最近更新 更多