redirecting socket stream to file

Discussion in 'Perl Misc' started by ccc31807, Aug 18, 2011.

  1. ccc31807

    ccc31807 Guest

    I'm not a network programmer, and don't even know if this is possible.

    I connect to a remote machine using telnet, and run some commands,
    which result in screen output. I would like to redirect this output to
    a file rather than to the console. I need to work within the following
    parameters:

    1. I can't use FTP, but only telnet (I know how weird this must
    sound).
    2. I use a VPN tunnel, so I have invoke a VPN client and to use expect
    to check for user and password prompts.
    3. The remote machine requires authentication, so I have to use expect
    to check for user and password prompts.
    4. I can easily automate the commands I use to generate the output,
    but all this goes to standard output (on my machine).
    5. Several years ago, I wrote something like this using multiple
    threads (a VPN thread to connect to the server, a query thread to run
    the commands, and an FTP thread to download the data), but now FTP has
    been disables and all I have is telnet.

    Ideally, I would like to capture the output of the commands in a text
    file so that I can process the file and generate some reports.

    Can I redirect output from a stream socket to a text file dynamically
    during the runtime of a script? if so, any clues as to how to do that?

    Thanks, CC.
     
    ccc31807, Aug 18, 2011
    #1
    1. Advertising

  2. ccc31807 <> writes:
    > I connect to a remote machine using telnet, and run some commands,
    > which result in screen output. I would like to redirect this output to
    > a file rather than to the console. I need to work within the following
    > parameters:
    >
    > 1. I can't use FTP, but only telnet (I know how weird this must
    > sound).
    > 2. I use a VPN tunnel, so I have invoke a VPN client and to use expect
    > to check for user and password prompts.
    > 3. The remote machine requires authentication, so I have to use expect
    > to check for user and password prompts.
    > 4. I can easily automate the commands I use to generate the output,
    > but all this goes to standard output (on my machine).


    [...]

    > Ideally, I would like to capture the output of the commands in a text
    > file so that I can process the file and generate some reports.
    >
    > Can I redirect output from a stream socket to a text file dynamically
    > during the runtime of a script?


    I assume the script runs remotely. Can't you just use select ('perldoc
    -f select') in order to change the default output file handle?
    Provided your OS supports this, it is also possible to redirect output
    temporarily at the file descriptor level, example

    -----------
    use feature 'say';

    my ($fh, $old);

    say("To stdout");

    open($fh, '>', '/tmp/out');
    open($old, '>&', STDOUT);
    open(STDOUT, '>&', $fh);

    say("To file");

    open(STDOUT, '>&', $old);

    say("To screen");
    -----------

    However, I have to admit that I don't quite understand your problem
    ....
     
    Rainer Weikusat, Aug 18, 2011
    #2
    1. Advertising

  3. ccc31807

    ccc31807 Guest

    On Aug 18, 12:04 pm, Rainer Weikusat <> wrote:
    > However, I have to admit that I don't quite understand your problem


    Here is how I would do it manually, on my Windows machine (we use
    Windows at work)
    1. Open a console and give the VPN client command, which accepts the
    profile, user, and password as parameters
    2. Accept the terms of use by entering 'y' at the prompt
    3. telnet to the remote server
    4. Enter username and password at the prompts.
    5. Accept the terms of user by entering 'y' at the prompt.
    6. Do my work, i.e., give commands and observe the output.
    7. Log off the remote machine.
    8. Disconnect the VPN client.

    I can script all the commands and it works fine. I can then run the
    script as a scheduled task (Windows, remember). The problem is that I
    lose the output.

    If I change the output stream to a file, I can't 'see' the prompts. I
    need to be able to read the prompts from the script in order to
    respond to them, but then AFTER I AM CONNECTED AND HAVE GIVEN THE
    COMMAND dynamically change the output stream to redirect the output to
    a file.

    I am running Perl on the local machine to send commands to the remote
    machine, as in system(command) or exec(command)

    Thanks, CC.
     
    ccc31807, Aug 18, 2011
    #3
  4. ccc31807

    Dave Saville Guest

    On Thu, 18 Aug 2011 16:29:12 UTC, ccc31807 <> wrote:

    > On Aug 18, 12:04ÿpm, Rainer Weikusat <> wrote:
    > > However, I have to admit that I don't quite understand your problem

    >
    > Here is how I would do it manually, on my Windows machine (we use
    > Windows at work)
    > 1. Open a console and give the VPN client command, which accepts the
    > profile, user, and password as parameters
    > 2. Accept the terms of use by entering 'y' at the prompt
    > 3. telnet to the remote server
    > 4. Enter username and password at the prompts.
    > 5. Accept the terms of user by entering 'y' at the prompt.
    > 6. Do my work, i.e., give commands and observe the output.
    > 7. Log off the remote machine.
    > 8. Disconnect the VPN client.
    >
    > I can script all the commands and it works fine. I can then run the
    > script as a scheduled task (Windows, remember). The problem is that I
    > lose the output.
    >
    > If I change the output stream to a file, I can't 'see' the prompts. I
    > need to be able to read the prompts from the script in order to
    > respond to them, but then AFTER I AM CONNECTED AND HAVE GIVEN THE
    > COMMAND dynamically change the output stream to redirect the output to
    > a file.
    >


    Can you not close stdout *before* giving the command and assign it to
    a file? Or do you need to see more prompts/command o/p before issuing
    the next command?

    --
    Regards
    Dave Saville
     
    Dave Saville, Aug 18, 2011
    #4
  5. ccc31807 <> writes:
    > On Aug 18, 12:04 pm, Rainer Weikusat <> wrote:
    >> However, I have to admit that I don't quite understand your problem

    >
    > Here is how I would do it manually, on my Windows machine (we use
    > Windows at work)
    > 1. Open a console and give the VPN client command, which accepts the
    > profile, user, and password as parameters
    > 2. Accept the terms of use by entering 'y' at the prompt
    > 3. telnet to the remote server
    > 4. Enter username and password at the prompts.
    > 5. Accept the terms of user by entering 'y' at the prompt.
    > 6. Do my work, i.e., give commands and observe the output.
    > 7. Log off the remote machine.
    > 8. Disconnect the VPN client.
    >
    > I can script all the commands and it works fine. I can then run the
    > script as a scheduled task (Windows, remember). The problem is that I
    > lose the output.
    >
    > If I change the output stream to a file, I can't 'see' the prompts. I
    > need to be able to read the prompts from the script in order to
    > respond to them, but then AFTER I AM CONNECTED AND HAVE GIVEN THE
    > COMMAND dynamically change the output stream to redirect the output to
    > a file.


    But if your script already analyzes the output from remote in order to
    detect the prompts and send appropriate input in response to them,
    what stops it from starting to write everything sent from remote to a
    file after the 'interesting command' has been started?
     
    Rainer Weikusat, Aug 18, 2011
    #5
  6. ccc31807

    J. Gleixner Guest

    On 08/18/11 16:29, ccc31807 wrote:

    [..]

    >
    > If I change the output stream to a file, I can't 'see' the prompts. I
    > need to be able to read the prompts from the script in order to
    > respond to them, but then AFTER I AM CONNECTED AND HAVE GIVEN THE
    > COMMAND dynamically change the output stream to redirect the output to
    > a file.
    >
    > I am running Perl on the local machine to send commands to the remote
    > machine, as in system(command) or exec(command)


    Expect has many ways to capture output. See before(), after(),
    log_file(), etc. You can turn logging on/off, or capture data, modify
    it, then write it yourself, if needed. Read through the Expect
    documentation again, to see what's available.
     
    J. Gleixner, Aug 18, 2011
    #6
  7. ccc31807

    ccc31807 Guest

    On Aug 18, 1:03 pm, Rainer Weikusat <> wrote:
    > But if your script already analyzes the output from remote in order to
    > detect the prompts and send appropriate input in response to them,
    > what stops it from starting to write everything sent from remote to a
    > file after the 'interesting command' has been started?


    The short answer is, I don't know how to do this.

    When I telnet to the server, I'm not on an OS prompt (the server runs
    on AIX). I'm logged into a particular application that expects a human
    user to enter command at the prompt.

    Before the sys admins shut down ftp, I would save the output of the
    commands to a file, log off the server, reconnect by ftp, and download
    the file. Now that I can't use ftp, all I can do is watch the screen
    and see the output. This isn't a problem if I am sitting at the
    keyboard and entering the commands, but I would like to be able to do
    this task asynchronously.

    Once I hit the application's prompt, I'm locked out of everything
    else, and in fact have to signal an interrupt to 'log off.' This works
    fine for people interacting with the system in real time, but I've
    gotten tired of making the same keystrokes day after day, and tired of
    making typing mistakes and having to recover, so I want to automate
    the task and be done with it ...

    .... but I don't know how.

    CC.
     
    ccc31807, Aug 18, 2011
    #7
  8. ccc31807

    ccc31807 Guest

    On Aug 18, 2:42 pm, Tad McClellan <> wrote:
    > If I am understanding you correctly, then you should be able
    > to use "screen" to log everything that appears in your (local) console.


    Yes, that appears to be exactly what I am thinking. I scanned the GNU
    manual, but unfortunately when I searched for 'Windows screen', guess
    what the search returned?

    Maybe I should abandon Perl and use Tcl instead.

    CC.
     
    ccc31807, Aug 18, 2011
    #8
  9. ccc31807 <> writes:
    > On Aug 18, 1:03 pm, Rainer Weikusat <> wrote:
    >> But if your script already analyzes the output from remote in order to
    >> detect the prompts and send appropriate input in response to them,
    >> what stops it from starting to write everything sent from remote to a
    >> file after the 'interesting command' has been started?

    >
    > The short answer is, I don't know how to do this.
    >
    > When I telnet to the server, I'm not on an OS prompt (the server runs
    > on AIX). I'm logged into a particular application that expects a human
    > user to enter command at the prompt.


    I assume that you're running expect via system because otherwise, the
    data from remote must already pass through your script. I don't know
    if this works on Windows, but could you perhaps run the command(s) via
    open($fh, 'comand |')?
     
    Rainer Weikusat, Aug 18, 2011
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    5,114
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    1,032
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    600
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    716
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    760
Loading...

Share This Page