A script for enabling the Oracle service and changing the host name does not know what to start with. This is a question raised by the database teacher yesterday. Because the recovery card is installed on all the machines in the lab, the system will return to the initial state every time it is started or restarted, so the listener of the number of databases. ora, tnsnames. the host in the ora file is not the computer name of the local machine. It needs to be manually modified every time, which is very troublesome. You need to write a script to automate the task. I wrote a technical blog for the first time. I don't know whether the problem is clearly described. I have done this in two parts. To enable the Service and obtain the host name, run the batch processing command and pass the host name as a parameter to a python script to modify the file. The following is the batch processing part, with the file name start. bat:
01 @ echo off02rem this is the note 0304rem to get the computer name 05 python update. py % computername % 0607rem enable Oracle service 08net start OracleServiceORCL09net start OracleOraDb10g_home1TNSListener1011pauseupdate.py: 01 # coding = utf-802import sys0304print u "computer name:" 05 print sys. argv [1] 0607 print u "witness a miraculous moment... "0809 # modify the host name 10def update_file (path, old_name, new_name): 11" "12 python cannot directly modify the text content. I tried several methods and found that the following method is better. 13 first, retrieve the file content, find and replace the computer name, and overwrite the original file. 14 Note: You can write data when opening a file in w or w + mode. If the file name exists, the original content is cleared first. 15 "16 file = open (path, 'r + ') 17 content = file. read () 18 content = content. replace (old_name, new_name) 19 file. close () 20 file = open (path, 'W + ') 21 file. write (content) 22 file. close () 23 return2425 # path of the three files 26path1 = 'C:/oracle/product/10.2.0/db_1/NETWORK/ADMIN/listener. ora '27path2 = 'C:/oracle/product/10.2.0/db_1/NETWORK/ADMIN/tnsnames. ora '28path3 = 'C:/oracle/product/10.2.0/client_1/NETWORK/ADMIN/tnsnames. ora '1970 # name of the computer in the file and 31old_name of the computer on the local machine = 'sdwh119' 32new _ name = sys. argv [1] 3334update_file (path1, old_name, new_name) 35update_file (path2, old_name, new_name) 36update_file (path3, old_name, new_name)
Later, I found that although python is available in the lab, the environment variables are not configured. Therefore, we need to configure the python environment variable "C: \ Python27;" at each startup ;". Both scripts are simple, but they are not written at once. The batch processing content is now available. The following is a full batch processing method. I still don't want to understand the following symbols:
01@echo off02setlocal enabledelayedexpansion03cd\04cd oracle\product\10.2.0\db_1\NETWORK\ADMIN05for /f "delims=" %%a in (tnsnames.ora) do (06 set aa=%%a07 set aa=!aa:sdwh119=%computername%!08 echo !aa!>>temp.txt09)10del tnsnames.ora11ren temp.txt tnsnames.ora12for /f "delims=" %%a in (listener.ora) do (13 set aa=%%a14 set aa=!aa:sdwh119=%computername%!15 echo !aa!>>temp.txt16)17del listener.ora18ren temp.txt listener.ora19cd\20cd oracle\product\10.2.0\client_1\NETWORK\ADMIN21for /f "delims=" %%a in (tnsnames.ora) do (22 set aa=%%a23 set aa=!aa:sdwh119=%computername%!24 echo !aa!>>temp.txt25)26del tnsnames.ora27ren temp.txt tnsnames.ora28net start OracleServiceORCL29net start OracleOraDb10g_home1TNSListener