VMware Cloud Community
dshivamu
Contributor
Contributor
Jump to solution

need help to run perl script by invoking ESX ssh command in power CLI

I have tried with invoking VCLI function but my perl script is not working.. so i want to upload my perl script file to ESX (not ESxi) then run the command that will output the value in /tmp directory then pull output file from /tmp to local VC any directory

something like this article using plink.exe http://communities.vmware.com/message/1260100#1260100

the command i want to run "perl iam_extract_global.pl --customer <customer name> --outfile /tmp/xxxxxx.mef"


then i need to pul /tmp/xxxxx.mef to local directory

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try placing the string values in the first 3 lines between quotes.

$User = "root"
$Pswd = "xxxxx"
$ESXname = "xxxxxxx"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
15 Replies
LucD
Leadership
Leadership
Jump to solution

With the plink command, like in the thread you refered to, you can execute that command in the COS. Place the command in the $cmd1 variable.

Once the file is created, you should be able to do a 'cat /tmp/xxxxxx.mef', which allows you to capture the content of the file in a variable in your PowerCLI session.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

dshivamu
Contributor
Contributor
Jump to solution

Hi


Thanks for response, I can run place the command but fact is we have 50+ ESX servers where we have to upload the perl script file and then execute, so we would require expert script to do my function ? can you help me here?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, I missed that upload part.

You could use the pscp.exe command from the PuTTY suite to transfer the Perl script to each ESX server and then execute it with plink.exe.

See Copying files ONTO esx hosts for an example use of the pscp.exe command.

Note that you can also use the pscp.exe to copy the result file back to your PC.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
dshivamu
Contributor
Contributor
Jump to solution

Just edited the command and ran but gives some error( note that i have uploaded file manually to ESX box to /tmp directory)

$User = root
$Pswd = xxxxx
$ESXname = xxxxxxx


$plink = "C:\Users\pf09\Desktop\PuTTY\plink.exe"
$plinkoptions = " -v -batch -pw $Pswd"
$cmd1 = '/tmp/perl iam_extract_global.pl --customer xxxxx'

$remoteCommand = '"' + $cmd1 + '"'
$command = $plink + " " + $plinkoptions + " " + $User + "@" + $ESXname + " " + $remoteCommand

$msg = Invoke-Expression -command $command

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What was the error you got ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

dshivamu
Contributor
Contributor
Jump to solution

Luc:

below is the error

PowerCLI C:\users\pf09\desktop\script\urt> .\pslink.ps1
The term 'root' is not recognized as the name of a cmdlet, function, script fil
e, or operable program. Check the spelling of the name, or if a path was includ
ed, verify that the path is correct and try again.
At C:\users\pf09\desktop\script\urt\pslink.ps1:1 char:13
+ $User = root <<<<
    + CategoryInfo          : ObjectNotFound: (root:String) [], CommandNotFoun
   dException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'PASSW0RD' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was in
cluded, verify that the path is correct and try again.
At C:\users\pf09\desktop\script\urt\pslink.ps1:2 char:17
+ $Pswd = PASSW0RD <<<<
    + CategoryInfo          : ObjectNotFound: (PASSW0RD:String) [], CommandNot
   FoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'aesx05' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At C:\users\pf09\desktop\script\urt\pslink.ps1:3 char:36
+ $ESXname = aesx05 <<<<
    + CategoryInfo          : ObjectNotFound: (albesx05.millercoors.com:String
   ) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Invoke-Expression : Unrecognized token in source text.
At C:\users\pf09\desktop\script\urt\pslink.ps1:13 char:25
+ $msg = Invoke-Expression <<<<  -command $command
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseExcep
   tion
    + FullyQualifiedErrorId : UnrecognizedToken,Microsoft.PowerShell.Commands.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try placing the string values in the first 3 lines between quotes.

$User = "root"
$Pswd = "xxxxx"
$ESXname = "xxxxxxx"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
dshivamu
Contributor
Contributor
Jump to solution

its worked

but when I run the my perl command it shows followin error

Server sent command exit status 127
bash: /tmp/perl: No such file or directory

--------------------------------------

I have used in script following command

$plink = "C:\Users\pf09\Desktop\PuTTY\plink.exe"

$plinkoptions = " -v -batch -pw $Pswd"

$cmd1 = '/tmp/perl iam_extract_global.pl --customer xxxxx'

$remoteCommand = '"' + $cmd1 + '"'

$command = $plink + " " + $plinkoptions + " " + $User + "@" + $ESXname + " " + $remoteCommand

$msg = Invoke-Expression -command $command

Any issue in above command

and Can you please let me know how can it run it for bulk of ESX server i belive we can replace the command Get-VMhost for $hostname?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I don't think the Perl module is located in the /tmp directory.

Your .pl file could be in the /tmp directory.

Can't you do

$cmd1 = 'perl /tmp/iam_extract_global.pl --customer xxxxx'


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
dshivamu
Contributor
Contributor
Jump to solution

still no luck i am getting below error message

Disconnected: All channels closed
Can't open perl script "/tmp/iam_extract_global.pl": No such file or directory

after changing $cmd1 = 'perl /tmp/iam_extract_global.pl --customer xxxxx'

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you sure the .pl file is in the /tmp folder ?

Is the name correct ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
dshivamu
Contributor
Contributor
Jump to solution

Yes luc, it is correct, file name and path

Can we check where it lands i mean which path when it login to ssh using plink? so that i can copy that file in that location only?

Reply
0 Kudos
dshivamu
Contributor
Contributor
Jump to solution

Apology once space is missing 🙂 it is working, lastly can I have two things now

1. How we can run it for multiple ESX,

2. Can I have command to force to say 'YES' for RSA keys it is ssh to the esx server

Reply
0 Kudos
dshivamu
Contributor
Contributor
Jump to solution

thanks Luc for your help, I made it myself running for all servers in cluster

But i couldn't able to remove prompt of RSA keys, i have to interactely login for each server then running the script

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

1. You set up a loop through all your ESX servers and call the plink.exe for each of them.

For example

Get-VMHost | %{

     $esxName = $_.Name

     ...

     plink.exe ... $esxName

     ...

}

2. I'm afraid there is no easy way to do this, as explained by the PuTTY people here.

It would in fact allow you to bypass the SSH security.

You can try the rather old Quest version of plink.exe which has a '-auto_store_key_in_cache' parameter.

But I haven't tried it recently I have to admit.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos