Perl 十进制到 ASCII 的转换

发布于 2024-11-30 23:45:10 字数 607 浏览 1 评论 0原文

我正在从 F5 LTM 提取 SNMP 信息,并将该信息存储在 psql 数据库中。我需要帮助将十进制格式返回的数据转换为 ASCII 字符。 以下是从 SNMP 请求返回的信息的示例:

iso.3.6.1.4.1.3375.2.2.10.2.3.1.9.10.102.111.114.119.97.114.100.95.118.115 = Counter64: 0  

在我的脚本中,我需要识别此信息的不同部分:

my ($prefix, $num, $char-len, $vs) = ($oid =~ /($vsTable)\.(\d+)\.(\d+)\.(.+)/);

这给出了以下内容:

(prefix= .1.3.6.1.4.1.3375.2.2.10.2.3.1)  
(num= 9 ) 
(char-len= 10 ) 
(vs= 102.111.114.119.97.114.100.95.118.115)

变量 $vs 是 中的对象名称十进制格式。我想将其转换为 ASCII 字符(应该是“forward_vs”)。 有人对如何做到这一点有建议吗?

I am pulling SNMP information from an F5 LTM, and storing this information in a psql database. I need help converting the returned data in decimal format into ASCII characters.
Here is an example of the information returned from the SNMP request:

iso.3.6.1.4.1.3375.2.2.10.2.3.1.9.10.102.111.114.119.97.114.100.95.118.115 = Counter64: 0  

In my script, I need to identify the different sections of this information:

my ($prefix, $num, $char-len, $vs) = ($oid =~ /($vsTable)\.(\d+)\.(\d+)\.(.+)/);

This gives me the following:

(prefix= .1.3.6.1.4.1.3375.2.2.10.2.3.1)  
(num= 9 ) 
(char-len= 10 ) 
(vs= 102.111.114.119.97.114.100.95.118.115)

The variable $vs is the Object name in decimal format. I would like to convert this to ASCII characters (which should be "forward_vs").
Does anyone have a suggestion on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

腻橙味 2024-12-07 23:45:10

鉴于这与解释 SNMP 数据有关,对我来说使用一个或多个 SNMP 模块可从 CPAN 获得。您必须对 SNMP 了解很多,才能确定您引用的字符串何时停止作为标识符(前缀)并开始作为值。与手工编写的代码相比,使用 SNMP 代码更有可能获得通用解决方案。

Given that this is related to interpreting SNMP data, it seems logical to me to use one or more of the SNMP modules available from CPAN. You have to know quite a lot about SNMP to determine when the string you quote stops being the identifier (prefix) and starts to be the value. You have a better chance of getting a general solution with SNMP code than with hand-hacked code.

素衣风尘叹 2024-12-07 23:45:10

Jonathan Leffler 有正确的答案,但这里有一些事情可以扩展您的 Perl 视野:

use v5.10;
$_ = "102.111.114.119.97.114.100.95.118.115";
say "Version 1: " => eval;
say "Version 2: " => pack "W".(1+y/.//) => /\d+/g;

执行,打印:

Version 1: forward_vs
Version 2: forward_vs

一旦您清楚了两者,您可以按空格键继续或按 q 退出。 :)

编辑:最后一个也可以写,

pack "WW".y/.//,/\d+/g

但请不要。 :)

Jonathan Leffler has the right answer, but here are a couple of things to expand your Perl horizons:

use v5.10;
$_ = "102.111.114.119.97.114.100.95.118.115";
say "Version 1: " => eval;
say "Version 2: " => pack "W".(1+y/.//) => /\d+/g;

Executed, that prints:

Version 1: forward_vs
Version 2: forward_vs

Once both are clear to you, you may hit space to continue or q to quit. :)

EDIT: The last one can also be written

pack "WW".y/.//,/\d+/g

But please don't. :)

遇见了你 2024-12-07 23:45:10
my $new_vs = join("", map { chr($_) } split(/\./,$vs));
my $new_vs = join("", map { chr($_) } split(/\./,$vs));
娜些时光,永不杰束 2024-12-07 23:45:10

简单的解决方案:

$ascii .= chr for split /\./, $vs; 

Simple solution:

$ascii .= chr for split /\./, $vs; 
蒲公英的约定 2024-12-07 23:45:10
pack 'C*', split /\./

例如,

>perl -E"say pack 'C*', split /\./, $ARGV[0]" 102.111.114.119.97.114.100.95.118.115
forward_vs
pack 'C*', split /\./

For example,

>perl -E"say pack 'C*', split /\./, $ARGV[0]" 102.111.114.119.97.114.100.95.118.115
forward_vs
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文