Page 1 of 1 1
Topic Options
#123968 - 2004-07-27 11:41 AM how to edit the services file in windows ?
slorf Offline
Fresh Scripter

Registered: 2004-07-27
Posts: 5
Loc: Netherlands
HI All,

I am trying to use kixstart to edit multple ini files, which worked fine :-)

Now there is a bigger challange..

I have to edit the windows\system32\drivers\etc\services file.

Every user has local admin rights, so that should not be a problem.

In the services file there are a list of SAP entries, starting like this:

sapdp00 3200/tcp
sapdp01 3201/tcp
sapdp02 3202/tcp
sapdp03 3203/tcp

And it goes on and on.

Before the first one i have to insert:
SapmsPRD 3601/tcp

I tried to replace the first entry with the new one, and then append the original to the end, but this did not
work :-(

Does anyone have a clue ?



Top
#123969 - 2004-07-27 11:52 AM Re: how to edit the services file in windows ?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Hi Slorf and welcome to the board.

We use SAP as well and I might be wrong, but I don't think it has to be in a specific order within the file. You should be able to append your entry to the end of the file and I "think" it will work. Not 100% sure as it has been over a year since I helped to roll out SAP.

If you want to place it in a specific location then you'll need to use some of the Array UDFs to place the entire file into an array and then locate what you're looking for, then place it into that location, then write the file back out.

Top
#123970 - 2004-07-27 12:37 PM Re: how to edit the services file in windows ?
slorf Offline
Fresh Scripter

Registered: 2004-07-27
Posts: 5
Loc: Netherlands
we have been told this entry should be the first one if you are using "groups" in sap.

I tried using the following script to replace the first entry, but it did not work?

$sMyIni="c:\windows\system32\drivers\etc\services"
$sOldString="3200/tcp"
$sNewString="3601/tcp"

For Each $sSection in Split(ReadProfileString($sMyIni,"",""),Chr(10))
If $sSection
"Checking section "+$sSection+@CRLF
For Each $sEntry in Split(ReadProfileString($sMyIni,$sSection,""),Chr(10))
If $sEntry
$sValue=ReadProfileString($sMyIni,$sSection,$sEntry)
"Entry "+$sEntry+" has value "+$sValue+" status:"
If $sValue=$sOldString
" **UPDATED**"+@CRLF
$=WriteProfileString($sMyIni,$sSection,$sEntry,$sNewString)
Else
" No change"+@CRLF
EndIf
EndIf
Next
EndIf
Next

Top
#123971 - 2004-07-27 05:06 PM Re: how to edit the services file in windows ?
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
I had to edit that file at one point myself (not for SAP). So i just edited my script for your needs, but haven't tested.

Code:

Break ON
dim $path, $servicesFile, $newLine, $tempFile, $backUp

$newLine = "SapmsPRD 3601/tcp"
$path = "%windir%" + "\system32\drivers\etc\"
$servicesFile = $path + "services"
$tempFile = $path + "temp.txt"
$backUp = $servicesFile + ".bak"

if open(1, $servicesFile) = 0
if exist($tempFile)
del $tempFile
endif

if open(2, $tempFile, 5) = 0
$curLine = readline(1)
while $curLine <> "sapdp00 3200/tcp" and @error = 0
$ = writeline(2, $curLine + @crlf)
$curLine = readline(1)
loop

if $curLine = "sapdp00 3200/tcp"
$ = writeline(2, $newLine + @crlf)
endif

while @error = 0
$ = writeline(2, $curLine + @crlf)
$curLine = readline(1)
loop

$ = close(1)
$ = close(2)

copy $servicesFile $backUp
copy $tempFile $servicesFile
del $tempFile
else
$ = close(1)
endif
endif

exit

_________________________
Eric

Top
#123972 - 2004-07-28 10:46 PM Re: how to edit the services file in windows ?
acmp Offline
Getting the hang of it

Registered: 2004-07-02
Posts: 69
Loc: Bingham, Nottinghamshire, Engl...
Quote:

Every user has local admin rights,




Man that's scary.

Another approach to 'put it in an array and write it back' is to create a new file on your server with the correct first line.

Then use KiX to rename hte existing file, copy over the new 'header file' then pipe the contents of the 'old' file in.

This way may be easier (I used something similar to reset NT host files)
If you need some code let me know and I'll post it (it's at work and I'm not)

acmp<><
_________________________
Every day is a school day

Top
#123973 - 2004-07-28 11:28 PM Re: how to edit the services file in windows ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
slorf,
Since the file does not conform to INI rules, you cannot use INI APIs like ReadProfileString(). THere are UDFs on this site that will read in the entire file into an array with each line being an element. Simply read in each element (line), writing it out to a new file, until you reach the one where you want to insert. Insert what you want and write the remainder. Rename the original and then remame the new to replace.

Alternately, use one of the many QReplace utilities available and replace the line where you want to insert with the new line plus the existing line. How hard can it be?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#123974 - 2004-07-29 10:00 AM Re: how to edit the services file in windows ?
slorf Offline
Fresh Scripter

Registered: 2004-07-27
Posts: 5
Loc: Netherlands
hi,

if you can send me the code you used that would be great

Top
#123975 - 2004-07-29 10:01 AM Re: how to edit the services file in windows ?
slorf Offline
Fresh Scripter

Registered: 2004-07-27
Posts: 5
Loc: Netherlands
Our users have local admin rights ( only on their pc's ) if not, they would not be able to run certain s/w packages we have.
Top
#123976 - 2004-07-29 04:27 PM Re: how to edit the services file in windows ?
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Is there something wrong with the code i provided?
_________________________
Eric

Top
#123977 - 2004-07-29 04:56 PM Re: how to edit the services file in windows ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Quote:

if you can send me the code you used that would be great



maciep,
Guess he does not want your code but he did ask acmp to "send" him his. Not sure how he is supposed to do that since slorf does not have his email addy in his profile. Since his profile does not even list his "location" we cannot even hazard a guess whether he is language challenged.

noobs... what can I say?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#123978 - 2004-07-29 10:03 PM Re: how to edit the services file in windows ?
acmp Offline
Getting the hang of it

Registered: 2004-07-02
Posts: 69
Loc: Bingham, Nottinghamshire, Engl...
hey! I'm listed as a noob! I guess he just wants me to post the code here, which I will when I go into work tomorrow (friday).

acmp<><
_________________________
Every day is a school day

Top
#123979 - 2004-07-29 10:13 PM Re: how to edit the services file in windows ?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Should I change the board to reflect you as NOOB
Top
#123980 - 2004-07-29 10:14 PM Re: how to edit the services file in windows ?
acmp Offline
Getting the hang of it

Registered: 2004-07-02
Posts: 69
Loc: Bingham, Nottinghamshire, Engl...
Your code looks good to me, I just had another idea up my sleeve from a problem that I had.

The only draw back with your code that I can see is that you will always rewrite the file. Yeah, I know it's an easy thing to fix, just add a 'if not readline=whatever the do the code' bit and it's fixed.

Maybe we can all share code and come to an appreciation of others ideas?

acmp<><
_________________________
Every day is a school day

Top
#123981 - 2004-07-29 10:38 PM Re: how to edit the services file in windows ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
OK, if share we must... but not doing silver platter stuff.
Code:
break on
$File = 'C:\WINDOWS\system32\drivers\etc\services'
$NewFile = 'C:\WINDOWS\system32\drivers\etc\services.new'
$NewFileHandle = freefilehandle()
$LineToFind = 'hostname 101/tcp hostnames #NIC Host Name Server'
$LineToAdd = 'newline 100/tcp newline #test'
$Array = ReadFile($File,,)
$LineNum = AScan($Array,$LineToFind)
'Line Number = ' +$LineNum ?
$index = 0
If Open($NewFileHandle,$NewFile,5) = 0
For Each $Element in $Array
If $index = $LineNum
$ = WriteLine($NewFileHandle,$LineToAdd + @CRLF)
EndIf
$ = WriteLine($NewFileHandle,$Element + @CRLF)
$index = $index + 1
Next
EndIf



p.s. needs the ReadFile() UDF from here.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#123982 - 2004-07-30 09:58 AM Re: how to edit the services file in windows ?
slorf Offline
Fresh Scripter

Registered: 2004-07-27
Posts: 5
Loc: Netherlands
Hi there

Sorry, i did not know my location and email was not listed, i will try to change it.
Yes, i am very very new.. :-)

Top
#123983 - 2004-07-30 09:04 PM Re: how to edit the services file in windows ?
acmp Offline
Getting the hang of it

Registered: 2004-07-02
Posts: 69
Loc: Bingham, Nottinghamshire, Engl...
I think I'll fix that my self by getting my posts up, but thanks for the offer.

acmp<><
_________________________
Every day is a school day

Top
#123984 - 2004-07-30 09:12 PM Re: how to edit the services file in windows ?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Okay, if none of these solutions are sufficient then just walk out to these desktops, open up notepad and edit the file manually.

Does that count for a post count increase ?

Top
Page 1 of 1 1


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

Who's Online
0 registered and 302 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.072 seconds in which 0.023 seconds were spent on a total of 12 queries. Zlib compression enabled.

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