Thread: [Ssh-sftp-perl-users] Using Net::SSH2 as a file handle
Brought to you by:
dbrobins
From: Bryan B. <br...@bu...> - 2006-01-19 20:01:31
|
Greetings, Some time ago I used Net::SSH::Perl, but gave it up for Expect. Now I'm back looking at Net::SSH2, but would still like to put expect on top of it. I want to use Net::SSH2 because it appears thread safe, while spawining a process with a tty in expect for perl is not. What I want to do is create a Net::SSH2 object, connect, get an interactive shell, and interact with it via a file handle. Then i can use expect on top of that and do whatever I like. I dont fully understand channels, but from what I remember with Net::SSH::Perl, a new channel had to be created every time you sent a command. But now there is a Net::SSH2::Channel->shell() function, perhaps there is a way to launch a shell on a channel and interact with that? I see Net::SSH2::Channel is itself a tied filehandle, but attempts to directly manipulate that dont result in what I expected. Thanks. Bryan http://www.sourceforge.net/projects/rover |
From: David R. <dbr...@cp...> - 2006-01-20 03:17:00
|
On Thursday January 19, 2006 09:02, Bryan Bueter wrote: > Greetings, Some time ago I used Net::SSH::Perl, but gave it up for Expect. > Now I'm back looking at Net::SSH2, but would still like to put expect on > top of it. I want to use Net::SSH2 because it appears thread safe, while > spawining a process with a tty in expect for perl is not. > > What I want to do is create a Net::SSH2 object, connect, get an > interactive shell, and interact with it via a file handle. Then i can use > expect on top of that and do whatever I like. > > I dont fully understand channels, but from what I remember with > Net::SSH::Perl, a new channel had to be created every time you sent a > command. But now there is a Net::SSH2::Channel->shell() function, perhaps > there is a way to launch a shell on a channel and interact with that? There is. The shell() function just makes the channel a shell channel, and you can send commands to it. > I see Net::SSH2::Channel is itself a tied filehandle, but attempts to > directly manipulate that dont result in what I expected. In what way? If you can give details I'll try to fix them. -- Dave Isa. 40:31 |
From: Bryan B. <br...@bu...> - 2006-01-20 14:50:59
|
>> I dont fully understand channels, but from what I remember with >> Net::SSH::Perl, a new channel had to be created every time you sent a >> command. But now there is a Net::SSH2::Channel->shell() function, >> perhaps >> there is a way to launch a shell on a channel and interact with that? > > There is. The shell() function just makes the channel a shell channel, > and you can send commands to it. > Does this act like a normal shell? Primarily does it just exec shell and pass IO back and forth, or is it interpreted somehow? >> I see Net::SSH2::Channel is itself a tied filehandle, but attempts to >> directly manipulate that dont result in what I expected. > > In what way? If you can give details I'll try to fix them. > > -- > Dave > Isa. 40:31 > With this snipit of code: -- snip --> use Net::SSH2; use Expect; my $ssh2 = Net::SSH2->new(); $ssh2->connect("127.0.0.1"); $ssh2->auth(username => scalar getpwuid($<), interact => 1) or die; my $chan = $ssh2->channel(); $chan->shell(); my $exp_obj = Expect->exp_init( $chan ); <-- end -- Returns the following when I run the script: $ ./test.pl [user username] Password: ERROR: cannot find method `FILENO' in class Expect at ./test3.pl line 13 Line 13 is the expect init line. Maybe its an expect bug, but I dont think so. I've been able to open expect on raw sockets before. So i think it has something to do with the way channel() sets itself up. Thanks. Bryan http://sourceforge.net/projects/rove |
From: David R. <dbr...@cp...> - 2006-01-20 17:24:08
|
On Friday January 20, 2006 03:52, Bryan Bueter wrote: > >> I dont fully understand channels, but from what I remember with > >> Net::SSH::Perl, a new channel had to be created every time you sent a > >> command. But now there is a Net::SSH2::Channel->shell() function, > >> perhaps > >> there is a way to launch a shell on a channel and interact with that? > > > > There is. The shell() function just makes the channel a shell channel, > > and you can send commands to it. > > Does this act like a normal shell? Primarily does it just exec shell and > pass IO back and forth, or is it interpreted somehow? I think it does act like a normal shell; you'll probably have to test it though. I'm not sure what the ssh spec says regarding the actual shell at the other end. > >> I see Net::SSH2::Channel is itself a tied filehandle, but attempts to > >> directly manipulate that dont result in what I expected. > > > > In what way? If you can give details I'll try to fix them. > > With this snipit of code: > > -- snip --> > use Net::SSH2; > use Expect; > > my $ssh2 = Net::SSH2->new(); > $ssh2->connect("127.0.0.1"); > $ssh2->auth(username => scalar getpwuid($<), interact => 1) or die; > > my $chan = $ssh2->channel(); > $chan->shell(); > > my $exp_obj = Expect->exp_init( $chan ); > <-- end -- > > Returns the following when I run the script: > $ ./test.pl > [user username] > Password: ERROR: cannot find method `FILENO' in class Expect at ./test3.pl > line 13 > > Line 13 is the expect init line. Maybe its an expect bug, but I dont > think so. I've been able to open expect on raw sockets before. So i > think it has something to do with the way channel() sets itself up. Ah, then there's the problem. Fileno tries to get a real Unix filehandle out of the object; since it isn't a real Unix file, you won't get one. Maybe something can be worked out with pipes so that it really is a handle (create a pipe, send the channel data to one end, give expect the other end) but it's beyond the scope of the current implementation. -- Dave Isa. 40:31 |
From: Bryan B. <br...@bu...> - 2006-01-20 20:22:06
|
> > Ah, then there's the problem. Fileno tries to get a real Unix filehandle > out > of the object; since it isn't a real Unix file, you won't get one. > > Maybe something can be worked out with pipes so that it really is a handle > (create a pipe, send the channel data to one end, give expect the other > end) > but it's beyond the scope of the current implementation. > > -- > Dave > Isa. 40:31 Ok, forgive me if my ignorance misses something here. Why not just implement channel as a real filehandle? You would still do everything as is, but only now you could create a shell, then print things directly to and read things directly from it. Which begets the question: What AM I to do with a shell created by $chan->shell() ? Do I have to keep polling this to know when I can write to it? I havnt been able to do much with this other then print out the first couple of lines after it connects. Printing directly to it and/or execing on top of shell doesnt seem to produce any more output then that. An example might be handy, and/or some further documentation. Thanks. Bryan http://sourceforge.net/projects/rover |
From: David R. <dbr...@cp...> - 2006-01-21 02:44:50
|
On Friday January 20, 2006 09:22, Bryan Bueter wrote: > > > > Ah, then there's the problem. Fileno tries to get a real Unix filehandle > > out > > of the object; since it isn't a real Unix file, you won't get one. > > > > Maybe something can be worked out with pipes so that it really is a handle > > (create a pipe, send the channel data to one end, give expect the other > > end) > > but it's beyond the scope of the current implementation. > > Ok, forgive me if my ignorance misses something here. > > Why not just implement channel as a real filehandle? You would still do > everything as is, but only now you could create a shell, then print things > directly to and read things directly from it. Because it's not a real file. It's a stream multiplexed over an encrypted socket connection formatted by the ssh protocol. If you read the raw socket you'd get ssh frames and encrypted data. > Which begets the question: What AM I to do with a shell created by > $chan->shell() ? Do I have to keep polling this to know when I can write > to it? I havnt been able to do much with this other then print out the > first couple of lines after it connects. Printing directly to it and/or > execing on top of shell doesnt seem to produce any more output then that. You don't exec on top of shell - exec is like exec(2) and doesn't let you run subsequent operations. When you have a shell channel you can send it commands and read back the responses using the standard I/O commands in Net::SSH2::Channel (or with filehandle ops using the tie). > An example might be handy, and/or some further documentation. If you get a chance to write one please submit it for inclusion. Thanks, -- Dave Isa. 40:31 |
From: Bryan B. <br...@bu...> - 2006-01-23 14:19:35
|
> > Because it's not a real file. It's a stream multiplexed over an encrypted > socket connection formatted by the ssh protocol. If you read the raw > socket > you'd get ssh frames and encrypted data. > I understand the logistics of the SSH connection, and that writing directly to the socket is not what i'm suggesting. I'm not a perl hacker so i'm sure its more complicated then I think, but I what I had in mind was, since you already create a tied filehandle that one can use to send IO to after a shell channel is created, why not create a real file handle. I'll take your previous response as, its way more complicated then I think, and consider it out of scope. Unless that explanation somehow makes a difference. > > You don't exec on top of shell - exec is like exec(2) and doesn't let you > run > subsequent operations. When you have a shell channel you can send it > commands and read back the responses using the standard I/O commands in > Net::SSH2::Channel (or with filehandle ops using the tie). > Yeah, I was having problems actually using a shell channel, i was trying anything. So noted. >> An example might be handy, and/or some further documentation. > > If you get a chance to write one please submit it for inclusion. > > Thanks, > > -- > Dave > Isa. 40:31 I'll let you know about an example/documentation. I'll be messing with it over the next week or so as I have free time, If I come up with something I'll submit it for your review. Thanks, Bryan http://sourceforge.net/projects/rover |