We help IT Professionals succeed at work.
Get Started

Problem Making Socket Connection

jpetter
jpetter asked
on
730 Views
Last Modified: 2013-11-22
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
CEO/Programmer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE