Recent

Author Topic: Your ip adress and the WebServer...  (Read 3738 times)

aminer

  • Hero Member
  • *****
  • Posts: 956
Your ip adress and the WebServer...
« on: January 24, 2013, 10:48:36 pm »
Hello all,


If your provider give you an IP that is changed in every period,
here is a program that i have wrote that updates your HTML web pages with the new IP and updates also the IP of your webserver programaticaly and restart your webserver and also upload the necessary files with an FTP client to your ftp server  of your internet provider.

Here is the VB script that i have wrote that will be called by the Object Pascal program:

===

dim objWebApp
dim intArraySize
dim arrOldBindings
dim arrNewBindings

Set oArgs = WScript.Arguments

Set objWebApp = GetObject("IIS://localhost/w3svc/1")

If isArray(objWebApp.ServerBindings) then

arrOldBindings = objWebApp.ServerBindings
    intArraySize = UBound(arrOldBindings)
    Redim arrNewBindings(intArraySize + 1)

    For i = 0 to intArraySize
    arrNewBindings(i) = arrOldBindings(i)
    Next
WScript.Echo oArgs(0)
str=oArgs(0)

 arrNewBindings(intArraySize) = str
'"12.12.12.12:88:"
    objWebApp.Put "ServerBindings", (arrNewBindings)
    objWebApp.SetInfo

End If

==



And here is the Object Pascal program that i have wrote for you


===

program test;

uses windows,classes,sysutils,Winsock,ALWinInetFTPClient;

var list:TStrings;
 i:integer;
 fstream:TFilestream;
 StrIP:string;
 handle:thandle;
ftp:TALWinInetFTPClient;

{...}

function ShellExecute(hWnd: HWND; Operation, FileName, Parameters,
  Directory: PChar; ShowCmd: Integer): HINST; stdcall;external 'shell32.dll' name 'ShellExecuteA';


function Stream2String(Stream1: TStream;var Mystring:string): boolean;
begin
result:=true;
try
 try
   Stream1.Position := 0;
   SetLength(Mystring, Stream1.Size);
   Stream1.ReadBuffer(Pointer(Mystring)^, Stream1.Size);
   
 finally
  // Stream1.Free;
 end;
except
result:=false;
end;

end;


function getIPs: Tstrings;
type
TaPInAddr = array[0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: array[0..63] of Char;
I: Integer;
GInitData: TWSAData;
begin
WSAStartup($101, GInitData);
Result := TstringList.Create;
Result.Clear;
GetHostName(Buffer, SizeOf(Buffer));
phe := GetHostByName(buffer);
if phe = nil then Exit;
pPtr := PaPInAddr(phe^.h_addr_list);
I := 0;
while pPtr^ <> nil do
begin
Result.Add(inet_ntoa(pptr^^));
Inc(I);
end;
WSACleanup;
end;

procedure FileReplaceString(const FileName, searchstring, replacestring: string);
var
  fs: TFileStream;
  S: string;
begin
  fs := TFileStream.Create(FileName, fmOpenread or fmShareDenyNone);
  try
    SetLength(S, fs.Size);
    fs.ReadBuffer(S[1], fs.Size);
  finally
    fs.Free;
  end;
  S  := StringReplace(S, SearchString, replaceString, [rfReplaceAll, rfIgnoreCase]);
  fs := TFileStream.Create(FileName, fmCreate);
  try
    fs.WriteBuffer(S[1], Length(S));
  finally
    fs.Free;
  end;
end;


begin

repeat

fstream:=TFileStream.create('ip.txt',fmOpenReadWrite);
Stream2String(fstream,StrIP);

list := GetIps;

If StrIP <> list[0]
 then
  begin
   FileReplaceString('Tree.html',StrIP,list[0]);
   FileReplaceString('Home.html',StrIP,list[0]);
   fstream.size:=0;
   fstream.WriteBuffer(list[0][1], Length(list[0]));
   ShellExecute(Handle,'open', pchar('cscript.exe'),pchar('iis.vbs '+list[0]+':88:'),nil,SW_HIDE{SW_SHOWNORMAL});
ShellExecute(Handle,'open', pchar('net.exe'),pchar(' stop   w3svc'),nil,SW_HIDE{SW_SHOWNORMAL});
ShellExecute(Handle,'open', pchar('net.exe'),pchar(' start    w3svc'),nil,SW_HIDE{SW_SHOWNORMAL});

ftp:=TALWinInetFTPClient.create(nil);

ftp.UserName:='****l';
ftp.Password:='****';
ftp.serverport:=21;

ftp.ServerName:='pages.videotron.com';
ftp.transfertype:=wFtpTt_BINARY;
ftp.connect;
ftp.PutFile('home.html','home.html');
//ftp.PutFile('tree.html','tree.html');

ftp.disconnect;

ftp.free;


end;

list.free;

fstream.free;

sleep(1000*60*5);
until false;

end.

===


and you can download TALWinInetFTPClient v.3.79 that i am usgin from:

http://www.torry.net/quicksearchd.php?String=ftp+client&Title=Yes


And don't forget to configure tour firewall to alloe active FTP clients,

here is how you can do it for Windows 2008 server:

http://morndej.wordpress.com/2012/05/17/how-to-add-windows-2008-firewall-rule-to-allow-active-ftp-client/





Thank you,
Amine Moulay Ramdane.




« Last Edit: January 24, 2013, 11:00:04 pm by marcov »

aminer

  • Hero Member
  • *****
  • Posts: 956
Re: Your ip adress and the WebServer...
« Reply #1 on: January 24, 2013, 10:52:15 pm »


Hello,

You have to wrote your IP adress inside the IP.txt file
before running the program.


You can download Lazarus or Freepascal to compile the program
from:

http://www.lazarus.freepascal.org/

or

http://www.freepascal.org/


Thank you,
Amine Moulay Ramdane.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8733
  • FPC developer.
Re: Your ip adress and the WebServer...
« Reply #2 on: January 24, 2013, 11:03:48 pm »
1. call me evil, but if I see hostnames, accountnames and passwords in sourcecode, I just have to try them out. Since they seem to work, and a single command could wipe your website, I took the liberty of editing your post and inserting some asterisks.

2.  Since your program is hopelessly windows dependent, I would not shell out to hapless shell programs, but use proper apis, like jwawinsvc for services and jwaliphlpapi for network interface control.  Oh, and in all windows versions that are not obsolete withinin the year, you will run into UAC.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Your ip adress and the WebServer...
« Reply #3 on: January 25, 2013, 10:35:24 am »
My eyes hurt... if you want to, you can format your code by enclosing it with code tags (use the Insert code button which looks like # when editing your post).
Makes for much more readable code.

Secondly: why use this code if there are existing cross-platform examples available for those who take the trouble of searching a little, e.g.:
http://lazarus.freepascal.org/index.php/topic,17506.msg96648.html#msg96648
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

aminer

  • Hero Member
  • *****
  • Posts: 956
Re: Your ip adress and the WebServer...
« Reply #4 on: January 25, 2013, 09:27:55 pm »
Hello,


I have corrected the program and used ShellExecuteEx() with a Wait function, and i have modified a little bit the  VBScript :



And here is the Object pascal program:

===

program test;

uses windows,classes,sysutils,Winsock,ALWinInetFTPClient;


const SEE_MASK_NOCLOSEPROCESS = $00000040;

type

TShellExecuteInfo = record
    cbSize: DWORD;
    fMask: ULONG;
    Wnd: HWND;
    lpVerb: PAnsiChar;
    lpFile: PAnsiChar;
    lpParameters: PAnsiChar;
    lpDirectory: PAnsiChar;
    nShow: Integer;
    hInstApp: HINST;
    { Optional fields }
    lpIDList: Pointer;
    lpClass: PAnsiChar;
    hkeyClass: HKEY;
    dwHotKey: DWORD;
    hIcon: THandle;
    hProcess: THandle;
  end;

PShellExecuteInfo = ^TShellExecuteInfo;

var list:TStrings;
 i:integer;
 fstream:TFilestream;
 StrIP,str:string;
 handle:thandle;
ftp:TALWinInetFTPClient;

{...}

function ShellExecuteEx(lpExecInfo: PShellExecuteInfo):BOOL; stdcall;
external 'shell32.dll' name 'ShellExecuteExA';

function RunAndWait(sFile: string; sArgs:string; Handle: Cardinal; iVisibility:integer): DWord;

var

Info:TShellExecuteInfo;
pInfo:PShellExecuteInfo;
exitCode:dWord;

begin
{Pointer to Info}
pInfo:=@Info;

{Fill info}
With Info do
begin
cbSize:=SizeOf(Info);
fMask := SEE_MASK_NOCLOSEPROCESS;
wnd:=Handle;
lpVerb := 'Open';
lpFile:=PChar(sFile);
{Executable parameters}
lpParameters:=Pchar(sArgs+#0);
lpDirectory := nil;
nShow := iVisibility;
hInstApp := 0;
end;

{Execute}
ShellExecuteEx(pInfo);

{Wait to finish}
repeat
exitCode := WaitForSingleObject(Info.hProcess,500);
//Application.ProcessMessages;
until (exitCode <> WAIT_TIMEOUT);

Result := exitCode;
end;

function Stream2String(Stream1: TStream;var Mystring:string): boolean;
begin
result:=true;
try
 try
   Stream1.Position := 0;
   SetLength(Mystring, Stream1.Size);
   Stream1.ReadBuffer(Pointer(Mystring)^, Stream1.Size);
   
 finally
  // Stream1.Free;
 end;
except
result:=false;
end;

end;

function getIPs: Tstrings;
type
TaPInAddr = array[0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: array[0..63] of Char;
I: Integer;
GInitData: TWSAData;
begin
WSAStartup($101, GInitData);
Result := TstringList.Create;
Result.Clear;
GetHostName(Buffer, SizeOf(Buffer));
phe := GetHostByName(buffer);
if phe = nil then Exit;
pPtr := PaPInAddr(phe^.h_addr_list);
I := 0;
while pPtr^ <> nil do
begin
Result.Add(inet_ntoa(pptr^^));
Inc(I);
end;
WSACleanup;
end;

procedure FileReplaceString(const FileName, searchstring, replacestring: string);
var
  fs: TFileStream;
  S: string;
begin
  fs := TFileStream.Create(FileName, fmOpenread or fmShareDenyNone);
  try
    SetLength(S, fs.Size);
    fs.ReadBuffer(S[1], fs.Size);
  finally
    fs.Free;
  end;
  S  := StringReplace(S, SearchString, replaceString, [rfReplaceAll, rfIgnoreCase]);
  fs := TFileStream.Create(FileName, fmCreate);
  try
    fs.WriteBuffer(S[1], Length(S));
  finally
    fs.Free;
  end;
end;


begin

repeat

fstream:=TFileStream.create('ip.txt',fmOpenReadWrite);
Stream2String(fstream,StrIP);

list := GetIps;

If StrIP <> list[0]
 then
  begin
   FileReplaceString('Tree.html',StrIP,list[0]);
   FileReplaceString('Home.html',StrIP,list[0]);
   fstream.size:=0;
   fstream.WriteBuffer(list[0][1], Length(list[0]));

RunAndWait('cscript.exe','iis.vbs '+list[0]+':88:',Handle,SW_HIDE{SW_SHOWNORMAL});
//RunAndWait('net.exe','stop    w3svc',Handle,SHOWNORMAL);
//RunAndWait('net.exe','start    w3svc',Handle,SW_SHOWNORMAL);

ftp:=TALWinInetFTPClient.create(nil);

ftp.UserName:='write here your user name';
ftp.Password:='write here your password';
ftp.serverport:=21;

ftp.ServerName:='write here your ftp server name';
ftp.transfertype:=wFtpTt_BINARY;
ftp.connect;
ftp.PutFile('home.html','home.html');
//ftp.PutFile('tree.html','tree.html');

ftp.disconnect;

ftp.free;


end;

list.free;

fstream.free;

sleep(1000*60*60);
until false;

end.

===


And here is the VBScript file "iis.vbs" that is called by the Object pascal
program:


===

dim objWebApp
dim intArraySize
dim arrOldBindings
dim arrNewBindings
Dim IISOBJ

Set oArgs = WScript.Arguments

Set objWebApp = GetObject("IIS://localhost/w3svc/1")

If isArray(objWebApp.ServerBindings) then

arrOldBindings = objWebApp.ServerBindings
    intArraySize = UBound(arrOldBindings)
    Redim arrNewBindings(intArraySize + 1)

    For i = 0 to intArraySize
    arrNewBindings(i) = arrOldBindings(i)
    Next
WScript.Echo oArgs(0)
str=oArgs(0)

 arrNewBindings(intArraySize) = str
'"12.12.12.12:88:"
    objWebApp.Put "ServerBindings", (arrNewBindings)
    objWebApp.SetInfo

objWebApp.Stop

objWebApp.Start

objWebApp = nothing


End If


==





Thank you,
Amine Moulay Ramdane.




 

TinyPortal © 2005-2018