in reply to a dumb socket question

Are you stuck on reading the results of the ssh call, or sending them to the client? You didn't say. Also, why does the server script call the run_script using ssh, if they are on the same machine? Surely you could skip that part and just call the run_script directly? (Using backticks or system, backticks will get you anything the script prints to STDOUT, eg: my $result = `run_script`;.

To return it to the client, you just need to print that information to the socket.

C.

Replies are listed 'Best First'.
Re^2: a dumb socket question
by rhxk (Beadle) on Jul 15, 2004 at 17:09 UTC
    I'm stuck on sending the result back to the client.
    I think if I read the perlipc's doc as suggested by beable, it'll help
    but in any case here's the code:
    server.script: use Socket; ($Program = $0) =~ s@^.*/@@; $file = "/tmp/socket.$Program"; $addr = sockaddr_un($file); socket(SERVER, PF_UNIX, SOCK_STREAM, 0) || die "Could not create socke +t: $!"; unlink("$file"); bind(SERVER, $addr) || die "Could not bind: $!"; listen(SERVER,SOMAXCONN) || die "Could not listen: $!"; while (accept(CLIENT,SERVER)) { $cmd = <CLIENT>; if ($cmd) { $time = &timeStamp; open(CMD, "ssh2 rob\@machine_b $cmd |"); @results = <CMD>; close(CMD); print @results; } } client.script: use Socket; ($Program = $0) =~ s@^.*/@@; $file = "/tmp/server.script"; &Message("/home/me/run_script -r -s a1 -avg"); sub Message { my ($cmd) = @_; my $level = 1; if (socket(SOCKET, PF_UNIX, SOCK_STREAM, 0)) { connect(SOCKET, sockaddr_un($file)) || die "Could not connect: + $!"; print SOCKET $cmd; close SOCKET; } else { print "I couldn't connect"; } }
    OK, the client.script sends the query to server.script,
    and server.script runs the cmd & prints it. how do i get it
    to return the results to the client.script so the client.script
    can print the result? (I think i should go and read the perlipc's doc)
    thanx...
      How about writing the result to the CLIENT socket? Sockets are bidirectional, and the client script is already connected to the server. I don't see any synchronization issues.
      # server @results = <CMD>; print CLIENT @results; # client print SOCKET $cmd; @results = <SOCKET>;

      Why do you have a separate server and client? Since they are running on the same machine, wouldn't it be easier to just have the client script do the ssh command? The only reason I can think of is that the server script is running as a different user with ssh privledges.

        exactly. I would be running the server script which has the
        ssh key shared to the other machine(no passwd is prompted)
        I've implemented your suggestion in, but the client is not
        getting anything back. It is waiting on the
        @results = <SOCKET> indefinitely
        here's the modified client:
        if (socket(SOCKET, PF_UNIX, SOCK_STREAM, 0)) { connect(SOCKET, sockaddr_un($file)) || die "Could not connect: + $!"; print SOCKET $cmd; @results = <SOCKET>; print @results;
        here's the modified server:
        socket(SERVER, PF_UNIX, SOCK_STREAM, 0) || die "Could not create socke +t: $!"; unlink("$file"); bind(SERVER, $addr) || die "Could not bind: $!"; listen(SERVER,SOMAXCONN) || die "Could not listen: $!"; while (accept(CLIENT,SERVER) || die "could not accept: $!") { $cmd = <CLIENT>; if ($cmd) { $cmdd = "ssh2 rob\@machine_b $cmd"; + open(CMD, "$cmdd |"); @results = <CMD>; close(CMD); + print CLIENT @results; } }
        somehow the print CLIENT @results is not
        actually returning the results....guess i have to do more tests