View Single Post
  #1  
Old 10-11-2019, 02:48 PM
mmckeehan mmckeehan is offline
Registered User
 
Join Date: Oct 2019
Posts: 2
VB Script to automate deploying RSA

Hello,

I am attempting to deploy RSA keys across around 4000 servers for my account, however they do exist on some servers already. I have a script that will deploy them if they did not exist already (below):

Code:
#$language = "VBScript"
#$interface = "1.0"
' the above tells the system what language and interface version we are using
'
'The below tells the system to keep the commands and the screen results in sync and start the script
'
crt.Screen.Synchronous = True

Sub Main

' this section sets the variables for reading the server list from a file
'

Dim fso, file, str
  Set fso = CreateObject("Scripting.FileSystemObject")

  ' Note: A runtime exception will be generated if 'UnlockHosts.txt' doesn't exist.
  ' the below sets the location for the hostfile
  '
  Set file = fso.OpenTextFile("C:\Hostfiles\scphosts.txt")

  crt.Screen.Synchronous = True

  Do While file.AtEndOfStream <> True

    str = file.Readline

    ' the below will send the commands shown and send the line with an appended CR
    ' note the wait for system prompt between each command this is required or the script will run to fast and not execute commands properly on the servers.

    ' check the failed login and reset then set the password, unlock the password then set it temp
    ' just edit the user ID
    '
    crt.Screen.Send "ssh -n -q " & str & " 'mkdir .ssh'" &chr(13)
	crt.Screen.WaitForString "sword:"
    crt.Screen.Send "password" &chr(13)
    crt.Screen.WaitForString "$"
    crt.Screen.Send "scp /local_home/uname/.ssh/authorized_keys uname@" & str & ":/local_home/uname/.ssh/authorized_keys" &chr(13)
	crt.Screen.WaitForString "sword:"
    crt.Screen.Send "password" &chr(13)
    crt.Screen.WaitForString "$"

	Loop
	End Sub
The servers are on saved to a .txt file that we are importing. When the keys are on a server, we get a message "File exist" To get around this I tried the below but keep getting an error:

Code:
#$language = "VBScript"
#$interface = "1.0"
' the above tells the system what language and interface version we are using
'
'The below tells the system to keep the commands and the screen results in sync and start the script
'
crt.Screen.Synchronous = True

Sub Main

' this section sets the variables for reading the server list from a file
'

Dim fso, file, str, result
	'Creates an array of the outcomes'
	result = Array ("File exists", _
					"sword:")

  Set fso = CreateObject("Scripting.FileSystemObject")

  ' Note: A runtime exception will be generated if 'UnlockHosts.txt' doesn't exist.
  ' the below sets the location for the hostfile
  '
  Set file = fso.OpenTextFile("C:\Hostfiles\scphosts.txt")

  crt.Screen.Synchronous = True

  Do While file.AtEndOfStream <> True

    str = file.Readline
	
   nResult = crt.Screen.WaitForString (result, 30)

    ' the below will send the commands shown and send the line with an appended CR
    ' note the wait for system prompt between each command this is required or the script will run to fast and not execute commands properly on the servers.

    ' check the failed login and reset then set the password, unlock the password then set it temp
    ' just edit the user ID
    '
    crt.Screen.Send "ssh -n -q " & str & " 'mkdir .ssh'" &chr(13)
	Select Case nResult
		Case 1
			'File Exist:  This will loop the process'
			crt.Screen.Send "ssh -n -q " & str & " 'mkdir .ssh'" &chr(13)
	
		Case 2
			'Asking for password: this will continue the process'
			crt.Screen.Send "password" &chr(13)
			crt.Screen.WaitForString "$"
			crt.Screen.Send "scp /local_home/uname/.ssh/authorized_keys uname@" & str & ":/local_home/uname/.ssh/authorized_keys" &chr(13)
			crt.Screen.WaitForString "sword:"
			crt.Screen.Send "password" &chr(13)
			crt.Screen.WaitForString "$"
		End Select
		
Loop		
End Sub

My thought was to create an array of different returns from the servers because and set a 30 second timer because sometimes there could just not be a response from the server.
Reply With Quote