检查Cisco交换机上是否存在文件

时间:2017-05-18 22:51:24

标签: operating-system filesystems python-3.6

我正在尝试检查Cisco交换机的内部闪存:/磁盘上是否存在文件。

switch-2950#dir flash:/
Directory of flash:/

    2  -rwx     3721946  Jul 24 2009 16:17:10 +00:00  c2950-i6k2l2q4-mz.121-22.EA13.bin
    3  -rwx        2035  Mar 01 1993 00:25:01 +00:00  config.text
    5  drwx        4416  Jul 24 2009 16:19:50 +00:00  html
    6  -rwx         556  Mar 01 1993 00:49:35 +00:00  vlan.dat
  335  -rwx         315  Jul 24 2009 17:43:37 +00:00  env_vars
   21  -rwx         112  Jul 24 2009 16:10:20 +00:00  info
   22  -rwx         112  Jul 24 2009 16:20:56 +00:00  info.ver
   23  drwx          64  Mar 01 1993 00:00:11 +00:00  crashinfo
   25  -rwx       13495  May 18 2011 19:57:30 +00:00  config.old
  336  -rwx        3832  Mar 01 1993 00:25:01 +00:00  private-config.text

7741440 bytes total (2124800 bytes free)

vlan.dat显然存在。我可以对它进行操作(例如复制)。

但是,我想在执行之前测试该特定文件是否存在 针对它的行动。

我在尝试:

if os.path.isfile("flash:/vlan.dat"):

但它总是返回False,并且跳过'if'语句中的命令。

我查看了很多帖子,但它们都涵盖了Linux或Windows。我找不到有关思科文件系统的任何信息。

1 个答案:

答案 0 :(得分:0)

我用另一种方式解决了。我做'dir flash:/',然后检查是否存在 'vlan.dat'文件。

today = time.strftime("%x")
timenow = time.strftime("%X")
filename = (hostname + '-' + '%s' + '_' + '%s') % (today, timenow)
filename = filename.replace("/", "-")
filename = filename.replace(":", "-")

ssh_channel.send("dir flash:/" + "\n")
time.sleep(0.3)
outp = ssh_channel.recv(2000)
output = outp.decode("utf-8")

if 'vlan.dat' in output:
    ssh_channel.send("copy flash:/vlan.dat tftp://192.168.1.106/" + filename + ".dat" + "\n")
    time.sleep(0.3)