【问题标题】:ssh to cisco ASA and store output to a filessh 到 cisco ASA 并将输出存储到文件
【发布时间】:2018-02-02 20:32:50
【问题描述】:

我在将 ssh 的 shell 脚本写入 cisco ASA 并将命令输出存储在文本文件中时遇到问题。

1.脚本中不需要密钥交换,因为它不是第一次登录。 2.从我的centOS服务器它应该使用ssh usr@serverip登录到cisco ASA,运行“en”,发送en密码 然后运行一些命令说“显示版本”并将输出存储到我服务器中的文本文件中。我尝试了shell脚本和expect的使用,都没有成功。请帮忙。

提前非常感谢。

【问题讨论】:

  • RANCID (Really Awesome New Cisco confIg Differ) 是一个不错的备份工具,它包括clogin,一个用于访问 Cisco 设备(至少基于 IOS 和 ASA 防火墙)的 Expect 脚本。使用示例:clogin -c "sh version" 10.1.2.3 > result.txt.
  • 我怀疑您被否决了,因为您的问题并不真正符合堆栈溢出准则。尽管人们提供了示例,但请发布您尝试执行的示例脚本,您可能会得到更好的响应。

标签: ssh expect cisco


【解决方案1】:

这是一个用 python 编写的小代码,可以为您完成工作。 Python 默认安装在您的 CentOS 中。只需将文件名中的代码保存为 .py 并使用“python .py”运行它。让我知道这是否有帮助。

import pexpect


try:

hostname= 'yourhostname/ip of firewall'
username= 'your username'
commandTorun = 'Enter your command here'
password= 'your password'
enable= 'your enable password'

ssh = 'ssh ' + username + '@' +hostname

s=pexpect.spawn(ssh)

s.expect('word')
s.sendline(password)
s.expect('>');
s.sendline('en')
s.expect('word')
s.sendline(enable)
s.expect('#')
s.sendline('configure terminal')
s.expect('#')
s.sendline('pager 0')
s.expect('#')
s.sendline(commandTorun)

s.expect('#')
output =  s.before
#You can save the output however you want here. I am printing it to the CLI.

s.sendline('exit')
s.expect('#')
s.sendline('exit')
s.expect(pexpect.EOF)
print output

except Exception, e:
print "The Script failed to login"
print str(e)

【讨论】:

    【解决方案2】:

    我不熟悉 CentOS,但我在 Windows 上使用 Plink(PuTTY 的命令行版本)完成了这项工作。 http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html(编辑:刚刚检查过,它显示 plink 作为 .rpm 文件存在并且在存储库中可用。如果您需要手动编译,则提供的链接也有源代码。)

    选项 -m 允许您指定命令脚本,-ssh 强制使用 ssh 协议,-l 是远程用户,-pw 是远程密码。

    命令如下:

    plink.exe -ssh -l user -pw pass -m "path_to_script/script.txt" ip_of_asa > outputfile.txt
    

    在 script.txt 文件中,您只是一个命令列表,包括启用命令和硬编码密码:

    en
    enable_password
    show ver
     
     
    exit
    

    注意,如果有多个页面,则其中有空格可以获取完整的“显示版本”。输出使用“>”将输出重定向到文件。

    希望这会有所帮助!

    【讨论】:

    • 实际上我正在尝试使用期望来做到这一点,我看到它也在登录并创建日志文件 output.txt,但问题是它是空的。任何人都可以帮我解决贝洛代码有什么问题吗? #!/usr/bin/expect -f #! /bin/bash 设置超时 60 log_user 1 设置主机 ********* 设置密码 ********** 设置启用密码 ********* 设置用户 ***** ** set logfile output.txt spawn ssh $user@$host expect "?assword:" send -- "$password\r" log_user 1 expect "*>" send "en\r" expect "*assword:" send -- "$enablepassword\r" expect "*#" log_file $logfile send -- "show clock\r" #log_file send -- "exit\r"
    【解决方案3】:

    这行得通。

    #!/usr/bin/expect
    set remote_server [lrange $argv 0 0]
    set timeout 10
    spawn ssh -M username@$remote_server
    while 1 {
      expect {
        "no)?"      {send "yes\r"}
        "denied" {
                    log_file /var/log/expect_msg.log
                    send_log "Can't login to $remote_server. Check username and password\n";
                    exit 1
                 }
        "telnet:" {
                    log_file /var/log/expect_msg.log
                    send_log "Can't connect to $remote_server via SSH or Telnet. Something went definitely wrong\n";
                    exit 2
                  }
        "failed" {
                    log_file /var/log/expect_msg.log
                    send_log "Host $remote_server exists. Check ssh_hosts file\n";
                    exit 3
                 }
        timeout {
                    log_file /var/log/expect_msg.log
                    send_log "Timeout problem. Host $remote_server doesn't respond\n";
                    exit 4
                }
        "refused" {
                    log_file /var/log/expect_msg.log
                    send_log "Host $remote_server refused to SSH. That is insecure.\n"
                    log_file
                    spawn telnet $remote_server
                  }
        "sername:" {send "username\r"}
        "assword:" {send "password\r"}
        ">"        {enable}
        "#"        {break}
      }
    }
    send "terminal length 0\r"
    expect "#"
    send "show running-config\r"
    log_file /opt/config-poller/tmp/poller.trash/$remote_server
    expect "#"
    send "exit\n"; exit 0
    

    【讨论】:

    • 不知何故 log_file logfilname 正在创建日志文件,但这不存储命令的输出,如“show running-config”或“show version”......
    • 我的错误,在退出之前缺少最后一个期望“#”...它现在正在工作..非常感谢您的帮助:-)
    【解决方案4】:

    如果您通过本地机器访问路由器,那么您可以创建一个期望脚本并在本地机器中配置 tftp。 脚本会是这样的。

    #!/usr/bin/expect-f
    spawn telnet Router_IP
    expect "word"
    sent "password"
    expect "word"
    sent "copy running-config tftp://$tftp/$config\n\n" #edit this line wrt system's tftp configurations.
    expect "word"
    send "\n"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      相关资源
      最近更新 更多