ssh到cisco ASA并将输出存储到文件中



我在为ssh编写shell脚本并在文本文件中存储命令输出时遇到了麻烦。

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

提前感谢。

这是一个用python编写的小代码,为您完成工作。默认情况下,Python安装在CentOS中。只需将代码保存在文件名中,并以"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)

我不熟悉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

注意,这里有空格来获得完整的"show ver"如果有多个页面。输出使用">"将输出重定向到file.

希望这对你有帮助!

可以了

#!/usr/bin/expect
set remote_server [lrange $argv 0 0]
set timeout 10
spawn ssh -M username@$remote_server
while 1 {
  expect {
    "no)?"      {send "yesr"}
    "denied" {
                log_file /var/log/expect_msg.log
                send_log "Can't login to $remote_server. Check username and passwordn";
                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 wrongn";
                exit 2
              }
    "failed" {
                log_file /var/log/expect_msg.log
                send_log "Host $remote_server exists. Check ssh_hosts filen";
                exit 3
             }
    timeout {
                log_file /var/log/expect_msg.log
                send_log "Timeout problem. Host $remote_server doesn't respondn";
                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 "usernamer"}
    "assword:" {send "passwordr"}
    ">"        {enable}
    "#"        {break}
  }
}
send "terminal length 0r"
expect "#"
send "show running-configr"
log_file /opt/config-poller/tmp/poller.trash/$remote_server
expect "#"
send "exitn"; exit 0

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

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

最新更新