変更前後の検証、テレメトリーとLinuxの通信経路7 / 9
PythonからNX-OSの表示コマンドを実行
管理端末でParamikoを使う例である。SSHの既知のホスト鍵を用意し、NX-OSへ接続する。
import os
import paramiko
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
try:
ssh.connect(
os.environ["NX_HOST"], username=os.environ["NX_USER"],
password=os.environ["NX_PASSWORD"],
timeout=10, allow_agent=False, look_for_keys=False
)
_, stdout, stderr = ssh.exec_command("show interface brief", timeout=15)
text = stdout.read().decode("utf-8", errors="replace")
error = stderr.read().decode("utf-8", errors="replace")
if stdout.channel.recv_exit_status() != 0:
raise RuntimeError(error)
print(text)
finally:
ssh.close()
show interface briefの標準出力を表示する。終了ステータスが0以外なら、標準エラーを使って失敗を通知する。接続はfinallyで閉じる。
参考資料
- Cisco DCNAUTO v2.0 Exam Topics ↗
- Cisco pyATS and Genie ↗
- Cisco pyATS CLI examples ↗
- NX-OS gNMI 10.6 ↗
- NX-OS gRPC Agent 10.6 ↗
- OpenConfig gNMI specification ↗
- NX-OS Telemetry 10.6 ↗
- Nexus telemetry configuration and verification ↗
- Nexus Dashboard Get Anomalies API ↗
- Nexus Dashboard API authentication ↗
- NetBox REST API integration ↗
- Linux bridge documentation ↗
- Python subprocess ↗
- Paramiko SSH client ↗
- Linux bonding driver ↗