getk Posted July 9, 2013 Posted July 9, 2013 (edited) Hi Friends, I'm new to autoit, so plz forgive me if this is a silly question We have got a config file as below IPAddress,ServerHostname,Env,Type 10.2.60.121,belgiumserver1,NP,WEB 10.2.61.121,belgiumserver2,NP,WEB 10.2.60.57,belgiumserver3,NP,WEB 10.2.60.82,belgiumserver4,DEV,WEB 10.2.60.98,belgiumserver5,DEV,WEB 10.2.60.99,belgiumserver6,DEV,WEB 10.2.61.101,belgiumserver7,DEV,WEB 10.2.90.82,belgiumserver8,PROD,WEB 10.2.58.82,belgiumserver9,PROD,WEB 10.2.90.83,belgiumserver10,PROD,WEB 10.2.58.83,denmark1,PROD,CORP 10.2.90.98,denmark2,PROD,CORP 10.2.90.99,denmark3,PROD,CORP 10.2.90.120,denmark4,PROD,CORP 10.2.91.82,denmark5,PROD,CORP 10.2.59.82,denmark6,PROD,CORP 10.2.91.98,denmark7,PROD,CORP 10.2.91.99,denmark8,PROD,CORP We are trying to automate "Putty" session creation using autoit & Putty_Portable ( Template Sample.. .... WinTitle\DYNAMIC_PUTTY_TITLE\ LogType\0\ LogFileName\putty.log\ HostName\DYNAMIC_HOSTNAME_IP\ .... Is there a ready-made script for such csv "Config" file lookup and string replacement? Cheers Edited July 11, 2013 by getk
water Posted July 9, 2013 Posted July 9, 2013 I use the following >script to read a CSV file into a 2D array. It should then be easy to create the output files. Another idea would be to use PLINK (a command-line interface to the PuTTY back ends). You will find a few examples how to use PLINK on this forum. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
getk Posted July 9, 2013 Author Posted July 9, 2013 I use the following >script to read a CSV file into a 2D array. It should then be easy to create the output files. Another idea would be to use PLINK (a command-line interface to the PuTTY back ends). You will find a few examples how to use PLINK on this forum. Thanks water. I will also use the above logic. can we find/replace string in a file with multiple arrays at a time or have to do one string and then loop through the entire file multiple times?
water Posted July 9, 2013 Posted July 9, 2013 Does the template change often so it needs to be saved in a file or is it possible to have the lines of the template "stored" in the script? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
getk Posted July 11, 2013 Author Posted July 11, 2013 Does the template change often so it needs to be saved in a file or is it possible to have the lines of the template "stored" in the script? Template should remain the same. But each template has around 200+ lines
Solution water Posted July 11, 2013 Solution Posted July 11, 2013 The following script reads the config file Config.txt, replaces the data in Template.txt and writes a chagned template for every entry in the config file to Output.txt: #include <File.au3> Global $aConfig, $aTemplate, $aValues, $sOutput ; Read config file _FileReadToArray(@ScriptDir & "\Config.txt", $aConfig) ; Read template file _FileReadToArray(@ScriptDir & "\template.txt", $aTemplate) ; Open output file $hOutput = FileOpen(@ScriptDir & "\Output.txt", 2) For $i = 2 To $aConfig[0] $aValues = StringSplit($aConfig[$i], ",") For $j = 1 To $aTemplate[0] $sOutput = StringReplace($aTemplate[$j], "\DYNAMIC_PUTTY_TITLE\", "\" & $aValues[2] & "\") ; ServerHostName $sOutput = StringReplace($sOutput, "\DYNAMIC_HOSTNAME_IP\", "\" & $aValues[1] & "\") ; IPAddress ; more replacements to fill in here FileWriteLine($hOutput, $sOutput) Next Next FileClose($hOutput) Is this what you need? Config.txtTemplate.txtOutput.txt My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
getk Posted July 11, 2013 Author Posted July 11, 2013 The following script reads the config file Config.txt, replaces the data in Template.txt and writes a chagned template for every entry in the config file to Output.txt: @water, much appreciated for your quick reply. I will have a check and get back to you..
getk Posted July 11, 2013 Author Posted July 11, 2013 #include <File.au3> Global $aConfig, $aTemplate, $aValues, $sOutput, $outDirectory, $tempVarHostName ; Read config file _FileReadToArray(@ScriptDir & "\Config.txt", $aConfig) ; Read template file _FileReadToArray(@ScriptDir & "\template.txt", $aTemplate) ; Open output file per item in Config File $outDirectory = "C:\Rough\PuttyList" For $i = 2 To $aConfig[0] $aValues = StringSplit($aConfig[$i], ",") $tempVarHostName = $aValues[2] $hOutput = FileOpen($outDirectory & "\" & $tempVarHostName & ".txt", 2) For $j = 1 To $aTemplate[0] $sOutput = StringReplace($aTemplate[$j], "\DYNAMIC_PUTTY_TITLE\", "\" & $tempVarHostName & "\") ; ServerHostName $sOutput = StringReplace($sOutput, "\DYNAMIC_HOSTNAME_IP\", "\" & $aValues[1] & "\") ; IPAddress ; more replacements to fill in here FileWriteLine($hOutput, $sOutput) Next FileClose($hOutput) Next @water, I just modified your script slightly, so that each iterations create individual output files. Again, your logic worked absolutely great and special thanks for your solution. Great help
water Posted July 11, 2013 Posted July 11, 2013 Glad this little script solved your "problem" My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
getk Posted July 16, 2013 Author Posted July 16, 2013 Thanks all. I have put article together to automate putty session and integration into keepass using AutoIT http://www.diaryfolio.com/putty-session-automation-and-portability/ Hope some body will benefit from it.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now