| 1 | package PDK::Connector::Cisco; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use v5.30; | ||
| 5 | use Moose; | ||
| 6 | use Carp; | ||
| 7 | use Data::Dumper; | ||
| 8 | use Expect qw(exp_continue); | ||
| 9 | |||
| 10 | # 继承连接器常用功能 | ||
| 11 | with 'PDK::Connector::Role'; | ||
| 12 | |||
| 13 | # 登录成功提示符 | ||
| 14 | has prompt => ( | ||
| 15 | is => 'ro', | ||
| 16 | required => 0, | ||
| 17 | default => '\S+[#>]\s*\z', # 匹配Cisco设备的标准提示符格式 | ||
| 18 | ); | ||
| 19 | |||
| 20 | # 使能模式提示符 | ||
| 21 | has enPrompt => ( | ||
| 22 | is => 'ro', | ||
| 23 | required => 0, | ||
| 24 | default => '#\s*$', # 匹配Cisco设备的特权模式提示符 | ||
| 25 | ); | ||
| 26 | |||
| 27 | # 进入特权模式(Cisco设备使用enable命令) | ||
| 28 | sub enable { | ||
| 29 | my $self = shift; | ||
| 30 | my $enPass = $self->{enPassword} // $self->{password}; # 优先使用特权密码 | ||
| 31 | my $username = $self->{username}; | ||
| 32 | my $exp = $self->{exp}; | ||
| 33 | |||
| 34 | $exp->send("enable\n"); # Cisco使用enable命令进入特权模式 | ||
| 35 | my $enable = 1; | ||
| 36 | my $result = 0; | ||
| 37 | |||
| 38 | # 处理进入特权模式的过程 | ||
| 39 | my @ret = $exp->expect($self->{timeout}, | ||
| 40 | # 处理密码提示 | ||
| 41 | [ qr/assword:\s*$/ => sub { | ||
| 42 | if ($enable) { | ||
| 43 | $enable = 0; | ||
| 44 | $exp->send("$enPass\n"); # 输入密码 | ||
| 45 | } | ||
| 46 | else { | ||
| 47 | croak("[切换到特权模式异常] 用户或使能密码错误!") | ||
| 48 | } | ||
| 49 | exp_continue; | ||
| 50 | } ], | ||
| 51 | # 处理登录名提示 | ||
| 52 | [ qr/ogin:\s*$|name:\s*$/i => sub { | ||
| 53 | $exp->send("$username\n"); # 输入用户名 | ||
| 54 | exp_continue; | ||
| 55 | } ], | ||
| 56 | # 成功进入特权模式 | ||
| 57 | [ qr/$self->{enPrompt}/ => sub { | ||
| 58 | $self->{_enable_} = 1; # 设置特权模式标志 | ||
| 59 | $result = 1; # 设置操作成功标志 | ||
| 60 | } ], | ||
| 61 | # 进入特权模式失败 | ||
| 62 | [ qr/\^.*$/i => sub { | ||
| 63 | croak("请联系管理员检查原因: $1") | ||
| 64 | } ], | ||
| 65 | ); | ||
| 66 | |||
| 67 | if (defined $ret[1]) { | ||
| 68 | croak("[切换到特权模式异常]" . $ret[3] . $ret[1]); | ||
| 69 | } | ||
| 70 | |||
| 71 | return $result; | ||
| 72 | } | ||
| 73 | |||
| 74 | # 等待设备响应,处理各种交互提示 | ||
| 75 | sub waitfor { | ||
| 76 | my ($self, $prompt) = @_; | ||
| 77 | $prompt = $prompt // $self->{prompt}; # 使用提供的提示符或默认提示符 | ||
| 78 | my $exp = $self->{exp}; | ||
| 79 | my $buff = ""; | ||
| 80 | |||
| 81 | # 处理设备输出中的各种情况 | ||
| 82 | my @ret = $exp->expect($self->{timeout}, | ||
| 83 | # 处理分页显示(Cisco特有格式) | ||
| 84 | [ qr/^.+more\s*.+$/mi => sub { | ||
| 85 | $self->send(" "); # 发送空格继续显示 | ||
| 86 | $buff .= $exp->before(); | ||
| 87 | exp_continue; | ||
| 88 | } ], | ||
| 89 | # 处理配置文件确认提示 | ||
| 90 | [ qr/\[startup-config\]\?/i => sub { | ||
| 91 | $self->send("\r"); # 发送回车确认 | ||
| 92 | $buff .= $exp->before() . $exp->match(); | ||
| 93 | exp_continue; | ||
| 94 | } ], | ||
| 95 | # 处理远程主机地址提示 | ||
| 96 | [ qr/Address or name of remote host/i => sub { | ||
| 97 | $exp->send("\r"); # 发送回车确认 | ||
| 98 | $buff .= $exp->before() . $exp->match(); | ||
| 99 | exp_continue; | ||
| 100 | } ], | ||
| 101 | # 处理目标文件名提示 | ||
| 102 | [ qr/Destination filename \[/i => sub { | ||
| 103 | $exp->send("\r"); # 发送回车确认 | ||
| 104 | $buff .= $exp->before() . $exp->match(); | ||
| 105 | exp_continue; | ||
| 106 | } ], | ||
| 107 | # 匹配命令提示符 | ||
| 108 | [ qr/$prompt/m => sub { | ||
| 109 | $buff .= $exp->before() . $exp->match(); | ||
| 110 | } ] | ||
| 111 | ); | ||
| 112 | |||
| 113 | if (defined $ret[1]) { | ||
| 114 | croak("[等待回显捕捉异常]" . $ret[3] . $ret[1]); | ||
| 115 | } | ||
| 116 | |||
| 117 | # 清理输出内容(Cisco设备特有控制字符) | ||
| 118 | $buff =~ s/\r\n|\n+\n/\n/g; # 将Windows换行(\r\n)和多个连续换行统一为单个换行符 | ||
| 119 | $buff =~ s/\x{08}+\s+\x{08}+//g; # 删除退格符及其后面的空格(用于清除终端上的退格效果) | ||
| 120 | $buff =~ s/\x0D\[\s*#+\s*\]?\s*\d{1,2}%//g; # 删除进度条显示(如"[### ] 50%") | ||
| 121 | $buff =~ s/\x1B\[K//g; # 删除ANSI控制序列(清除行内容) | ||
| 122 | $buff =~ s/\x0D//g; # 删除单独的回车符(\r) | ||
| 123 | |||
| 124 | return $buff; | ||
| 125 | } | ||
| 126 | |||
| 127 | # 执行多个命令 | ||
| 128 | sub execCommands { | ||
| 129 | my ($self, $commands) = @_; | ||
| 130 | |||
| 131 | # 检查登录状态 | ||
| 132 | if ($self->{_login_} == 0) { | ||
| 133 | my $ret = $self->login(); | ||
| 134 | return $ret if $ret->{success} == 0; # 登录失败直接返回 | ||
| 135 | } | ||
| 136 | |||
| 137 | my $result = ""; | ||
| 138 | # 检查特权模式状态 | ||
| 139 | $self->enable() if not $self->{_enable_}; | ||
| 140 | |||
| 141 | # 逐个执行命令 | ||
| 142 | for my $cmd (@{$commands}) { | ||
| 143 | $self->send("$cmd\n"); | ||
| 144 | my $buff = $self->waitfor(); | ||
| 145 | |||
| 146 | # 检查命令执行结果(Cisco特有错误消息) | ||
| 147 | if ($buff =~ /Ambiguous|Incomplete|Unrecognized|not recognized|%Error/i) { | ||
| 148 | return { | ||
| 149 | success => 0, | ||
| 150 | failCommand => $cmd, | ||
| 151 | result => $result, | ||
| 152 | reason => $1 | ||
| 153 | }; | ||
| 154 | } | ||
| 155 | elsif ($buff =~ /Permission denied|syntax error|authorization failed/i) { | ||
| 156 | return { | ||
| 157 | success => 0, | ||
| 158 | failCommand => $cmd, | ||
| 159 | result => $result, | ||
| 160 | reason => $buff | ||
| 161 | }; | ||
| 162 | } | ||
| 163 | elsif ($buff =~ /Invalid (parameter|command|input)|Unknown command|Login invalid/i) { | ||
| 164 | return { | ||
| 165 | success => 0, | ||
| 166 | failCommand => $cmd, | ||
| 167 | result => $result, | ||
| 168 | reason => $buff | ||
| 169 | }; | ||
| 170 | } | ||
| 171 | else { | ||
| 172 | $result .= $buff; # 保存命令执行结果 | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | return { success => 1, result => $result }; | ||
| 177 | } | ||
| 178 | |||
| 179 | # 获取设备配置(Cisco特有命令) | ||
| 180 | sub getConfig { | ||
| 181 | my $self = shift; | ||
| 182 | |||
| 183 | my $commands = [ | ||
| 184 | "terminal width 511", # 设置终端宽度 | ||
| 185 | "terminal length 0", # 禁用分页 | ||
| 186 | "show run | exclude !Time", # 显示运行配置,排除时间注释行 | ||
| 187 | "copy run start" # 保存配置到启动配置 | ||
| 188 | ]; | ||
| 189 | |||
| 190 | my $config = $self->execCommands($commands); | ||
| 191 | my $lines = ""; | ||
| 192 | |||
| 193 | if ($config->{success} == 1) { | ||
| 194 | $lines = $config->{result}; | ||
| 195 | } | ||
| 196 | else { | ||
| 197 | return $config; # 返回错误信息 | ||
| 198 | } | ||
| 199 | |||
| 200 | return { success => 1, config => $lines }; | ||
| 201 | } | ||
| 202 | |||
| 203 | # 使Moose类不可变 | ||
| 204 | __PACKAGE__->meta->make_immutable; | ||
| 205 | 1; | ||
| 206 | |||
| 207 | =encoding utf8 | ||
| 208 | =head1 NAME | ||
| 209 | |||
| 210 | PDK::Connector::Cisco - Cisco设备专用连接器 | ||
| 211 | |||
| 212 | =head1 SYNOPSIS | ||
| 213 | |||
| 214 | use PDK::Connector::Cisco; | ||
| 215 | |||
| 216 | my $cisco = PDK::Connector::Cisco->new( | ||
| 217 | host => '192.168.1.1', | ||
| 218 | username => 'admin', | ||
| 219 | password => 'password', | ||
| 220 | enPassword => 'enablepass' | ||
| 221 | ); | ||
| 222 | |||
| 223 | my $config = $cisco->getConfig(); | ||
| 224 | |||
| 225 | =head1 DESCRIPTION | ||
| 226 | |||
| 227 | Cisco网络设备专用连接器模块,提供Cisco路由器、交换机等设备的 | ||
| 228 | 特殊命令支持和交互处理。 | ||
| 229 | |||
| 230 | =head1 ATTRIBUTES | ||
| 231 | |||
| 232 | =over 4 | ||
| 233 | |||
| 234 | =item * prompt: Cisco设备标准提示符 | ||
| 235 | |||
| 236 | =item * enPrompt: 特权模式提示符 | ||
| 237 | |||
| 238 | =item * enPassword: 特权模式密码 | ||
| 239 | |||
| 240 | =back | ||
| 241 | |||
| 242 | =head1 METHODS | ||
| 243 | |||
| 244 | =head2 enable() | ||
| 245 | |||
| 246 | 进入Cisco设备特权模式。 | ||
| 247 | |||
| 248 | =head2 waitfor($prompt) | ||
| 249 | |||
| 250 | 等待设备响应,处理Cisco特有交互提示。 | ||
| 251 | |||
| 252 | =head2 execCommands(\@commands) | ||
| 253 | |||
| 254 | 执行多个Cisco命令。 | ||
| 255 | |||
| 256 | =head2 getConfig() | ||
| 257 | |||
| 258 | 获取设备配置信息。 | ||
| 259 | |||
| 260 | =head1 CISCO特有功能 | ||
| 261 | |||
| 262 | =over 4 | ||
| 263 | |||
| 264 | =item * 自动处理'more'分页显示 | ||
| 265 | |||
| 266 | =item * 支持enable特权模式 | ||
| 267 | |||
| 268 | =item * 识别Cisco错误消息格式 | ||
| 269 | |||
| 270 | =item * 处理配置确认提示 | ||
| 271 | |||
| 272 | =back | ||
| 273 | |||
| 274 | =head1 EXAMPLES | ||
| 275 | |||
| 276 | =head2 获取设备配置 | ||
| 277 | |||
| 278 | my $cisco = PDK::Connector::Cisco->new(...); | ||
| 279 | my $result = $cisco->getConfig(); | ||
| 280 | if ($result->{success}) { | ||
| 281 | print $result->{config}; | ||
| 282 | } | ||
| 283 | |||
| 284 | =head2 执行Cisco命令 | ||
| 285 | |||
| 286 | my $commands = [ | ||
| 287 | 'show version', | ||
| 288 | 'show interface', | ||
| 289 | 'show running-config' | ||
| 290 | ]; | ||
| 291 | my $results = $cisco->execCommands($commands); | ||
| 292 | |||
| 293 | =head1 DIAGNOSTICS | ||
| 294 | |||
| 295 | =over 4 | ||
| 296 | |||
| 297 | =item * 特权模式失败: 检查enable密码是否正确 | ||
| 298 | |||
| 299 | =item * 命令执行错误: 确认命令在设备上可用 | ||
| 300 | |||
| 301 | =item * 连接超时: 检查设备响应状态 | ||
| 302 | |||
| 303 | =back | ||
| 304 | |||
| 305 | =head1 AUTHOR | ||
| 306 | |||
| 307 | WENWU YAN E<lt>968828@gmail.comE<gt> | ||
| 308 | |||
| 309 | =head1 LICENSE AND COPYRIGHT | ||
| 310 | |||
| 311 | 版权所有 2025 WENWU YAN。 | ||
| 312 | |||
| 313 | 本软件按Perl自身许可条款发布。 | ||
| 314 | |||
| 315 | =cut |