| 1 | package PDK::Connector::Huawei; | ||
| 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 | # 登录成功提示符(Huawei设备特有格式) | ||
| 14 | has prompt => ( | ||
| 15 | is => 'ro', | ||
| 16 | required => 0, | ||
| 17 | default => '^\s*(\x00)?[<\[].*?[>\]]\s*$', # 匹配Huawei设备的提示符格式 | ||
| 18 | ); | ||
| 19 | |||
| 20 | # 进入特权模式 | ||
| 21 | sub enable { | ||
| 22 | my $self = shift; | ||
| 23 | my $enPass = $self->{enPassword} // $self->{password}; # 优先使用特权密码 | ||
| 24 | my $username = $self->{username}; | ||
| 25 | my $exp = $self->{exp}; | ||
| 26 | |||
| 27 | $exp->send("su\n"); # Huawei使用su命令进入特权模式 | ||
| 28 | my $enable = 1; # 标记是否已尝试输入密码 | ||
| 29 | my $result = 0; # 操作结果 | ||
| 30 | |||
| 31 | # 处理进入特权模式的过程 | ||
| 32 | my @ret = $exp->expect($self->{timeout}, # 使用配置的超时时间 | ||
| 33 | # 处理密码提示 | ||
| 34 | [ qr/assword:\s*$/ => sub { | ||
| 35 | if ($enable) { | ||
| 36 | $enable = 0; | ||
| 37 | $exp->send("$enPass\n"); # 输入密码 | ||
| 38 | } | ||
| 39 | else { | ||
| 40 | croak("[切换到特权模式异常] 用户或使能密码错误!") | ||
| 41 | } | ||
| 42 | exp_continue; | ||
| 43 | } ], | ||
| 44 | # 处理登录名提示 | ||
| 45 | [ qr/ogin:\s*$|name:\s*$/i => sub { | ||
| 46 | $exp->send("$username\n"); # 输入用户名 | ||
| 47 | exp_continue; | ||
| 48 | } ], | ||
| 49 | # 成功进入特权模式(匹配Huawei特权模式提示) | ||
| 50 | [ qr/(privilege\s+level\s+is.+>\s*\z)|(privilege\s+is\s+.+>\s*\z)/si => sub { | ||
| 51 | $self->{_enable_} = 1; # 设置特权模式标志 | ||
| 52 | $result = 1; # 设置操作成功标志 | ||
| 53 | } ], | ||
| 54 | # 进入特权模式失败 | ||
| 55 | [ qr/\^.*$/i => sub { | ||
| 56 | croak("请联系管理员检查原因: $1") # 抛出异常并包含错误信息 | ||
| 57 | } ], | ||
| 58 | ); | ||
| 59 | |||
| 60 | if (defined $ret[1]) { | ||
| 61 | croak("[切换到特权模式异常]" . $ret[3] . $ret[1]); | ||
| 62 | } | ||
| 63 | |||
| 64 | return $result; | ||
| 65 | } | ||
| 66 | |||
| 67 | # 等待设备响应,处理各种交互提示 | ||
| 68 | sub waitfor { | ||
| 69 | my ($self, $prompt) = @_; | ||
| 70 | $prompt //= $self->{prompt}; # 使用提供的提示符或默认提示符 | ||
| 71 | |||
| 72 | my $exp = $self->{exp}; | ||
| 73 | my $buff = ""; | ||
| 74 | |||
| 75 | # 处理设备输出中的各种情况 | ||
| 76 | my @ret = $exp->expect($self->{timeout}, # 使用配置的超时时间 | ||
| 77 | # 处理分页显示(Huawei特有格式) | ||
| 78 | [ qr/^\s*---- More ----\s*$/i => sub { | ||
| 79 | $self->send(" "); # 发送空格继续显示 | ||
| 80 | $buff .= $exp->before(); | ||
| 81 | exp_continue; | ||
| 82 | } ], | ||
| 83 | # 处理确认提示 | ||
| 84 | [ qr/(Are you sure|overwrite|Continue|save operation)\? \[Y\/N\]:/i => sub { | ||
| 85 | $self->send("Y\r"); # 自动确认 | ||
| 86 | $buff .= $exp->before() . $exp->match(); | ||
| 87 | exp_continue; | ||
| 88 | } ], | ||
| 89 | # 处理选择提示 | ||
| 90 | [ qr/Before pressing ENTER you must choose/i => sub { | ||
| 91 | $self->send("Y\r"); # 自动选择是 | ||
| 92 | $buff .= $exp->before() . $exp->match(); | ||
| 93 | exp_continue; | ||
| 94 | } ], | ||
| 95 | # 处理按键提示 | ||
| 96 | [ qr/press the enter key/i => sub { | ||
| 97 | $self->send("\r"); # 发送回车 | ||
| 98 | $buff .= $exp->before() . $exp->match(); | ||
| 99 | exp_continue; | ||
| 100 | } ], | ||
| 101 | # 匹配命令提示符,表示命令执行完成 | ||
| 102 | [ qr/$prompt/m => sub { | ||
| 103 | $buff .= $exp->before() . $exp->match(); | ||
| 104 | } ] | ||
| 105 | ); | ||
| 106 | |||
| 107 | if (defined $ret[1]) { | ||
| 108 | croak("[等待回显捕捉异常]" . $ret[3] . $ret[1]); | ||
| 109 | } | ||
| 110 | |||
| 111 | # 清理输出内容(Huawei设备特有控制字符) | ||
| 112 | $buff =~ s/\cM+[ ]+\cM//g; # 删除退格效果 | ||
| 113 | $buff =~ s/\cM{2}//g; # 删除多余回车符 | ||
| 114 | $buff =~ s/\cM//g; # 删除回车符 | ||
| 115 | $buff =~ s/\x00//g; # 删除空字符 | ||
| 116 | $buff =~ s/\x1b\[\d+D\s+\x1b\[\d+D//g; # 删除ANSI光标左移序列及其影响的空格(用于终端覆盖显示) | ||
| 117 | |||
| 118 | return $buff; | ||
| 119 | } | ||
| 120 | |||
| 121 | # 执行多个命令 | ||
| 122 | sub execCommands { | ||
| 123 | my ($self, $commands) = @_; | ||
| 124 | |||
| 125 | # 检查登录状态,如果未登录则先登录 | ||
| 126 | if ($self->{_login_} == 0) { | ||
| 127 | my $ret = $self->login(); | ||
| 128 | return $ret if $ret->{success} == 0; # 登录失败直接返回 | ||
| 129 | } | ||
| 130 | |||
| 131 | my $result = ""; | ||
| 132 | # 如果未进入特权模式,则先进入特权模式 | ||
| 133 | $self->enable() if not $self->{_enable_}; | ||
| 134 | |||
| 135 | # 逐个执行命令 | ||
| 136 | for my $cmd (@{$commands}) { | ||
| 137 | $self->send("$cmd\n"); | ||
| 138 | my $buff = $self->waitfor(); | ||
| 139 | |||
| 140 | # 检查命令是否执行失败(Huawei特有错误消息) | ||
| 141 | if ($buff =~ /Ambiguous|Incomplete|not recognized|Unrecognized/i) { | ||
| 142 | return { | ||
| 143 | success => 0, | ||
| 144 | failCommand => $cmd, # 失败的命令 | ||
| 145 | result => $result, # 已成功执行的命令结果 | ||
| 146 | reason => $buff # 失败原因 | ||
| 147 | }; | ||
| 148 | } | ||
| 149 | elsif ($buff =~ /Too many parameters|Invalid (input|command|file)|Unknown command|Wrong parameter/i) { | ||
| 150 | return { | ||
| 151 | success => 0, | ||
| 152 | failCommand => $cmd, # 失败的命令 | ||
| 153 | result => $result, # 已成功执行的命令结果 | ||
| 154 | reason => $buff # 失败原因 | ||
| 155 | }; | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | $result .= $buff; # 保存命令执行结果 | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | return { success => 1, result => $result }; | ||
| 163 | } | ||
| 164 | |||
| 165 | # 获取设备配置方法(Huawei特有命令) | ||
| 166 | sub getConfig { | ||
| 167 | my $self = shift; | ||
| 168 | my $commands = [ | ||
| 169 | "screen-length disable", # 禁用分页显示 | ||
| 170 | "dis current-configuration", # 显示当前配置 | ||
| 171 | "save force", # 强制保存配置 | ||
| 172 | ]; | ||
| 173 | |||
| 174 | my $config = $self->execCommands($commands); | ||
| 175 | my $lines = ""; | ||
| 176 | |||
| 177 | if ($config->{success} == 1) { | ||
| 178 | $lines = $config->{result}; | ||
| 179 | } | ||
| 180 | else { | ||
| 181 | return $config; # 返回错误信息 | ||
| 182 | } | ||
| 183 | |||
| 184 | return { success => 1, config => $lines }; | ||
| 185 | } | ||
| 186 | |||
| 187 | # 使Moose类不可变 | ||
| 188 | __PACKAGE__->meta->make_immutable; | ||
| 189 | 1; | ||
| 190 | |||
| 191 | =encoding utf8 | ||
| 192 | =head1 NAME | ||
| 193 | |||
| 194 | PDK::Connector::Huawei - Huawei 华为设备连接器 | ||
| 195 | |||
| 196 | =head1 SYNOPSIS | ||
| 197 | |||
| 198 | use PDK::Connector::Huawei; | ||
| 199 | |||
| 200 | # 创建 Huawei 设备连接器实例 | ||
| 201 | my $conn = PDK::Connector::Huawei->new( | ||
| 202 | host => '192.168.1.1', | ||
| 203 | username => 'admin', | ||
| 204 | password => 'password', | ||
| 205 | enPassword => 'enable_password' # 特权密码 | ||
| 206 | ); | ||
| 207 | |||
| 208 | # 登录设备 | ||
| 209 | $conn->login; | ||
| 210 | |||
| 211 | # 获取设备配置 | ||
| 212 | my $config = $conn->getConfig; | ||
| 213 | |||
| 214 | # 执行自定义命令 | ||
| 215 | my $result = $conn->execCommands(['display version', 'display interface']); | ||
| 216 | |||
| 217 | =head1 DESCRIPTION | ||
| 218 | |||
| 219 | Huawei 华为网络设备专用连接器,继承 L<PDK::Connector::Role> 角色,提供与 Huawei 设备的交互功能。 | ||
| 220 | |||
| 221 | 支持设备登录、特权模式切换、命令执行、配置获取等操作,专门处理 Huawei 设备特有的交互流程和提示信息。 | ||
| 222 | |||
| 223 | =head1 ATTRIBUTES | ||
| 224 | |||
| 225 | =head2 prompt | ||
| 226 | |||
| 227 | Huawei 设备命令行提示符的正则表达式模式,默认匹配包含尖括号或方括号的提示符格式。 | ||
| 228 | |||
| 229 | =head1 METHODS | ||
| 230 | |||
| 231 | =head2 enable | ||
| 232 | |||
| 233 | $conn->enable; | ||
| 234 | |||
| 235 | 进入特权模式。Huawei 设备使用 'su' 命令进入特权模式,自动处理密码输入和权限验证。 | ||
| 236 | |||
| 237 | =head2 waitfor | ||
| 238 | |||
| 239 | my $output = $conn->waitfor($prompt); | ||
| 240 | |||
| 241 | 等待设备响应,处理 Huawei 设备特有的交互提示: | ||
| 242 | - 分页显示控制(---- More ----) | ||
| 243 | - 操作确认提示(Are you sure? [Y/N]) | ||
| 244 | - 选择提示(Before pressing ENTER you must choose) | ||
| 245 | - 按键提示(press the enter key) | ||
| 246 | |||
| 247 | 自动清理输出中的控制字符、ANSI 转义序列和特殊字符,返回处理后的文本内容。 | ||
| 248 | |||
| 249 | =head2 execCommands | ||
| 250 | |||
| 251 | my $result = $conn->execCommands(\@commands); | ||
| 252 | |||
| 253 | 执行多个命令序列。自动检测 Huawei 特有的错误消息(Ambiguous/Incomplete/Unrecognized/Invalid command 等),返回包含执行结果的结构化数据。 | ||
| 254 | |||
| 255 | =head2 getConfig | ||
| 256 | |||
| 257 | my $config = $conn->getConfig; | ||
| 258 | |||
| 259 | 获取设备完整配置。使用 Huawei 特有的命令序列: | ||
| 260 | - 禁用分页显示(screen-length disable) | ||
| 261 | - 显示当前配置(dis current-configuration) | ||
| 262 | - 强制保存配置(save force) | ||
| 263 | |||
| 264 | =head1 SEE ALSO | ||
| 265 | |||
| 266 | L<PDK::Connector::Role>, L<Expect>, L<Moose> | ||
| 267 | |||
| 268 | =head1 AUTHOR | ||
| 269 | |||
| 270 | WENWU YAN E<lt>968828@gmail.comE<gt> | ||
| 271 | |||
| 272 | =head1 LICENSE AND COPYRIGHT | ||
| 273 | |||
| 274 | 版权所有 2025 WENWU YAN。 | ||
| 275 | |||
| 276 | 本软件按Perl自身许可条款发布。 | ||
| 277 | |||
| 278 | =cut |