We help IT Professionals succeed at work.

Problem Making Socket Connection

jpetter
jpetter asked
on
Hi,
I'm brand new to TCL, and am trying to learn it so I can write some expect scripts that would help us automate some of our workload.

I'm trying to take it nice and slow, and move ahead once I get one piece of code working. But I can't even get this first piece working, so I must be way off on the wrong track.

What I'm trying to do here is to help automate a piece of our vulnerability management work. With this small snippet, I just wanted to pass the script an ip address, and a port, and have the program check to see if the port is open or not. Once I get this piece working, I plan to work on the Expect piece which will logon to one of my Linux systems, and run rpcinfo -p with the arguments passed, which would be the IP and port if they are found to be open.

I would greatly appreciate it if someone could get me started on this. I have pasted the code I've been playing around with far too long.

BTW, the error message I receive is:
"$ ./get-rpc-info 105.37.49.115 80
couldn't open socket: connection refused
    while executing
"socket $address $port"
    invoked from within
"set sock [socket $address $port]"
    (file "./get-rpc-info" line 11)"

Thanks very much,
Jeff
#!/usr/local/bin/tclsh

if { $argc != 2 } {
    puts "Usage: $argv0 <ip address> <port>"
    exit 1;
} 
set address [lindex $argv 0]
set port [lindex $argv 1]
#puts "Command line is: $argv0 $address:$port"
	
set sock [socket $address $port]
set result "$address:$port"
if [ns_sockcheck] {
    puts "$result -- open"
} else {
    puts "$result -- closed"
}		
	close $sock
}

Open in new window

Comment
Watch Question

fridomCEO/Programmer

Commented:
The code is ok, there is however one bug after close $sock is a closing } which has not opening counterpart
I also do not have nd_sockcheck, but If I access an open port with your script I got:
 ./open_socket.tcl 127.0.0.1 80
127.0.0.1:80

If I try to access a port which is firwall protected:
 ./open_socket.tcl 127.0.0.1 30
couldn't open socket: connection refused
    while executing
"socket $address $port"
    invoked from within
"set sock [socket $address $port]"

So you can read it as "socket" is not oopen. So you just have to catch the exception which may occur while accessing a
"ip-saved" socket.

Regards
Friedrich

Author

Commented:
Hi Friedich,
I appreciate your help, and response.

I also probably don't have the ns_sockcheck. It was listed in a "TCL API Reference" (http://www.zill.net/asdocs/tcl-api/index.html) I found on line, and based on my experience of API's, they're usually already built into the language and expose different functions to aid the programmer - such as with Microsoft's API for instance. This then must be some type of library I have to link in.

All I was tring to do with that is determine if I had a connection or not. Something like what I could do in C or Perl with: if ( $socket ), but I tried that first and it didn't like it. But initially, the first thing I need to do is make a connection, and be able to get a status as to whether that was successful or not.

Thanks for spotting that misplaced bracket. I had deleted another block of code to strip it down to the minimum, and probably left that lingering.

Now I get the following error when I try to run it:
invalid command name "ns_sockcheck"
    while executing
"ns_sockcheck"
    invoked from within
"if [ns_sockcheck] {
    puts "$result -- open"
} else {
    puts "$result -- closed"
}               "

Looks I need to find a way to confirm a connection, and figure out why I can connect using Perl, but not Tcl.

Thanks for your help.
CEO/Programmer
Commented:
Well I know ns_sockchck is part of the AOL Server. See. e.g
http://aolserver.am.net/docs/2.3/tapi-124.htm#82277

Howerver the code which probably willl do is:



#!/usr/bin/tclsh

if { $argc != 2 } {
    puts "Usage: $argv0 <ip address> <port>"
    exit 1;
} 
set address [lindex $argv 0]
set port [lindex $argv 1]
#puts "Command line is: $argv0 $address:$port"
	
if { [ catch { set sock [socket $address $port] } result] }  {
    puts "Could not connect result = $result"

} else {
    puts "connecting successfull result = $result";
    close $sock
}

Open in new window

Author

Commented:
fridom,

Thank you very much! That works perfectly well for what I need.

It makes sense now that the API would belong to Netscape/AOL with the "ns_" prefix.

Again, thanks very much,
Jeff

Author

Commented:
Expert did a great job sticking with me as he worked me toward the solution I needed, Great job fridom!