| 1 | package PDK::Connector::Paloalto; | ||
| 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 | # 登录成功提示符(Paloalto设备特有格式) | ||
| 14 | has prompt => ( | ||
| 15 | is => 'ro', | ||
| 16 | required => 0, | ||
| 17 | default => '^.*?\((?:active|passive|suspended)\)[>#]\s*$', # 匹配Paloalto设备的提示符格式(包含HA状态) | ||
| 18 | ); | ||
| 19 | |||
| 20 | # 特权模式状态标志(Paloalto设备默认进入特权模式) | ||
| 21 | has _enable_ => ( | ||
| 22 | is => 'ro', | ||
| 23 | required => 0, | ||
| 24 | default => 1, # Paloalto设备默认进入特权模式 | ||
| 25 | ); | ||
| 26 | |||
| 27 | # 等待设备响应,处理各种交互提示 | ||
| 28 | sub waitfor { | ||
| 29 | my ($self, $prompt) = @_; | ||
| 30 | $prompt //= $self->{prompt}; # 使用提供的提示符或默认提示符 | ||
| 31 | |||
| 32 | my $exp = $self->{exp}; | ||
| 33 | my $buff = ""; | ||
| 34 | |||
| 35 | # 等待设备响应,处理各种交互提示 | ||
| 36 | my @ret = $exp->expect($self->{timeout}, | ||
| 37 | # 处理分页提示(Paloalto特有格式) | ||
| 38 | [ qr/^lines\s*\d+-\d+\s*$/i => sub { | ||
| 39 | $self->send(" "); # 发送空格继续显示 | ||
| 40 | $buff .= $exp->before(); | ||
| 41 | exp_continue; | ||
| 42 | } ], | ||
| 43 | # 处理确认提示 | ||
| 44 | [ qr/are you sure\?/i => sub { | ||
| 45 | $self->send("y\r"); # 发送y确认 | ||
| 46 | $buff .= $exp->before() . $exp->match(); | ||
| 47 | exp_continue; | ||
| 48 | } ], | ||
| 49 | # 匹配命令提示符 | ||
| 50 | [ qr/$prompt/m => sub { | ||
| 51 | $buff .= $exp->before() . $exp->match(); # 保存完整输出 | ||
| 52 | } ] | ||
| 53 | ); | ||
| 54 | |||
| 55 | # 检查Expect操作是否出错 | ||
| 56 | if (defined $ret[1]) { | ||
| 57 | croak("[等待回显期间异常]" . $ret[3] . $ret[1]); | ||
| 58 | } | ||
| 59 | |||
| 60 | # 清理输出内容(Paloalto设备特有控制字符) | ||
| 61 | $buff =~ s/ \cH//g; # 移除空格后跟退格符(常用于终端覆盖显示) | ||
| 62 | $buff =~ s/(\c[\S+)+\cM(\c[\[K)?//g; # 移除ANSI转义序列和回车符(控制光标位置和清除行) | ||
| 63 | $buff =~ s/\cM(\c[\S+)+\c[>//g; # 移除包含回车符的复杂ANSI序列(通常是终端提示符相关) | ||
| 64 | |||
| 65 | return $buff; | ||
| 66 | } | ||
| 67 | |||
| 68 | # 执行多个命令 | ||
| 69 | sub execCommands { | ||
| 70 | my ($self, $commands) = @_; | ||
| 71 | |||
| 72 | # 检查登录状态,如果未登录则先登录 | ||
| 73 | if (!$self->{_login_}) { | ||
| 74 | my $ret = $self->login(); | ||
| 75 | return $ret unless $ret->{success}; | ||
| 76 | } | ||
| 77 | |||
| 78 | my $result = ''; | ||
| 79 | # 逐个执行命令 | ||
| 80 | for my $cmd (@{$commands}) { | ||
| 81 | $self->send("$cmd\n"); | ||
| 82 | |||
| 83 | # 检查命令是否执行失败(Paloalto特有错误消息) | ||
| 84 | my $buff = $self->waitfor(); | ||
| 85 | if ($buff =~ /^Error:|Unknown command|Invalid syntax/i) { | ||
| 86 | return { | ||
| 87 | success => 0, | ||
| 88 | failCommand => $cmd, | ||
| 89 | result => $result, | ||
| 90 | reason => $buff | ||
| 91 | }; | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | $result .= $buff; # 保存命令执行结果 | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | return { success => 1, result => $result }; | ||
| 99 | } | ||
| 100 | |||
| 101 | # 获取设备配置方法(Paloalto特有命令) | ||
| 102 | sub getConfig { | ||
| 103 | my $self = shift; | ||
| 104 | my $commands = [ | ||
| 105 | "set cli pager off", # 禁用分页 | ||
| 106 | "set cli config-output-format set", # 设置配置输出格式为set命令格式 | ||
| 107 | "configure", # 进入配置模式 | ||
| 108 | "show", # 显示配置 | ||
| 109 | "quit" # 退出配置模式 | ||
| 110 | ]; | ||
| 111 | my $config = $self->execCommands($commands); | ||
| 112 | my $lines = ""; | ||
| 113 | |||
| 114 | if ($config->{success} == 1) { | ||
| 115 | $lines = $config->{result}; | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | return $config; # 返回错误信息 | ||
| 119 | } | ||
| 120 | |||
| 121 | return { success => 1, config => $lines }; | ||
| 122 | } | ||
| 123 | |||
| 124 | # 使Moose类不可变 | ||
| 125 | __PACKAGE__->meta->make_immutable; | ||
| 126 | 1; | ||
| 127 | |||
| 128 | =encoding utf8 | ||
| 129 | =head1 NAME | ||
| 130 | |||
| 131 | PDK::Connector::Paloalto - Paloalto 防火墙设备连接器 | ||
| 132 | |||
| 133 | =head1 SYNOPSIS | ||
| 134 | |||
| 135 | use PDK::Connector::Paloalto; | ||
| 136 | |||
| 137 | # 创建 Paloalto 设备连接器实例 | ||
| 138 | my $conn = PDK::Connector::Paloalto->new( | ||
| 139 | host => '192.168.1.1', | ||
| 140 | username => 'admin', | ||
| 141 | password => 'password' | ||
| 142 | ); | ||
| 143 | |||
| 144 | # 登录设备 | ||
| 145 | $conn->login; | ||
| 146 | |||
| 147 | # 获取设备配置 | ||
| 148 | my $config = $conn->getConfig; | ||
| 149 | |||
| 150 | # 执行自定义命令 | ||
| 151 | my $result = $conn->execCommands(['show system info', 'show interface all']); | ||
| 152 | |||
| 153 | =head1 DESCRIPTION | ||
| 154 | |||
| 155 | Paloalto 防火墙设备专用连接器,继承 L<PDK::Connector::Role> 角色,提供与 Paloalto 防火墙设备的交互功能。 | ||
| 156 | |||
| 157 | 支持设备登录、命令执行、配置获取等操作,自动处理 Paloalto 设备特有的交互提示、分页显示和 ANSI 控制字符。 | ||
| 158 | |||
| 159 | =head1 ATTRIBUTES | ||
| 160 | |||
| 161 | =head2 prompt | ||
| 162 | |||
| 163 | Paloalto 设备命令行提示符的正则表达式模式,默认匹配包含 HA 状态(active/passive/suspended)的提示符格式。 | ||
| 164 | |||
| 165 | =head2 _enable_ | ||
| 166 | |||
| 167 | 特权模式状态标志,Paloalto 设备默认进入特权模式,值为 1。 | ||
| 168 | |||
| 169 | =head1 METHODS | ||
| 170 | |||
| 171 | =head2 waitfor | ||
| 172 | |||
| 173 | my $output = $conn->waitfor($prompt); | ||
| 174 | |||
| 175 | 等待设备响应,处理 Paloalto 设备特有的交互提示: | ||
| 176 | - 分页显示控制(lines X-X 格式) | ||
| 177 | - 操作确认提示(are you sure?) | ||
| 178 | |||
| 179 | 自动清理输出中的 ANSI 转义序列和控制字符,返回处理后的文本内容。 | ||
| 180 | |||
| 181 | =head2 execCommands | ||
| 182 | |||
| 183 | my $result = $conn->execCommands(\@commands); | ||
| 184 | |||
| 185 | 执行多个命令序列。自动检测 Paloalto 特有的错误消息(Error:/Unknown command/Invalid syntax),返回包含执行结果的结构化数据。 | ||
| 186 | |||
| 187 | =head2 getConfig | ||
| 188 | |||
| 189 | my $config = $conn->getConfig; | ||
| 190 | |||
| 191 | 获取设备完整配置。使用 Paloalto 特有的命令序列: | ||
| 192 | - 禁用分页显示 | ||
| 193 | - 设置配置输出格式为 set 命令格式 | ||
| 194 | - 进入配置模式并显示配置 | ||
| 195 | |||
| 196 | =head1 SEE ALSO | ||
| 197 | |||
| 198 | L<PDK::Connector::Role>, L<Expect>, L<Moose> | ||
| 199 | |||
| 200 | =head1 AUTHOR | ||
| 201 | |||
| 202 | WENWU YAN E<lt>968828@gmail.comE<gt> | ||
| 203 | |||
| 204 | =head1 LICENSE AND COPYRIGHT | ||
| 205 | |||
| 206 | 版权所有 2025 WENWU YAN。 | ||
| 207 | |||
| 208 | 本软件按Perl自身许可条款发布。 | ||
| 209 | =cut |