AstaaLavista
(Starting to like KiXtart)
2005-08-11 01:37 PM
Reading/Writing from /to Command prompt

Hi,
Please excuse me. I am new to Kixtart.
Using Kixtart, is it possible to write automated scripts which can READ from & WRITE to “Command Prompt” windows.

Basically, my issue is that I want to check accessibility to several IPs. These IPs have several ports open. Using Telnet, I want to check if the ports are open or not.
Thus, on the command prompt window, if I see the statement 'Could not open connection to the host.. Connect failed' , it means the port is NOT opened.
Thus, if I am able to read from the command prompt windows, the above thing wud be achieved.
If anybody knows better method of checking the accessibility, please let me know.
Thanks in advance.


ShawnAdministrator
(KiX Supporter)
2005-08-11 02:04 PM
Re: Reading/Writing from /to Command prompt

Assuming your doing some sort of SHELL statement that runs a program that produces this output. Suggest you try querying the value of @ERROR to see if a meaningfull number if returned, that can be checked with an IF statement.

Try putting this after the SHELL statement and advise:

? @ERROR

-Shawn


AstaaLavista
(Starting to like KiXtart)
2005-08-11 02:32 PM
Re: Reading/Writing from /to Command prompt

Hi Shawn,
I am sorry, but I didn't get ur suggestion.

Basically, I want to know if its possible to READ from any COMMAND PROMPT window, using KIXTART. If yes, then how ?


Radimus
(KiX Supporter)
2005-08-11 02:50 PM
Re: Reading/Writing from /to Command prompt

look into WSHPipe() from the udf forum.

It will capture the output from a cndline app, and dump the output into an array to make it easier to parse.

Otherwise, you will have to redirect output into a text file and then read the file into an array, etc...


ShawnAdministrator
(KiX Supporter)
2005-08-11 03:05 PM
Re: Reading/Writing from /to Command prompt

I guess what I'm saying is that you probably dont have to read the command line ... whatever program is displaying that (what looks like) an error message, is probably also setting the value of @ERROR, and you may be able to check that instead (basically make it easier on yourself).

Les
(KiX Master)
2005-08-11 03:54 PM
Re: Reading/Writing from /to Command prompt

If you want to chaeck ports, there is a better way than to telnet and scrape the screen. I use a utility called portchek.exe to test for open ports.

AstaaLavista
(Starting to like KiXtart)
2005-08-12 11:05 AM
Re: Reading/Writing from /to Command prompt

Checking the value of @error does not help.

I tried $rc=WshPipe("telnet SOME I.P. 80")

For both the open, closed ports, it displayed -->
0 | The operation completed successfully.

I also checked all the commands of telnet. Its not possible to redirect its output.

I just want to write a script that will automate the telnet process. i.e. i will take the list of I.P. from a file and using the script telnet to all these I.P. If the port is NOT open then I will get an error 'Could not open connection to the host'. This error will be recorded using kixtart (somehow, but unfortunately I dont know & thats what I am asking)
Is it possible using Kixtart. Please help.


Witto
(MM club member)
2005-08-12 12:34 PM
Re: Reading/Writing from /to Command prompt

If I try Shawn's advise
Code:

Break On
Shell "PING "+@WKSTA
? @ERROR": "@SERROR
Shell "PING 192.168.222.3"
? @ERROR": "@SERROR


I know that 192.168.222.3 is non-existant
The @ERROR is 1 and @SERROR tells me the function is wrong.


LonkeroAdministrator
(KiX Master Guru)
2005-08-12 01:02 PM
Re: Reading/Writing from /to Command prompt

there is telnet object in howard bullocks hoby-objects library dll.

anyone care to pass the link?


Mart
(KiX Supporter)
2005-08-12 02:05 PM
Re: Reading/Writing from /to Command prompt

Sure.

http://home.comcast.net/~habullock/habobjects.htm


Radimus
(KiX Supporter)
2005-08-12 02:35 PM
Re: Reading/Writing from /to Command prompt

Code:

Shell 'PortQry.exe -n '+$ipaddr+' -e 135 -nr -q'
$PC = iif(@ERROR,'Not Windows','Windows')
? $pc



ShawnAdministrator
(KiX Supporter)
2005-08-12 02:37 PM
Re: Reading/Writing from /to Command prompt

Expanding on Witto's comments, and assuming your looking to test just accessibility versus whether the telnet ports are specificically open or not, this code works. Just change the $WKSTA variable to test good and bad hostnames.

Code:

break on

$wksta = @WKSTA

shell '%comspec% /c ping -n 2 $wksta | find /i "TTL=" >NUL 2>NUL'

if @ERROR = 0
?"$wksta is accessible!"
else
?"$wksta is not accessible!"
endif

exit 0



Or - are you looking to test specifically whether telnet is available ?

-Shawn


Les
(KiX Master)
2005-08-12 02:49 PM
Re: Reading/Writing from /to Command prompt

It is a common practice to use telnet to test other than telnet ports and I suspect that is what this is about. While telnet is fine from the commandline, there are better ways as suggested by my and Rad's posts.

Richard H.Administrator
(KiX Supporter)
2005-08-12 03:34 PM
Re: Reading/Writing from /to Command prompt

IIRC UDP ports will not accept a TCP connection (three way connection negotiation fails) so telnet is of limited use.

Portchek or a port scanning tool like nmap (www.insecure.org) are much better prospects.


Les
(KiX Master)
2005-08-12 03:45 PM
Re: Reading/Writing from /to Command prompt

Code:
Break on
;Uses NetView() UDF from http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=12;t=000059
Call "C:\KiXScripts\netview.udf"
$netview = netview("@LDOMAIN")
if @error = 0
for each $computer in $netview
$ShellCMD="C:\KiXScripts\portchek -h "+$computer+" 36794"
Shell $ShellCMD
If @error
"No listener on port 36794 of "+$computer ?
Else
"YIKES! LISENER FOUND ON PORT 36794 OF "+$computer ?
Beep
EndIf
next
endif


"That all folks!"?
Get $_



AllenAdministrator
(KiX Supporter)
2005-08-12 04:03 PM
Re: Reading/Writing from /to Command prompt

Me and Doc worked on a script a while back that "kind of" does what you want... its definately not the most efficient way to do it, but at the time, I think it was all that was needed.

MX Record lookup help

I modified the MailServerUp function (discussed in the above thread) into the code below. Requires EndProcess() UDF.

Code:
 
Function TNServerUp($server,$port)
Dim $output,$,$fh,$line,$path
$output='%temp%\output.txt'
If Exist($output)
Del $output
EndIf
Run '%comspec% /c telnet -f ' + $output + ' ' + $server + ' ' + $port
Sleep 2
$=EndProc('telnet.exe')
Sleep 1
$fh=FreeFileHandle()
If Open($fh,$output)=0
$line=ReadLine($fh)
While @ERROR=0
If Left($line,3)=220
$TNServerUp=1
EndIf
$line=ReadLine($fh)
Loop
$=Close($fh)
EndIf
If Exist($output)
Del $output
EndIf
EndFunction




AstaaLavista
(Starting to like KiXtart)
2005-08-22 11:52 AM
Re: Reading/Writing from /to Command prompt

Hi All,
Sorry for the late reply. I had been out of station.
Quote:


Or - are you looking to test specifically whether telnet is available ?

-Shawn




This was what I was looking for.
I will try the code sent by Allen. I hope my problem will be solved.
Thanks a lot


AstaaLavista
(Starting to like KiXtart)
2005-08-22 03:17 PM
Re: Reading/Writing from /to Command prompt

I tried the script by entering the I.P. address & the port. But, for both valid & invalid ports it gives the same message "System did not respond".
I copied the entire code of "MX Record lookup help " & also of Allen, but was unlucky to get any end result. i dont think i missed anything here.

I wud like to repeat my problem once again. i have a I.P. address. I want to design a script given I.P & port, i do the telnet. If i get the error message "Could not open connection to the host", it means the port is NOT opened.
I will record the result from the window.

I think i m behaving like a dumb, but sadly, i m not understanding what to do.
Plz. help.


AllenAdministrator
(KiX Supporter)
2005-08-22 04:03 PM
Re: Reading/Writing from /to Command prompt

The UDF above "determines" a connection by getting a return code by making a connection to a SMTP server via telnet. I admit, I have not tested this on any other type of telnet server. It may be possible to modify the udf above to meet your needs but you might also consider the other suggestions in this thread.

If you want to use the UDF I wrote, you will need to run the telnet command in a command window with the logging feature enabled (telnet -f telnet.log xxx.xxx.xxx.xxx p). Then look at the log to see if it gives you a consistent connection line. Then modify the UDF above to look for that string, changing this line: If Left($line,3)=220.

You might also post your results and we might be able to help.


AstaaLavista
(Starting to like KiXtart)
2005-08-23 06:27 AM
Re: Reading/Writing from /to Command prompt

I tried the following on the command prompt:
telnet -f abc.txt port.

Surprisingly, abc.txt was modified (seeing from the timestamp), but nothing was written into it.

Should I enable logging from somewhere. I tried 'set logging on' but I got an error, 'Environment variable logging not defined'.


AllenAdministrator
(KiX Supporter)
2005-08-23 08:00 AM
Re: Reading/Writing from /to Command prompt

Quote:

I tried the following on the command prompt:
telnet -f abc.txt port.





Not sure if you made a typo there... but should be
Code:
 telnet -f abc.txt ipaddress port 



AstaaLavista
(Starting to like KiXtart)
2005-08-23 08:25 AM
Re: Reading/Writing from /to Command prompt

Quote:



Code:

break on

$wksta = @WKSTA

shell '%comspec% /c ping -n 2 $wksta | find /i "TTL=" >NUL 2>NUL'

if @ERROR = 0
?"$wksta is accessible!"
else
?"$wksta is not accessible!"
endif

exit 0







I tried this too. It works fine, BUT unfortunately, ping does not work for checking foreign I.P. what i mean is, I am not able to ping to any I.P. outside my network due to the FIREWALL. so the only option which is have is telnet. I replaced the ping statement with TELNET but it didnt work.

Actually, i dont want to use any thing other than Kixtart, not even the HABobjects.DLL. Is it possible to do? I am trying & will let you all know.


AstaaLavista
(Starting to like KiXtart)
2005-08-23 08:30 AM
Re: Reading/Writing from /to Command prompt

Quote:

Quote:

I tried the following on the command prompt:
telnet -f abc.txt port.





Not sure if you made a typo there... but should be
Code:
 telnet -f abc.txt ipaddress port 







Yeah, Sorry it was a typo. But it doesn't work. The file is modified, but nothing is written into it.


AstaaLavista
(Starting to like KiXtart)
2005-08-23 09:08 AM
Re: Reading/Writing from /to Command prompt

I tried NETVIEW.UDF but unfortunately it also wont be of much help. Basically, i want to maintain a list of all accessible ports which are outside my network.
so telnet is the only option. which somehow is NOT working well with Kix.


LonkeroAdministrator
(KiX Master Guru)
2005-08-23 09:19 AM
Re: Reading/Writing from /to Command prompt

sure it is.
how else I would have written a massmailer with it.

anyway, can you do the telnet stuff manually without kix?
that is, see what is going wrong.


AstaaLavista
(Starting to like KiXtart)
2005-08-23 09:28 AM
Re: Reading/Writing from /to Command prompt

I tried telnet -f abc.txt 192.168.33.22. Log file was created. but nothing was written into it.
Do i have to enable LOGGING from anywhere?


LonkeroAdministrator
(KiX Master Guru)
2005-08-23 09:30 AM
Re: Reading/Writing from /to Command prompt

are you using WinME or win9x?

AstaaLavista
(Starting to like KiXtart)
2005-08-24 08:03 AM
Re: Reading/Writing from /to Command prompt

Neither WinME nor Win9X, but Win 2K & Win XP.

AstaaLavista
(Starting to like KiXtart)
2005-08-25 07:30 AM
Re: Reading/Writing from /to Command prompt

Hi All,

I have got some breakthrough. I was able to set the logging option.
I didnt know that it is done by typing ONLY telnet on the command prompt.
The log file was successfully created.

Thus, I will check whether the log file is empty or not, which in turn will imply that port is opened or not.
BUT...,this solves the problem for only port 23(telnet port); apart from PORT no. 23, for all other ports, nothing is written into the log file.

So, I will have to find a way of differentiating the OTHER ports. I am looking into it.


Les
(KiX Master)
2005-08-25 04:42 PM
Re: Reading/Writing from /to Command prompt

So why can't you simply use a portchek or portqry utility as Rad and I suggested?

AstaaLavista
(Starting to like KiXtart)
2005-08-26 10:09 AM
Re: Reading/Writing from /to Command prompt

Quote:

So why can't you simply use a portchek or portqry utility as Rad and I suggested?




Please excuse me, but there are some reasons why I cant use any utility:

Since i work for a company, using any utility will create license issues.
Also, if the utility does everything, then what is my contribution.


Les
(KiX Master)
2005-08-26 05:26 PM
Re: Reading/Writing from /to Command prompt

Well... that's a lame arguement.
Rad and I work for companies and have no issue with license.
Telnet is a utility so what is your contribution there? I'm pretty sure Ruud will not be hacking KiX anytime soon to add screen scraping of MS telnet.

I do script telnet but not the not-so-well-behaved MS telnet and not entirely with KiX. I use the HyperTerm telnet and the 'AutoItX3.Control' COM object.