redirecting socket stream to file

C

ccc31807

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.
 
R

Rainer Weikusat

ccc31807 said:
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");
 
C

ccc31807

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.
 
D

Dave Saville

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?
 
R

Rainer Weikusat

ccc31807 said:
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?
 
J

J. Gleixner

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.
 
C

ccc31807

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.
 
C

ccc31807

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.
 
R

Rainer Weikusat

ccc31807 said:
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 |')?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top