Reading File from Server - perl

Hey,

I'm trying to read a file from a server. Simple file with some numbers.
Here is the code i'm running.

use Net::SSH::Perl::SSH1 ;

$scon = Net::SSH::Perl->new ("com123.sever.mydomain.com",(protocol=>'2',port=>'22',debug=>'true'));
$scon->login("user123","pass123");
open(FILE, "/tmp/test.o") ||
print("Unable to open test.o");
{
  $line = <FILE>;
  while ($line ne "") {
    print ($line);
    $line = <FILE>;
  print "Reading file...";
  }
print "Exiting...";
}

Doesn't seem to be working, have i left something out?

Guys,

I need some real help here. I'm stuck big time. This is what i have so far. I cannot figure out why its not running. It makes perfect sence to me, looks correct, but something is wrong.

sub calculate {
$scon = Net::SSH::Perl->new ("com123.user123.mydomain.com",(protocol=>'2',port=>'22'));
$scon->login("user123","pass123");
print "connected ...\n";

$data_file="test4.o";
open(DAT, "$data_file") || die("Could not open file!");
@raw_data=<DAT>;
foreach $num (@raw_data)
{
chomp($num);
($num1,$num2,$num3)=split(' ',$num);
print "Number 1 is $num1, Number2 is $num2, Number3 is $num3\n";
}
close(DAT);
} 

I am recieving this error alot of the time..

Could not open file! at hello.pl line 43.

Could not open file! at hello.pl line 43.

    while executing
"::perl::CODE(0x55eb4cc)"
    invoked from within
".c.calc invoke "
    invoked from within
".c.calc instate {pressed !disabled} { .c.calc state !pressed; .c.calc invoke } "
    (command bound to event)

Line 43 is :

open(DAT, "$data_file") || die("Could not open file!");

if i change it to this to it

open(DAT, "+>" . $data_file) || die("Could not open file!");

Then it only creates file in my c: drive.

I'm calling this code from a tcl button which is set up as follows:

Tkx::ttk__button(".c.calc", -text => "Connect to server", -command => sub {calculate();});

I've used a mkdir command after the initall connection, but it creates the directory on my c: drive and not the server, this leads me to think its not connecting right.

Can someone please give me some direction to go with this!!!

Thanks,
Philip.

Sorry I have no experience with what you are doing.

What are you trying to do? Open a connection to a server and open a file there for reading? If so, you should know that Net::SSH::Perl is just the secure equivalent of Net::Telnet. Meaning, it opens a connection to the remote host, sends commands and receives answers. It cannot magically make available files that are non-local.

As for your error messages: you can improve the helpfulness of those by including

use strict;
use warnings;
use diagnostics;

and using $! in your error messages.

No problem kevin, I just hope someone else can help me out, you seem to be the only person on this forum.

Yes, i want to connect to the server through ssh and read the file.

im using this

use Net::SSH::Perl::SSH1 ;

I'm not using telnet.

Read my post again. Then read the documentation for Net::SSH::Perl (hopefully) again. Then meditate on the information. Then please show me where in the documentation for Net::SSH::Perl it says that you can use the module to open remote files, because I must have missed that bit when reading it.

Pludi Thank you!!! I love your scarcasm. I thought i could just open once a connect had been made to the server. I'm going to look at File::Remote module and see where that gets me. Thanks again. Hope i can get it going.

Just a quick question here:
If i had the following:

use File::Remote;
my $remote = new File::Remote;
# Create a new file and change its permissions
$remote->mkdir("host:/remote/dir");
$remote->touch("host:/remote/dir/file");

what is host?
would i write the line like this

$remote->mkdir("com123.user123.mydomain.com:/home/user/dirTocreate");

Thanks again.

UPDATE 2:

Here is my connection details

use File::Remote;
use Net::SSH::Perl;

$scon = Net::SSH::Perl->new ("server12.comp123.domain123.com",(protocol=>'2',port=>'22',debug=>'true'));
$scon->login("user123","pass123");
print "connected ...\n";


my $remote = new File::Remote;

$remote->touch("server12:/home/user123/file.o") or die ":: $!\n";;

When i execute this code, i'm getting a broken pipe error.

:: Broken pipe

:: Broken pipe

    while executing
"::perl::CODE(0x577fc1c)"
    invoked from within
".c.calc invoke "
    invoked from within
".c.calc instate {pressed !disabled} { .c.calc state !pressed; .c.calc invoke } "
    (command bound to event)

Anyone know how to fix this?

I think its to do with the path to the server, but it seems right to me.

Thanks.

From the documentation of File::Remote:

# Object-oriented method
   use File::Remote;
   my $remote = new File::Remote;
   my $secure = new File::Remote (rsh => "/bin/ssh", rcp => "/bin/scp");

So you'll probably need the second method of using it, plus public key authentication, as this module doesn't allow passing a username and password.

Other than that, to open a remote file use

$secure->open("server12.comp123.domain123.com:/home/user123/file.o) or die "Can't open: $!";

It doesn't look like you can combine it with Net::SSH::Perl or something similar, except maybe if you modify the source yourself.