[Ssh-sftp-perl-users] SOLVED: Fully scripted interactive SSH2 connection - example included
Brought to you by:
dbrobins
From: Matthew J. S. <vag...@ya...> - 2006-03-31 17:09:56
|
After banging my head against the wall for the last week and a half, I finally got something working. I used the code from the shell sub in Net::SSH::Perl::SSH2. Basically I copied the shell sub into my main script and changed 2 lines. I send all of the data that is received during an ssh session to a sub named: ssh_interact In ssh_interact, the received data is compared to what we are expecting to receive, and when a match is made, the specified command is sent. The logic in the sub completely depends on what you plan on doing with it. I gave an example below, but it's wide open, so do what you want. Now that I have done this, I will kick myself for the rest of the week for not thinking about it earlier. This also works with Net::SSH::W32Perl #!/usr/bin/perl -w use strict; use Net::SSH::Perl; use Net::SSH::Perl::Constants qw( :msg2 ); my %params; $params{"debug"} = 0; $params{"port"} = 22; $params{"protocol"} = 2; $params{"interactive"} = 1; $params{"compression"} = 0; $params{"PreferredAuthentications"} = 'password'; $params{"PubkeyAuthentication"} = 'no'; $params{"RSAAuthentication"} = 'no'; $params{"password_prompt_host"} = 0; $params{"auth_kbd_interactive"} = 0; $params{"use_pty"} = 0; my $stdoutcnt = 0; my $host = "10.10.10.127"; my $user = "username"; my $pw = "password"; my $ssh = Net::SSH::Perl->new($host, %params, options => ["BatchMode no"]) || die "Cannot Connect to system $! $@\n"; print "Opening Connection\n"; $ssh->login($user,$pw) || die "Cannot Login $! $@\n"; my $cmgr = $ssh->channel_mgr; my $channel = $ssh->_session_channel; $channel->open; $channel->register_handler(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, sub { my($channel, $packet) = @_; my $r_packet = $channel->request_start('pty-req', 0); my($term) = $ENV{TERM} =~ /(\w+)/; $r_packet->put_str($term); $r_packet->put_int32(0) for 1..4; $r_packet->put_str(""); $r_packet->send; $channel->{ssh}->debug("Requesting shell."); $channel->request("shell", 0); }); my($exit); $channel->register_handler(SSH2_MSG_CHANNEL_REQUEST, $ssh->_make_input_channel_req(\$exit)); $channel->register_handler("_output_buffer", sub { syswrite STDOUT, $_[1]->bytes; ssh_interact($_[1]->bytes); }); $channel->register_handler("_extended_buffer", sub { syswrite STDERR, $_[1]->bytes; }); $ssh->debug("Entering interactive session."); $ssh->client_loop; sub ssh_interact { # Here is where the interaction happens # All prompts get printed to the screen # as well as put in $stdout # # From this sub, you can execute commands # and interact with them as well. # # You just need to know what the prompts # will be and then you can script them. # # There is alot of logic that can be added # here, but that's up to each user. # # You can keep track of where you are by # following $stdoutcnt. This entire sub # could be written much better, but it's just # an example. $expected1 = "[user@machine ~]#"; $expected2 = qq|cp: overwrite `/var/log/messages'?|; # Notice that at the end of the commands I am # sending a \n $command1 = "cp /etc/fstab /var/log/messages\n"; $command2 = "n\n"; print "STDOUTcnd - $stdoutcnt\n"; my $stdout = $_[0]; if ($stdout =~ /$expected1/i){ $channel->send_data($command1); $stdoutcnt++; return; } if ($stdout =~ /$expected2/i){ $channel->send_data("$command2"); $stdoutcnt++; return; } } __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com |