如何使用Expect与多个具有列表和更改密码的文件进行Telnet

时间:2016-03-09 00:41:31

标签: while-loop expect telnet cisco

我是脚本新手。期待似乎是我正在做的最好的。我让它通过我的device-ip.txt文件列表运行,我能够从其他列表中获取一个密码来登录。

我无法弄清楚如何让它通过所有文件列表,就像我的device-ip.txt一样。我猜它与while命令有关。我想要它做什么:

  • 从device-ip.txt抓取第一个IP地址到Telnet。
  • 使用password3-list.txt中的第一个密码进行访问。
  • 对于启用模式,请使用enable-password4-list.txt。
  • 中的第一个密码
  • 要更改密码,请使用enable-password-list.txt中的第一个密码。
  • vty和con密码将相同,因此需要从password-list.txt中获取。

在所有这一切发生之后,我希望它能够通过并执行相同的操作,但是在每个文件列表的第二行上依此类推,直到所有列表都已完成。

这是我的Expect脚本,只适用于device-ip.txt:

#!/usr/bin/expect -f

# ---------------- variables ---------------- #

set timeout 2
set argv3 [open "password3-list.txt" r]
set password3 [gets $argv3]
set argv4 [open "enable-password4-list.txt" r]
set password4 [gets $argv4]
set argv1 [open "password-list.txt" r]
set password [gets $argv1]
set argv2 [open "enable-password-list.txt" r]
set password2 [gets $argv2]
set argv0 [open "device-ip.txt" r]
set ip [gets $argv0]
while {$ip > 0 }  { 

# ---------------- telnet ---------------- # 

spawn telnet $ip
expect "*Password:" 
send "$password3\n"
expect "*>"


# ---------------- enable mode ---------------- #

send "en\n"
expect "Password:" 
send "$password4\n"
expect "*#"

# ---------------- configuration mode ---------------- #

send "config t\n"
expect "*(config)#"

# ---------------- password change ---------------- #

send "enable secret $password2\n"
expect "*(config)#"
send "line vty 0 15\n"
expect "*(config-line)#"
send "password $password\n"
expect "*(config-line)#"
send "login\n"
expect "*(config-line)#"
send "line con 0\n"
expect "*(config-line)#"
send "password $password\n"
expect "*(config-line)#"
send "end\n"
expect "*#"

# ---------------- write to memory and backup ---------------- #

send "wr\n"
expect "*#"
send "backup\n"
expect "*]?"
send "\n"
expect "*]?"
send "\n"
expect "*#"

# ---------------- exit ---------------- #

send "exit\n"
expect ":~\$"
set ip [gets $argv0]
}
 close $argv0

如果有人可以提供帮助,我们将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

我找到了解决问题的方法。以下是感兴趣的人的更新脚本:

#!/usr/bin/expect -f

# ---------------- variables ---------------- #

set timeout 2
set argv3 [open "telnet-login.txt" r]
set telnetlogin [gets $argv3]
set argv4 [open "enable-login.txt" r]
set enablelogin [gets $argv4]
set argv1 [open "password-vty-con0.txt" r]
set passwordvtycon0 [gets $argv1]
set argv2 [open "enable-secret.txt" r]
set enablesecret [gets $argv2]
set argv0 [open "device-ip.txt" r]
set ip [gets $argv0]
while {$ip > 0 && $passwordvtycon0 > 0 && $telnetlogin > 0 && $enablelogin > 0 && $enablesecret > 0 } { 

# ---------------- telnet ---------------- # 

    spawn telnet $ip
    expect "*Password:" 
    send "$telnetlogin\n"
    expect "*>"


# ---------------- enable mode ---------------- #

    send "en\n"
    expect "Password:" 
    send "$enablelogin\n"
    expect "*#"

# ---------------- configuration mode ---------------- #

    send "config t\n"
    expect "*(config)#"

# ---------------- password change ---------------- #

    send "enable secret $enablesecret\n"
    expect "*(config)#"
    send "line vty 0 15\n"
    expect "*(config-line)#"
    send "password $passwordvtycon0\n"
    expect "*(config-line)#"
    send "login\n"
    expect "*(config-line)#"
    send "line con 0\n"
    expect "*(config-line)#"
    send "password $passwordvtycon0\n"
    expect "*(config-line)#"
    send "end\n"
    expect "*#"

# ---------------- write to memory and backup ---------------- #

    send "wr\n"
    expect "*#"
    send "backup\n"
    expect "*]?"
    send "\n"
    expect "*]?"
    send "\n"
    expect "*#"

# ---------------- exit ---------------- #

    send "exit\n"
    expect ":~\$"
    set ip [gets $argv0]
    set telnetlogin [gets $argv3]
    set enablelogin [gets $argv4]
    set passwordvtycon0 [gets $argv1]
    set enablesecret [gets $argv2]
 }
 close $argv0
 close $argv3
 close $argv4
 close $argv1
 close $argv2