Computer Hope
Microsoft => Microsoft DOS => Topic started by: Patplays852 on April 25, 2009, 01:33:05 PM
-
Hello, I've tried to find what i was looking for on the forums and the internet and have not been able to find an answer yet.
Here is the situation:
I set up an ftp server on my friends computer (yippie) the bad news is his ISP gives him a dynamic EXTERNAL IP address... bad news for ftp'ing lol. We found a program that sences when his external ip changes, and it uploads a text file to my ftp server with just the new IP in it.
I would like to make a batch file that runs every hour or so (i can do that) but i want it to DELETE the old entry in the hosts file and then add the new ip to the hosts file. (I have the text file setup so that it has:
***.***.***.*** [tab] dan
this way when i want to ftp into his server all i need to do is type:
ftp://dan:(port)
The key part that is stopping me from acomplishing this is that i need the old entry to be deleted, otherwise i am going to have multiple addresses for the same host (dan)
Thank you very much for your help =]
-
if you can use vbscript
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\host.txt"
Set objFile = objFS.OpenTextFile(strFile,1)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
splitted = Split(strLine)
splitted(0) = "new ip address"
WScript.Echo Join(splitted)
Loop
save as myscript.vbs and on command line
C:\test>cscript /nologo myscript.vbs
new ip address [tab] dan
redirect to new file as desired. (or do your modification inside the script, i leave it to you)
or if you can use tools like Perl (v5.8++)
C:\test>perl -lane "$F[0]=newip; print join(' ',@F);" host.txt
newip [tab] dan
-
Excuse my ignorance of vbscripts, but how does that find the line with the old ip address on it, or do i just need to make sure that it is on the last line?
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "D:\FROM DAN\IP\ipaddress.txt" //the text file with ip address in it.
Set objFile = objFS.OpenTextFile(strFile,1)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
splitted = Split(strLine)
splitted(0) = "new ip address"
WScript.Echo Join(splitted)
Loop
C:\FROM DAN\IP\>cscript /nologo myscript.vbs
new ip address [tab] dan
and im wondering in there where it points to the hosts file to update it?
-
i only assume you have 1 line in that host file. to check for old ip address, you can use Instr()
...
if Instr(strLine,"old ip address") > 0 then
wscript.echo "found "
' do the splitting here......
end if
to learn more about vbscript, download this (http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9) and read.
-
nope, hosts file has over 11k lines in it (thanks to spybot S&D)
I will download that now and see what i can do =] thank you very much for your help.
-EDIT (looks like im not ganna get it from windows site, wants me to install the genuine validation =P and i can't have that) lol Im still able to get windows updates, not sure if i install that plugin what will happen tho, and not ganna risk it.
If anyone has a batch file that can do this, my ears are still open
I can make the LAST line of the hosts file the ip that needs to be deleted and replaced. so if anyone can help with that info that would be greatly appreciated =]
-
nope, hosts file has over 11k lines in it (thanks to spybot S&
then show some samples and where the IP you need to change is. show also your sample output.
I will download that now and see what i can do =] thank you very much for your help.
-EDIT (looks like im not ganna get it from windows site, wants me to install the genuine validation =P and i can't have that) lol Im still able to get windows updates, not sure if i install that plugin what will happen tho, and not ganna risk it.
don't underestimate the power of googling.... check here (http://www.awarenetwork.org/home/outlaw/)
-
this is at the end of the hosts file atm:
127.0.0.1 www.searchdestroydownload.com
127.0.0.1 searchdestroydownload.com
127.0.0.1 win-loads.net
127.0.0.1 www.win-loads.net
127.0.0.1 www.win-pc-defender.com
127.0.0.1 win-pc-defender.com
# End of entries inserted by Spybot - Search & Destroy
127.0.0.1 activate.adobe.com
***.***.***.*** pat
***.***.***.*** dan
this is the ip.txt
?*?.?*?.?*?.?*? dan
i need to replace the last line of the hosts file [***.***.***.*** dan] with [?*?.?*?.?*?.?*? dan]
lol, was using three "?" but it came up as a smiley ???
note his ip is dynamic, and it wont have 3 numbers in each octet every time.
-
try this then
Set regEx = New RegExp
regEx.Pattern = "\s+" 'pattern to substitute all spaces/tabs with 1 space
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\host.txt"
strIPFile = "c:\test\ip.txt"
Set objFile = objFS.OpenTextFile(strIPFile,1)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
strLine = regEx.Replace(strLine, " ")
splitted = Split(strLine," ")
strIPtoChange = splitted(0)
strUserName = splitted(1)
Loop
Set objFile = objFS.OpenTextFile(strFile,1)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine,strUserName) > 0 Then
splitted = Split(strLine)
splitted(0) = strIPtoChange
strLine = Join(splitted," ")
End If
WScript.Echo strLine
Loop
-
ok, i have sync.bat, and sync2.vbs in the same folder as DansIP.txt
Sync.bat:
@echo off
cls
cd D:\FROMDAN\IP
start D:\FROMDAN\IP\>cscript /nologo sync2.vbs
new ip address dan
pause
I only get the error that new is not an internal/external command.
sync2.vbs:
Set regEx = New RegExp
regEx.Pattern = "\s+" 'pattern to substitute all spaces/tabs with 1 space
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\WINDOWS\system32\drivers\etc\hosts.txt"
strIPFile = "D:\FROMDAN\IP\DansIP.txt"
Set objFile = objFS.OpenTextFile(strIPFile,1)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
strLine = regEx.Replace(strLine, " ")
splitted = Split(strLine," ")
strIPtoChange = splitted(0)
strUserName = splitted(1)
Loop
Set objFile = objFS.OpenTextFile(strFile,1)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine,strUserName) > 0 Then
splitted = Split(strLine)
splitted(0) = strIPtoChange
strLine = Join(splitted," ")
End If
WScript.Echo strLine
Loop
hosts file remains unchanged.
-
you do not need to use start to call cscript. hostfile remain unchanged because all i do in the vbs is to echo out the changed lines. I leave it to you to figure out how to rename file. Its basic DOS
@echo off
cscript /nologo myscript.vbs > newfile
REM rename the newfile back to oldfile.
-
got another batch file to work:
@echo off
cls
pushd "%systemroot%\system32\drivers\etc"
type hosts|find /i /v "#dan's dynamic address" > hosts.new
move "%cd%\hosts.new" "%cd%\hosts"
for /f "usebackq" %%a in ("D:\FROM_DAN\IP\DansIP.txt") do (
>>hosts echo %%a dan #dan's dynamic address.
)
popd