以下のスクリプトは、このスクリプトを実行しているサーバー上のファイルにデータを保存します。ファイル名は routeroutput になります。以下のコードにスイッチ IP、パスワード、有効化パスワードを入力し、Python を使用してサーバーから実行するだけです。
pexpect という追加モジュールが必要です。ここからダウンロードしてインストールできますhttps://pypi.python.org/pypi/pexpect/
import pexpect
try:
switchIP= 'x.x.x.x'
switchPassword = 'your-switch-password'
switchEnable= 'your-enable-password'
commandTorun= 'The command you want to run'
telnet = 'telnet ' + switchIP
#Login to the switch
t=pexpect.spawn(telnet)
t.expect('word:')
t.sendline(switchPassword)
t.expect('#')
t.sendline(switchEnable)
t.expect('>')
#Send the command
t.sendline('commandTorun')
t.expect('>')
data = t.before
#Closing the Telnet Connection
t.sendline('exit')
t.expect('>')
t.sendline('exit')
t.expect(pexpect.EOF)
#Opening the file and writing the data to it
f = open('routeroutput', 'w')
f.write(data)
f.close()
except Exception, e:
print "The Script failed to login"
print str(e)