Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Unicode issues with emc uemcli

by pritesh_ugrankar (Monk)
on Sep 07, 2020 at 19:08 UTC ( [id://11121460]=note: print w/replies, xml ) Need Help??


in reply to Re: Unicode issues with emc uemcli
in thread Unicode issues with emc uemcli

Hi Haukex,

I get the following output. Please note. This time, I have not changed/modified the IP Address and the other information that was originally captured. I had earlier changed it in the output posted with the question for security purposes. Hope you understand. Thank you for your help and patience.

"\xFF\xFES\0t\0o\0r\0a\0g\0e\0 \0s\0y\0s\0t\0e\0m\0 \0a\0d\0d\0r\0e\0s +\0s\0:\0 \x001\x000\0.\x001\x009\x006\0.\x001\x002\x004\0.\x006\x004\ +0\n\0S\0t\0 o\0r\0a\0g\0e\0 \0s\0y\0s\0t\0e\0m\0 \0p\0o\0r\0t\0:\0 \x004\x004\x003 +\0\n\0H\0T\0T\0P\0S\0 \0c\0o\0n\0n\0e\0c\0t\0i\0o\0n\0\n\0\n\x001\0:\ +0 \0 \0 \0 \0S\0y\0s\0t\0e\0m\0 \0n\0a\0m\0e\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0=\ +0 \0C\0D\0R\0-\0V\0N\0X\0e\x001\0\n\0 \0 \0 \0 \0 \0 \0M\0o\0d\0e\0l\ +0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0=\0 \0V\0N\0X\0e\x003\x003\x0 +00\x000\0\n\0 \0 \0 \0 \0 \0 \0P\0l\0a\0t\0f\0o\0r\0m\0 \0t\0y\0p\0e\ +0 \0 \0 \0 \0 \0 \0 \0 \0 \0=\0 \0E\0M\0C\0 \0S\0t\0o\0r\0a\0g\0e\0 \0S\0y\0s\0t\ +0e\0m\0\n\0 \0 \0 \0 \0 \0 \0P\0r\0o\0d\0u\0c\0t\0 \0s\0e\0r\0i\0a\0l +\0 \0n\0u\0 m\0b\0e\0r\0 \0=\0 \0A\0P\0M\x000\x000\x001\x001\x004\x007\x000\x000\x +008\x009\x004\0\n\0 \0 \0 \0 \0 \0 \0A\0u\0t\0o\0 \0f\0a\0i\0l\0b\0a\ +0c\0k\0 \0 \0 \0 \0 \0 \0 \0 \0 \0=\0 \0o\0n\0\n\0 \0 \0 \0 \0 \0 \0H\0e\0a\0l\0t +\0h\0 \0s\0t\0a\0t\0e\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0=\0 \0O\0K\0 \0( +\x005\0)\0\ n\0 \0 \0 \0 \0 \0 \0H\0e\0a\0l\0t\0h\0 \0d\0e\0t\0a\0i\0l\0s\0 \0 \0 +\0 \0 \0 \0 \0 \0=\0 \0\"\0T\0h\0e\0 \0s\0y\0s\0t\0e\0m\0 \0i\0s\0 \0 +o\0p\0e\0r\ 0a\0t\0i\0n\0g\0 \0n\0o\0r\0m\0a\0l\0l\0y\0.\0\"\0\n\0\n\0"

Replies are listed 'Best First'.
Re^3: Unicode issues with emc uemcli
by haukex (Archbishop) on Sep 07, 2020 at 20:02 UTC

    That data is encoded with UTF-16LE. Although I'm not sure if there's a more elegant way to solve this because I'm not familiar with the "uemcli" command, by adding the following code to the example I posted above, you will get the properly decoded Perl string in $str.

    use Encode qw/decode/; my $str = decode('UTF-16', $out, Encode::FB_CROAK);

      Hi Haukex,

      All thanks to your clever code, I am finally able to capture the output to text files too!!. Here's the addition to your code.

      my $aref_cmd1= ['uemcli','-d',$vnxe_ip,'-u',$username,'-p',$password, +'/sys/general','show','-detail']; my $aref_cmd2 = ['uemcli', '-d', $vnxe_ip, '-u', $username, '-p', $pas +sword, '/env/bat', 'show', '-detail']; my $aref_cmd3 = ['uemcli', '-d', $vnxe_ip, '-u', $username, '-p', $pas +sword, '/env/ps', 'show', '-detail']; run3 $aref_cmd1, undef, \my $out1; run3 $aref_cmd2, undef, \my $out2; run3 $aref_cmd3, undef, \my $out3; use Encode qw/decode/; my $str1 = decode('UTF-16', $out1, Encode::FB_CROAK); print "$str1\n"; my $textfile2 = "textfile2.txt"; open (my $fh2, '+>', $textfile2) or die "Cannot open file.$!"; my $str2 = decode('UTF-16', $out2, Encode::FB_CROAK); print $fh2 $str2;

      Here, the cmd1 prints to screen, cmd2 can be output to a file. cmd3 I am yet to work on. Thank you once again..

        Of course, a loop would be a bit more elegant, here's just one example.

        use warnings; use strict; use IPC::Run3; use Encode qw/decode/; my @outputs; for my $loc ('/sys/general', '/env/bat', '/env/ps') { run3 ['uemcli','-d',$vnxe_ip,'-u',$username,'-p',$password, $loc,'show','-detail'], undef, \my $out; my $str = decode('UTF-16', $out, Encode::FB_CROAK); push @outputs, $str; } print $outputs[0], "\n"; open my $fh, '>', $textfile or die "$textfile: $!"; print $fh $outputs[1]; close $fh;

        Update: Note that both prints in this example assume you don't have any Unicode (non-ASCII) characters in the strings (as your sample data shows). If you did, you'd need to set an encoding on the filehandles you're writing the data to - that's STDOUT, with for example use open qw/:std :utf8/; or several variations on that, and for filehandles, for example open my $fh, '>:encoding(UTF-8)', $filename or die "$filename: $!";.

        Simpler:

        open (my $fh2, '>:encoding(UTF-16)', $textfile2)

      Hi Haukex,

      Yes, it works and perfectly shows the output!! Thank you so much.

      All I now how to figure out is how to put this my script so that I get the desired output. Quite frankly I'm not able to make much out of the clever stuff you wrote. You've wrapped the command in what I think is a anonymous aref along with undef, \my $out. Wish there were an easier way to do this, but , that is something for me to figure out. Thank you once again.

        You've wrapped the command in what I think is a anonymous aref along with undef, \my $out.

        Yes, all of this is part of the API of IPC::Run3, its documentation explains it. I used an arrayref because this will cause the module to do its best to avoid the shell (which is a slightly complicated topic on Windows, but at least newer versions of the module will use Win32::ShellQuote to help with some of the issues). The undef causes the child process to inherit STDIN from the parent, and \my $out causes the command's STDOUT to be stored in that variable. Then, I use Encode to decode the data from the command, which is encoded in UTF-16LE; note that I specify the encoding to simply be UTF-16 because the module is smart enough to use the BOM that is part of the output you showed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11121460]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found