PowerShell and Plink to Update Avamar Proxy Timeout Settings

When colleagues in the office have a repeatable process that can and should be scripted, I’m the person they seek out. This particular time it was a storage admin looking to see what could be done with verifying a setting in a file on a Linux appliance and changing the setting if it is not set to the preferred value.

My knowledge of Linux is very limited as I mostly only work with Windows based machines. I’m very eager to learn more about Linux, so I’m excited to work on a script like this. While this one seems more complex than the last Linux script that I wrote.

Avamar Backup Solution deploys virtual appliances (OVAs) that are used as proxies servers to perform backups of the VMs in the environment. They do this by generating a snapshot of the VM and mounting them to the proxy for backups. The default deployment of the OVA doesn’t have a timeout value in the configuration file. (shown below)

# If the snapshot check is preventing a backup or restore, the user can skip the
#    snapshot check instead of fixing the root cause. Default is false.
#
#--skip_snapshot_check=false
#
# The VM list in the logs can get long, so this allows a moderate list to show normally,
#    but also allows support to increase it's size. -1 displays all, default is 30.
#
#--max_vm_list_count= -1
#
# Adjust the amount of seconds to wait for a snapshot removal to complete.
#    Default = 4, minimum = 2, maximum = 9999
#
#--max_seconds_to_wait_for_snapshot_removal=4
#
#  If vMotion reservations are broken, the user can skip the checking
#    instead of fixing the root cause. Default is false.
#
#--vmotion_reservation_enable=false
#
# If true, quiesce the file system prior to snapshot. Default is true.
#
#--quiesce_fs=true
#
# If false, disk consolidation is disabled and backup will be aborted.Default is true.
#--consolidate_disks=true
#
#---------------------- Version 7.0 Flags ----------------------
#
# If enabled, the create VM request on a restore to a new VM will be done by the proxy,
#    not the Avamar system service. Default is true.
#
#--enableLocalVmCreation=true
#
# If enabled, report the backup/restore tasks in the vSphere Client recent tasks window.
#    Default is true.
#
#--enable_vsphere_ui_msgs=true

We have 36 Avamar proxy VMs, so making a simple change can take some time. Log into the first node, change to the directory, VI the file, make the edits, and then save and quit. Rise, wash, and repeat 35 more times. Before the storage admin came to me with this script request, he added the configuration line, “–snapshotManager_https_timeout=60000”, to all of the 36 proxy by hand.

Normally when I write scripts, I like to break them down into smaller blocks or steps to make them more manageable. So…

Step 1. Remote to the Proxy

I already have worked out out to script a remote connection to a Linux OS. Plink* is a command line interface for PuTTY that can be used for scripting. I have used this with another script and it worked great. This other script will be in another post.

$User = "Username"
$Pswd = "Password_Here"
$Computer = $Proxy.Name
$hostkey = "SSH_Host_Key_Here"
$plink = "C:\scripts\Applications\plink\plink.exe"
$plinkoptions = " -v -batch -pw $Pswd -hostkey $hostkey"

If you are unaware of how to get the ssh host key needed for this to work, you will need to make a connection to the Linux machine with PuTTY. If this is the first time you have connected to this machine with PuTTY, you will get an security alert.

Once you have accepted this alert, it will add a key to the registry, and you will not be prompted again. Per the Plink documentation you shouldn’t need to use the host key in the Plink command, but I found that I still need to use it as it command fails with “Disconnected: Aborted at host key verification

ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key
2048 SSH_HOST_KEY [MD5]  root@hostname (RSA)

Step 2. Parse for the Config Line

Now that we can make a connection to the Linux machine via Plink, we need to get build the command to find the line. Even with my limited Linux knowledge, I know to use the GREP** command.

$Command1 = "grep 'snapshotManager_https' /usr/local/avamarclient/var/avvcbimageAll.cmd"
$remoteCommand = '"' + $Command1 + '"'
$command = $plink + " " + $plinkoptions + " " + $User + "@" + $computer + " " + $remoteCommand
$msg = Invoke-Expression -command $command
$msg

Above, you can see that the GREP command is looking for ‘snapshotManager_https’ in the file avvcbimageAll.cmd. The way to get this to work, is all about the syntax. This was the part that took the longest to get correct. Once the $command variable has been populated its ready to be pushed to the machine.

When the command executes, it looks really bad. Normally red text in PowerShell is failure, but in the case its just how it runs. Since I stored the results of the command as a variable, I’m able to print it to the screen.

Step 3. Change the Value

Not going to lie, but this step stumped me. Finding the value was simple. Comparing the value to make sure its set correctly was easy using PowerShell. Changing the value from the incorrect value to the proper value, and that is where my head meets the wall. I combed the web for a solution, and found my command, SED.

If ($msg -ne $CorrectValue){
$Command2 = @'
echo Password | su root --command 'sed -i "s/$msg/$CorrectValue/g" /usr/local/avamarclient/var/avvcbimageAll.cmd'
'@
$remoteCommand2 = '"' + $Command2 + '"'
$command2 = $plink + " " + $plinkoptions + " " + $User + "@" + $computer + " " + $remoteCommand2
$msg2 = Invoke-Expression -command $command2
$msg3 = Invoke-Expression -command $command
$msg3
}

So the syntax for the SED*** command is:
-i – inplace edit
“s/$msg/$CorrectValue/g” – $msg – is the value to parse the file that needs to be changed
– $CorrectValue – is the new value that the previously parsed and found.
– /usr/local/avamarclient/var/avvcbimageAll.cmd – is the file that is getting parsed.

When this portion of the script executes, there isn’t any output returned as it modifies the file in place. So to verify that the SED command worked as intended, I ran the GREP command again.

Final Step. Put it all Together

The final script looks like this.

$AvamarProxies = Get-VM AvamarProxy* | Sort
$Command1 = "grep 'snapshotManager_https' /usr/local/avamarclient/var/avvcbimageAll.cmd"
$Date = Get-date -Format "MM-dd-yyyy"

foreach ($Proxy In $AvamarProxies){
    $User = "UserName"
    $Pswd = "Password"
    $Computer = $Proxy.Name
    $hostkey = "SSH_Host_Key"
    $plink = "C:\scripts\Applications\plink\plink.exe"
    $plinkoptions = " -v -batch -pw $Pswd -hostkey $hostkey"

    $remoteCommand = '"' + $Command1 + '"'
    $command = $plink + " " + $plinkoptions + " " + $User + "@" + $computer + " " + $remoteCommand

    $msg = Invoke-Expression -command $command
    $Computer | out-file C:\Scripts\logs\AvamarTimeout_$Date.txt -Append
    $msg | out-file C:\Scripts\logs\AvamarTimeout_$Date.txt -Append

    $CorrectValue = "--snapshotManager_https_timeout=120000"
    If ($msg -eq $null){
        # This line doesn't work as intended.
        $command3 = @'
echo Password | su root --command 'echo -n "$CorrectValue" >> /usr/local/avamarclient/var/avvcbimageAll.cmd'
'@
        $remoteCommand3 = '"' + $Command3 + '"'
        $msg4 = Invoke-Expression -command $command3
        $msg5 = Invoke-Expression -command $command
        $msg5 | out-file C:\Scripts\logs\AvamarTimeout_$Date.txt -Append
    }

    If ($msg -ne $CorrectValue){
$Command2 = @'
echo Password | su root --command 'sed -i "s/$msg/$CorrectValue/g" /usr/local/avamarclient/var/avvcbimageAll.cmd'
'@
        $remoteCommand2 = '"' + $Command2 + '"'
        $command2 = $plink + " " + $plinkoptions + " " + $User + "@" + $computer + " " + $remoteCommand2
        $msg2 = Invoke-Expression -command $command2
        $msg3 = Invoke-Expression -command $command
        $msg3 | out-file C:\Scripts\logs\AvamarTimeout_$Date.txt -Append
    }
}

In the final version of the script, I do have some simple logging so I can see the VM name and the parsed value before and after if it got changed.

– Stuart

* https://the.earth.li/~sgtatham/putty/0.73/htmldoc/Chapter7.html#plink
** https://www.gnu.org/software/grep/manual/grep.html
*** https://www.gnu.org/software/sed/manual/sed.html

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.