Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: system commands/shell and perl variables.

by haukex (Archbishop)
on Jan 22, 2019 at 15:02 UTC ( [id://1228819]=note: print w/replies, xml ) Need Help??


in reply to system commands/shell and perl variables.

With a command as complex as you showed, I would very much recommend doing as much of it in Perl as possible instead of shelling out - there are potential security issues, quoting challenges, etc. with shelling out. I wrote about that at length here (with code examples). In particular, both fgrep and awk can be replaced by pure Perl. Instead of ps, you might try Proc::ProcessTable. That leaves only xl and sockstat. If you want to run those external processes and need to feed something to their STDIN, I'd recommend IPC::Run3. Otherwise, if you just want a simple replacement for backticks, there's capturex from IPC::System::Simple.

Replies are listed 'Best First'.
Re^2: system commands/shell and perl variables.
by knight.neo (Initiate) on Jan 22, 2019 at 17:34 UTC
    Hi there, First of all thank you very much. I am starting with perl and I trying to automate my shell scripts by perl scripts. Here my final script ( of course I will follow HAUKEX advice which is what I want.
    #!/usr/local/bin/perl use strict; use warnings; my @get_vmlist = ( `xl list` ); sub show_vmlist { print @get_vmlist; } my $arg = shift @ARGV; my $vm_run = pop @ARGV; sub get_vncport { my $DOMID = qq(DOM_ID="`xl list | fgrep $vm_run | awk '{print +\$2}'`" && VNCPORT="`ps x | fgrep -m 1 "domid \$DOM_ID" | awk '{print + \$1}'`" && sockstat -4l | fgrep \$VNCPORT | awk '{print \$6}'); print `$DOMID`; } my %rec_args = ( vmlist => \&show_vmlist, getvnc => \&get_vncport, ); if (defined $arg && exists $rec_args{$arg}) { $rec_args{$arg}->(); }
        Hi haukex, Absolutely , xl is part of xen-tools. xl is used to manipulate virtual machines as other system parameters as well. Here is a tipical output of "xl list" ( used to list running virtual machines ) I am using FreeBSD as dom0. When I start a VM on HVM mode, I use VNC to access vm console.
        # xl list Name ID Mem VCPUs State + Time(s) Domain-0 0 18442 2 r----- + 16256.2 git 1 1023 2 -b---- + 14499.4 zabbix 2 512 1 -b---- + 3228.6 jenkins 3 4096 2 -b---- + 21733.3 openbsd 4 2048 1 -b---- + 1202.6 wiki 5 2048 1 -b---- + 1140.4 builder 6 4096 2 -b---- + 206.9
        This perl script which you guys helped, do the follow: "pxl vmlist" ( which will return xl list ), and "pxl getvnc jenkins" ( which will return vnc port listening of that VM ) "127.0.0.1:5906" Next steps, I will provide ssh port forward and connect to it :) Thanks again

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-28 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found