Page 1 of 1 1
Topic Options
#210745 - 2015-09-16 06:10 PM Creating variable name from a variable
Kevin_J_Murphy Offline
Just in Town

Registered: 2015-09-16
Posts: 4
Loc: NH
I would like to create a variable based on the name of the server being processed by my kix script.

Is there a way that I can create a variable who's name is determined in the script itself?

The script opens up a text file and reads server names from it, processing 1 line at a time. During this script, I'd like to create a variable based on the current server being processed.

For example, if I am processing Server01, I'd like the script to establish a $Server01 variable.

Is this possible?


Kevin Murphy

Top
#210746 - 2015-09-16 06:28 PM Re: Creating variable name from a variable [Re: Kevin_J_Murphy]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Is there a reason you need to do this? Just seems overly complicated. Maybe you could explain what you are trying to accomplish and we can show you a better way.

Anyway...
 Code:
$ServerName="Server01"
$RC=execute("$" + $servername + "=" + $servername )
? $server01

Top
#210747 - 2015-09-16 07:14 PM Re: Creating variable name from a variable [Re: Allen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
What Allen said. It is possible but it actually conplicates things like diagnosing your script when it fails due to bad input.
_________________________
!

download KiXnet

Top
#210750 - 2015-09-17 03:12 PM Re: Creating variable name from a variable [Re: Allen]
Kevin_J_Murphy Offline
Just in Town

Registered: 2015-09-16
Posts: 4
Loc: NH
1st of all, I'm not a programmer \:\)

FWIW( I like the snip above and don't think that's complicated at all)

I have a simple script that reads a text file with server names in it. The script pings each server and displays each server name with UP or DOWN for each as appropriate. It pauses 30 seconds and cycles the list again.

I use it to monitor them while being rebooted during patching. Keeping visual track of each of them showing UP, then DOWN, the UP again is easy enough with a small list, and harder with larger ones.

I want to introduce a "Phase" to it where the initial UP is considered Phase1, the DOWN is Phase2 (during rebooting), then the final UP would be Phase3.

So, to me that means creating a variable for each server with this Phase setting that would be incremented as the server when UP then DOWN then back UP again. Since the list is always different then I was hoping to create a couple variables such as:

$Phase To track the server's phase (i.e. 1 2 or 3)
$Status To capture the current UP or DOWN for incrementing Phase#

= the server name in the list currently being processed.

Right now I get output like this:
UP (or down)

I'd like to have add the phase so that I can tell if it ever went down or not (and back up again):
UP Phase_1 (or _2 or _3 as detected)

Top
#210751 - 2015-09-17 03:31 PM Re: Creating variable name from a variable [Re: Kevin_J_Murphy]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
What would be the distinguishing factor between phase 3 and a return to phase 1?
Top
#210753 - 2015-09-17 04:03 PM Re: Creating variable name from a variable [Re: Allen]
Kevin_J_Murphy Offline
Just in Town

Registered: 2015-09-16
Posts: 4
Loc: NH
The fact that the phase number would already at 2 for Down
Top
#210754 - 2015-09-17 04:06 PM Re: Creating variable name from a variable [Re: Lonkero]
Kevin_J_Murphy Offline
Just in Town

Registered: 2015-09-16
Posts: 4
Loc: NH
The other thing about doing that way is that how to I do IF statements on the contents of that newly created variable?

One way I thought of is to create them, and then use another execute function to copy the status and phase variable to generic ones that the script can then do IF statements on.

Top
#210755 - 2015-09-17 05:06 PM Re: Creating variable name from a variable [Re: Kevin_J_Murphy]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Yikes! Using per-host vars is complicated!
Using some UDFs:
 Code:
; load external functions via call if you don't copy/paste the content directly into this script
Call '.\FileIO.kxf'
Call '.\Ping.kxf'

; clear DONE flag
$Done = 0

; load list of monitored devices into an array
$aTargets = FileIO('.\ServerList.txt', 'R')
; Create a status array to match the targets
Dim $aStatus[UBound($aTargets)]
; Initialize the status array to Status:1
For $ = 0 to UBound($aStatus)
  $aStatus[$P] = 1
Next

; loop until all devices are at Status:3
While Not $Done
  Cls
  $Done = 1			; assume complete
  ; enumerate the list of targets
  For $P = 0 to UBound($aTargets)
    If Ping($Target)		; host is up - responds to Ping
      If $aStatus[$P] = 2	; prior status was down
        $aStatus[$P] = 3	; log the transition from down to up (Status:2 to Status:3)
      EndIf
      If $aStatus[$P] = 1	; not rebooted yet
        $Done = 0		; this host hasn't rebooted, we're not done
      EndIf
    Else
      $aStatus[$P] = 2		; Host is down - set Status:2
      $Done = 0			; this host is (still) down, we're not done
    EndIf    
    $Target ' is at Stage: ' $aStatus[$P] @CRLF
  Next
  Sleep 60			; wait 60 seconds and do it all again
Loop
'All hosts have completed the reboot process.' @CRLF
This is untested, and you should declare the vars, but basically, it loads a list of hostnames into an array. It creates a duplicate array to hold the per-host status, setting it to "1". It pings each host. If the host responds AND the status is 1, it does nothing but clear the DONE flag. If the host responds AND the status is 2, it moves to Stage:3 - reboot complete. If the host doesn't respond, it sets the Stage to 2 (down) and clears the DONE flag.

As long as the DONE flag is 0, the loop repeats the process (after the delay). When all hosts have rebooted and come back up (transitioned from stage 2 to 3), the DONE flag will not be cleared and the loop will exit.

You might want to add a test to insure that the process only runs 60 times (one hour, assuming a 60-second delay between loops). If you want this, just add a $L value, increment it prior to the Loop statement, and add If $L > 60 $Done = 1 EndIf.

In this case, there is never a transition from stage 3 back to 1. The program ends when all monitored systems have rebooted. Running it again starts from state 1.

You will need to add / modify the status messages as your requirements dictate.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#210756 - 2015-09-18 02:55 AM Re: Creating variable name from a variable [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
Thanks for the silver platter code Glenn!
Would have been gold, but it requires fileIO, so you get silver.

Kevin, like pointed out before, it is doable, but what you want is way easier to accomplish with Glenn's code.
_________________________
!

download KiXnet

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 739 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.17 seconds in which 0.127 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org