本文目录导读:

- 最简单的:Ansible Playbook(推荐)
- 使用 Python + Netmiko / NAPALM
- 自动化 TACACS+ 服务器端配置
- 一键脚本(Bash + Expect)— 适合极少数设备
- 总结建议
- 最后,一个重要的警告
当然可以,有些实用脚本可以辅助或部分自动化配置 TACACS+,但完全“自动配置”的复杂性取决于你的网络设备类型、TACACS+ 服务器类型以及你想要实现的功能。
以下是几种常见的自动化脚本方法,按复杂度和适用场景分类:
最简单的:Ansible Playbook(推荐)
这是目前最成熟、最实用的方式,Ansible 有大量现成的模块用于网络设备配置。
目标:自动将网络设备(如 Cisco IOS、Nexus、Arista 等)指向你的 TACACS+ 服务器,并配置认证、授权、计费(AAA)。
示例 Playbook(适用于 Cisco IOS):
---
- name: Configure TACACS+ on Cisco Devices
hosts: all
gather_facts: no
vars:
tacacs_server_ip: "192.168.1.100"
tacacs_key: "YourSecretKey123!"
enable_password: "MyEnablePass456"
tasks:
- name: Enable AAA services globally
ios_config:
lines:
- aaa new-model
- name: Configure TACACS+ server group
ios_config:
lines:
- tacacs server {{ tacacs_server_ip }}
- key {{ tacacs_key }}
- name: Define AAA authentication login using TACACS+
ios_config:
lines:
- aaa authentication login default group tacacs+ local
- name: Define AAA authorization (for exec/commands/enable)
ios_config:
lines:
- aaa authorization exec default group tacacs+ local
- aaa authorization commands 15 default group tacacs+ local
- aaa authorization config-commands
- name: Define AAA accounting
ios_config:
lines:
- aaa accounting exec default start-stop group tacacs+
- aaa accounting commands 15 default start-stop group tacacs+
- name: Configure console/vty lines to use AAA
ios_config:
parents:
- line console 0
lines:
- login authentication default
ios_config:
parents:
- line vty 0 4
lines:
- login authentication default
优点:
- 幂等性:多次运行不会导致重复配置。
- 广泛支持:支持思科(Nexus/IOS-XR)、Juniper(Junos)、华为、Aruba 等。
- 易于管理:可扩展到成百上千台设备。
使用 Python + Netmiko / NAPALM
如果你熟悉 Python,可以用脚本通过 SSH 连接到设备,逐台推送配置。
示例代码(Cisco 设备):
from netmiko import ConnectHandler
device = {
'device_type': 'cisco_ios',
'ip': '192.168.1.1',
'username': 'admin',
'password': 'password123'
}
tacacs_commands = [
'configure terminal',
'aaa new-model',
'tacacs server 192.168.1.100',
'key YourSecretKey123!',
'exit',
'aaa authentication login default group tacacs+ local',
'aaa authorization exec default group tacacs+ local',
'aaa authorization commands 15 default group tacacs+ local',
'aaa accounting exec default start-stop group tacacs+',
'aaa accounting commands 15 default start-stop group tacacs+',
'line console 0',
'login authentication default',
'line vty 0 4',
'login authentication default',
'end',
'write memory'
]
connection = ConnectHandler(**device)
output = connection.send_config_set(tacacs_commands)
print(output)
connection.disconnect()
优点:
- 高度灵活:可以嵌入逻辑判断(如只配特定型号的设备)。
- 可集成:与 CMDB(配置管理数据库)或 ticket 系统联动。
缺点:
- 需要编程基础。
- 需要处理错误(如连接失败、命令回滚)。
自动化 TACACS+ 服务器端配置
脚本不仅能配网络设备, 还能自动配置 TACACS+ 服务器本身(如 tac_plus、freeradius、Cisco ACS / ISE)。
场景:批量添加用户、管理权限、生成配置。
示例脚本(修改 /etc/tac_plus.conf):
#!/bin/bash
# 添加一个名为"ops_engineer"的用户到TACACS+服务器
echo 'user = ops_engineer {
global_acl = "permit"
service = exec {
priv-lvl = 15
}
login = cleartext "StrongPass789!"
}' >> /etc/tac_plus.conf
# 重启服务
systemctl restart tac_plus
更复杂的用法:从数据库或 CSV 文件中读取用户信息,生成配置。
# 读取 CSV,生成 TACACS+ 配置
import csv
with open('users.csv') as f:
reader = csv.DictReader(f)
for row in reader:
config_str = f"user = {row['username']} {{\n"
config_str += f" login = cleartext \"{row['password']}\"\n"
config_str += f" service = exec {{\n"
config_str += f" priv-lvl = {row['level']}\n"
config_str += " }\n}\n"
with open('/etc/tac_plus.conf', 'a') as config_file:
config_file.write(config_str)
一键脚本(Bash + Expect)— 适合极少数设备
如果只有几台设备,且不想学 Ansible 或 Python,可以用 Expect 自动交互。
#!/usr/bin/expect
set timeout 20
set ip [lindex $argv 0]
set username "admin"
set password "pass123"
spawn ssh $username@$ip
expect "Password:" { send "$password\r" }
expect "#" { send "configure terminal\r" }
expect "(config)#" { send "aaa new-model\r" }
expect "(config)#" { send "tacacs server 10.0.0.1\r" }
expect "(config-server-tacacs)#" { send "key MyKey\r" }
# ... 更多命令
expect "(config)#" { send "end\r" }
expect "#" { send "write memory\r" }
expect "#" { send "exit\r" }
缺点:
- 脆弱:依赖精确的输出抓取(如 、
Password:字符串)。 - 不可靠:如果设备响应慢或输出不同,脚本会失败。
- 不适合批量。
总结建议
| 方法 | 适用场景 | 难度 | 可靠性 |
|---|---|---|---|
| Ansible | 批量、大规模设备 | 中等 | 极高(幂等,结构化) |
| Python + Netmiko | 灵活定制、集成现有系统 | 高 | 高(需处理异常) |
| Shell + Expect | 临时调试、少量旧设备 | 低 | 低(脆弱) |
| TACACS服务器配置脚本 | 管理用户、权限 | 中等 | 高 |
推荐方案:
- 如果要用企业级配置:用 Ansible,社区有现成的 Role(如
ansible-network)。 - 如果要快速定制:用 Python + Netmiko。
- 如果只想自己用几台设备:用 Bash 脚本 + 手动执行(或者拷贝配置模板)。
一个重要的警告
在自动配置 TACACS+ 前,务必确保满足以下条件,否则你会把自己锁在门外:
- 保留本地后备:配置
aaa authentication login default group tacacs+ local(local在最后是必须的)。 - 先测试一小部分设备:先在 1-2 台不影响业务的设备上试运行。
- 确保网络可达:设备必须能连接到 TACACS+ 服务器的 IP 和端口(通常是 TCP 49)。
- 使用 SSH,别用 Telnet:脚本的配置过程中不要断连。
如果你能提供具体的设备型号(如 Cisco 3750、Juniper EX、华为 CE)和 TACACS+ 服务器类型(如 FreeRADIUS、Cisco ISE、tac_plus),我可以给出更精确的脚本示例。