Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > redirecting socket stream to file

Reply
Thread Tools

redirecting socket stream to file

 
 
ccc31807
Guest
Posts: n/a
 
      08-18-2011
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.
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-18-2011
ccc31807 <(E-Mail Removed)> 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
....
 
Reply With Quote
 
 
 
 
ccc31807
Guest
Posts: n/a
 
      08-18-2011
On Aug 18, 12:04*pm, Rainer Weikusat <(E-Mail Removed)> 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.
 
Reply With Quote
 
Dave Saville
Guest
Posts: n/a
 
      08-18-2011
On Thu, 18 Aug 2011 16:29:12 UTC, ccc31807 <(E-Mail Removed)> wrote:

> On Aug 18, 12:04’pm, Rainer Weikusat <(E-Mail Removed)> 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
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-18-2011
ccc31807 <(E-Mail Removed)> writes:
> On Aug 18, 12:04*pm, Rainer Weikusat <(E-Mail Removed)> 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?
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      08-18-2011
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.

 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      08-18-2011
On Aug 18, 1:03*pm, Rainer Weikusat <(E-Mail Removed)> 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.
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      08-18-2011
On Aug 18, 2:42*pm, Tad McClellan <(E-Mail Removed)> 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.
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-18-2011
ccc31807 <(E-Mail Removed)> writes:
> On Aug 18, 1:03*pm, Rainer Weikusat <(E-Mail Removed)> 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 |')?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments