Finally, it’s the last PowerCLI Report Tip series! In this blog post, it will be discussing how PLINK could be used for advanced reporting. This will be quite short compared to other blog series and if you want to read previous series, they could be found below:
What is PLINK?
Many of you would already be familiar with SSH client. Putty if you are running Windows or terminal for Mac. PLINK is a command-line interface to the PuTTY back ends. More information could be found and downloaded here.
Why PLINK with PowerCLI?
Normally, PowerCLI would be enough for reports with information from vCenter Server and ESXi server. However, sometimes, reports will require additional information from external sources. I will explain reasons in Examples section.
How do you run PLINK with PowerCLI?
Running PLINK with PowerCLI is quite simple:
- Download the file
- Locate it under a folder
Example attached below:
PowerCLI C:\script\plink> .\PLINK.EXE PuTTY Link: command-line connection utility Usage: plink [options] [user@]host [command] ("host" can also be a PuTTY saved session name) Options: -V print version information and exit -pgpfp print PGP key fingerprints and exit -v show verbose messages -load sessname Load settings from saved session -ssh -telnet -rlogin -raw -serial force use of a particular protocol -P port connect to specified port -l user connect with specified username -batch disable all interactive prompts The following options only apply to SSH connections: -pw passw login with specified password -D [listen-IP:]listen-port Dynamic SOCKS-based port forwarding -L [listen-IP:]listen-port:host:port Forward local port to remote address -R [listen-IP:]listen-port:host:port Forward remote port to local address -X -x enable / disable X11 forwarding -A -a enable / disable agent forwarding -t -T enable / disable pty allocation -1 -2 force use of particular protocol version -4 -6 force use of IPv4 or IPv6 -C enable compression -i key private key file for authentication -noagent disable use of Pageant -agent enable use of Pageant -m file read remote command(s) from file -s remote command is an SSH subsystem (SSH-2 only) -N don't start a shell/command (SSH-2 only) -nc host:port open tunnel in place of session (SSH-2 only) -sercfg configuration-string (e.g. 19200,8,n,1,X) Specify the serial configuration (serial only)
Specifically, you could execute the following to run a command over a server:
C:\script\PLINK\plink.exe -pw "Password of the user" "Username@ServerName" "Command you want to run"
Assuming the connection is successfully made, let’s go through the tips.
Tips, PLINK with PowerCLI
When you run SSH command to a server for the first time, it asks you to accept the certification like the following:
PowerCLI C:\script\plink> .\PLINK.EXE -pw "Password" "Username@ServerName" "Command you want to run" The server's host key is not cached in the registry. You have no guarantee that the server is the computer you think it is. The server's dss key fingerprint is: ssh-dss 1024 aa:bb:cc:dd:ee:ff:gg:aa:bb:cc:dd:ee:ff:gg:aa:bb If you trust this host, enter "y" to add the key to PuTTY's cache and carry on connecting. If you want to carry on connecting just once, without adding the key to the cache, enter "n". If you do not trust this host, press Return to abandon the connection. Store key in cache? (y/n)
Instead of typing yes all the time for the first connection, you could simply use “echo” to avoid it. Use the following:
echo y | C:\script\PLINK\plink.exe -pw "Password of the user" "Username@ServerName" "Command you want to run"
When you save an output from PLINK to a PowerCLI variable, formatting it is not straightforward. Let’s have a look at example. Below is an output from Dell Remote Access Control (DRAC):
PowerCLI C:\script\plink> .\PLINK.EXE -pw ""email@example.com" "racadm getniccfg" IPv4 settings: NIC Enabled = 1 IPv4 Enabled = 1 DHCP Enabled = 0 IP Address = 10.10.10.1 Subnet Mask = 255.255.255.0 Gateway = 10.10.10.254 IPv6 settings: IPv6 Enabled = 0 DHCP6 Enabled = 1 IP Address 1 = :: Gateway = :: Link Local Address = :: IP Address 2 = :: IP Address 3 = :: IP Address 4 = :: IP Address 5 = :: IP Address 6 = :: IP Address 7 = :: IP Address 8 = :: IP Address 9 = :: IP Address 10 = :: IP Address 11 = :: IP Address 12 = :: IP Address 13 = :: IP Address 14 = :: IP Address 15 = :: LOM Status: NIC Selection = Dedicated Link Detected = Yes Speed = 100Mb/s Duplex Mode = Full Duplex
Let’s say you would like to pull out network settings, i.e. IP address/Subnet Mask/Gateway, how could we pull only three elements out? First of all, save the output to a variable:
PowerCLI C:\script\plink> $output = .\PLINK.EXE -pw ""firstname.lastname@example.org" "racadm getniccfg"
Good news is that the output is saved as an array, not to a single line meaning you could do the following:
PowerCLI C:\script\plink> $output IP Address = 10.10.10.1 PowerCLI C:\script\plink> $output Subnet Mask = 255.255.255.0 PowerCLI C:\script\plink> $output Gateway = 10.10.10.254
Then you could simply use -replace and -split functions to get the information you are after:
PowerCLI C:\script\plink> $output -replace " " IPAddress=10.10.10.1 PowerCLI C:\script\plink> ($output -replace " ") -split "=" IPAddress 10.10.10.1 PowerCLI C:\script\plink> $ipaddress = (($output -replace " ") -split "=") PowerCLI C:\script\plink> $ipaddress 10.10.10.1
Not a simple way but it’s achievable.
Let’s take a look at another example. This time, it’s an output from IBM SVC:
PowerCLI C:\script\plink> .\PLINK.EXE -pw "Password!" "UserName@ServerName" "lsvdisk" id name IO_group_id IO_group_name status mdisk_grp_id mdisk_grp_name capacity type FC_id FC_name RC_id RC_name vdisk_UID fc_map_count copy_count fast_write_state se_copy_count RC_change compressed_copy_count 0 vDisk1 0 io_grp0 online 0 MDISK1 1.00TB striped 6005076812345678A000000000000000 0 1 not_empty 0 no 0 1 vDisk2 0 io_grp0 online 1 MDISK2 1.00TB striped 6005076812345678A000000000000003 0 1 empty 0 no 0
Referring to the output, it won’t be as easy as the first example since the elements are separated with spaces that varies.
Thinking about the SVC command, it has the parameter to output with delimiter, i.e. CSV output:
PowerCLI C:\script\plink> .\PLINK.EXE -pw "Password!" "Username@ServerName" "lsvdisk -delim ,"PowerCLI C:\script\plink> output.csv id,name,IO_group_id,IO_group_name,status,mdisk_grp_id,mdisk_grp_name,capacity,type,FC_id,FC_name,RC_id,RC_name,vdisk_UID,fc_map_count,copy_count,fast_write_state,se_copy_count,RC_change,compressed_copy_count 0,vDisk1,io_grp0,online,0,MDISK1,striped,,,,,6005076812345678A000000000000000,0,1,not_empty,0,no,0 1,vDisk2,io_grp0,online,1,MDISK2,striped,,,,,6005076812345678A000000000000003,0,1,empty,0,no,0
Why would you want to output with delimiter “,”? The reason is simple, PowerCLI has CSV function Import-CSV and using this, formatting the output will be quite easy:
- > to save the output as a CSV file
- Then, run Import-Csv
PowerCLI C:\script\plink> $output > test.csv PowerCLI C:\script\plink> Import-Csv .\test.csv
Now, you will see formatted output:
id : 0 name : vDisk1 IO_group_id : 0 IO_group_name : io_grp0 status : online disk_grp_id : 0 mdisk_grp_name : MDISK1 capacity : 1.00TB type : striped FC_id : FC_name : RC_id : RC_name : vdisk_UID : 6005076812345678A000000000000000 fc_map_count : 0 copy_count : 1 fast_write_state : not_empty se_copy_count : 0 RC_change : no compressed_copy_count : 0 id : 1 name : vDisk2 IO_group_id : 0 IO_group_name : io_grp0 status : online mdisk_grp_id : 1 mdisk_grp_name : MDISK2 capacity : 1.00TB type : striped FC_id : FC_name : RC_id : RC_name : vdisk_UID : 6005076812345678A000000000000003 fc_map_count : 0 copy_count : 1 fast_write_state : empty se_copy_count : 0 RC_change : no compressed_copy_count : 0
This is a way of formatting the output nice and simple. One thing to note is that it will save the output to your local disk, e.g. C drive. I suggest you to delete the file once you save it to a variable, like this:
PowerCLI C:\script\plink> .\PLINK.EXE -pw "Password!" "Username@ServerName" "lsvdisk -delim ,"PowerCLI C:\script\plink> output.csv PowerCLI C:\script\plink> $output = Import-Csv output.csv PowerCLI C:\script\plink> Remove-Item output.csv
Time to go through examples.
I already wrote a blog post Virtual Machines running on vDisks in relationships. The purpose of this report was to find which virtual machines are mapped with VMFS volumes that are in Metro Mirror relationship, i.e. synchronous replication. Why would you need PLINK for this?
Even though the naming convention across VMFS volumes is solid to represent which ones are in relation, administrators make mistakes. Meaning that if there are VMFS volumes with wrong naming convention, then the report will not be accurate. Hence, I decided to match against UID so the report is 100% right! For more information, refer to the link attached above.
To check custom services running on ESXi servers:
PowerCLI C:\script\plink> Get-VMHostService -VMHost (Get-VMHost -Name "ESXi.test.com") Key Label Policy Running Required --- ----- ------ ------- -------- DCUI Direct Console UI on True False TSM ESXi Shell off False False TSM-SSH SSH on True False lbtd lbtd on True False lsassd Local Security Authenticati... off False False lwiod I/O Redirector (Active Dire... off False False netlogond Network Login Server (Activ... off False False ntpd NTP Daemon on True False sfcbd-watchdog CIM Server on True False snmpd snmpd on False False vmware-fdm vSphere High Availability A... off False False vprobed vprobed off False False vpxa vpxa on True False xorg xorg on False False
How about if there is a custom VIB installed, for example HP AMS and want to check the status? Unfortunately, Get-VMHostService won’t show this.
The way of checking the status is, SSH to ESXi directly and run /etc/init.d/hp-ams.sh status:
PowerCLI C:\script\plink> .\PLINK.EXE -pw "Password!" "Username@ESXiServer" "/etc/init.d/hp-ams.sh status"
Hope the PowerCLI Report Tip series helped and in near future, I will come back with PowerCLI automation tips.
As always, feel free to leave a comment for any clarifications.