-2

ssh のシェル スクリプトを cisco ASA に書き込んでコマンド出力をテキスト ファイルに保存するのに問題があります。

1.初回ログインではないため、スクリプトでキー交換は必要ありません。2.centOSサーバーから、ssh usr@serveripを使用してcisco ASAにログインし、「en」を実行し、enパスワードを送信してから、コマンドを実行する必要があります「バージョンを表示」し、出力をサーバーのテキスト ファイルに保存します。シェルスクリプトとexpectの使用の両方を試しましたが、どちらも成功しませんでした。助けてください。

よろしくお願いします。

4

4 に答える 4

2

これは、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)
于 2015-02-20T05:56:32.670 に答える
0

私は CentOS には詳しくありませんが、PuTTY のコマンド ライン バージョンである Plink を使用して Windows でこれを実行しました。 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 ファイルには、enable コマンドとハードコードされたパスワードを含むコマンドのリストが含まれています。

en
enable_password
show ver
 
 
exit

複数のページの場合、完全な「show ver」を取得するためのスペースがそこにあることに注意してください。出力は ">" を使用して、出力をファイルにリダイレクトします。

お役に立てれば!

于 2013-04-19T23:20:08.230 に答える
0

これは機能します。

#!/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
于 2013-04-22T11:13:33.537 に答える