Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SFTP error on call to open /dev/urandom

Status
Not open for further replies.

ajdavis

Programmer
Jun 17, 2005
14
US
I'm changing a script from using FTP to SFTP. I have this code:
Code:
	print "Trying to connect to $host.\n";

	if ( $sftp = Net::SFTP->new( "$host" ) )
	{
		print "Connected to $host.\n";
		$connected = "true";
	}
	else
	{
		$rc = 1;
		push @ERRORS, "Can't connect to $host: $!\n";
	}
and I get the following error.
Trying to connect to host_name. (output from my code)
Couldn't open /dev/urandom at /usr/local/lib/perl5/site_perl/5.8.8/Crypt/DH.pm line 96.
The code in DH.pm is:
Code:
sub _makerandom {
    my $size = shift;

    my $bytes = int($size / 8) + ($size % 8 ? 1 : 0);

    my $rand;
    if (-e "/dev/urandom") {
        my $fh;
[b][COLOR=red]        open($fh, '/dev/urandom')
            or die "Couldn't open /dev/urandom"; [/color][/b]
        my $got = sysread $fh, $rand, $bytes;
        die "Didn't read all bytes from urandom" unless $got == $bytes;
        close $fh;
    } else {
        for (1..$bytes) {
            $rand .= chr(int(rand(256)));
        }
    }

    my $bits = unpack("b*", $rand);
    die unless length($bits) >= $size;

    Math::BigInt->new('0b' . substr($bits, 0, $size));
}
but as you can see, the code was able to see the existence of /dev/urandom before trying to open it.
Here are the permissions for /dev/urandom
=> ls -l /dev/urandom
crw-r--r-- 1 root system 40, 1 May 21 2007 /dev/urandom
So I'm not sure why it can't read from it. I've been looking on-line and haven't found anything that would help. Any suggestions as to how to fix this or get around it?

Thanks in advance.
Josh

 
I decided to go the Net::SFTP::Foreign route but I'm getting a different error with it on the ->put() call. I'll search the posts for this error and repost if I can't find anything.

Code:
#!/usr/bin/perl -X

use Net::SFTP::Foreign;

my $host = "";
my $username = "";
my $password = "";
my $directory = "";
$Net::SFTP::Foreign::debug = 1;

my $sftp = Net::SFTP::Foreign->new( "$username\@$host", password => $password );
$sftp->setcwd( "$directory" );
$sftp->put( "test_file.txt" => "test_file.txt" );

if ( $sftp->error )
{
	print "Put error: " . $sftp->error . "\n";
	print "Put status: " . $sftp->status . "\n";
}

=> perl test2.pl
# queueing msg len: 5, code:1, id:3 ... [1]
# waiting for message... [1]
# got it!, len:5, code:2, id:3, status: -
# queueing msg len: 15, code:16, id:0 ... [1]
# waiting for message... [1]
# got it!, len:63, code:104, id:0, status: -
# queueing msg len: 42, code:3, id:1 ... [1]
# waiting for message... [1]
# got it!, len:10, code:102, id:1, status: -
# queueing msg len: 18, code:10, id:2 ... [1]
# waiting for message... [1]
# got it!, len:106, code:101, id:2, status: 3
# queueing msg len: 10, code:4, id:3 ... [1]
# waiting for message... [1]
# got it!, len:40, code:101, id:3, status: 0
Put error: Couldn't setstat remote file (fsetstat): The requested operation cannot be performed because there is a file transfer in progress.
Put status: The requested operation cannot be performed because there is a file transfer in progress.
 
In the OP you don't say why /dev/urandom can't be opened. Try changing the line in DH.pm to
Code:
die "Couldn't open /dev/urandom[red]: $![/red]";
to see if it gives you any more info.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
DH.pm is part of the default installation and I only have read permission to that file. I'm not an admin on this box so I can't change it. Sorry.

Thanks for looking at this though. I do appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top