in reply to Net::SSH::Expect output

Code?

Replies are listed 'Best First'.
Re^2: Net::SSH::Expect output
by sierpinski (Chaplain) on Jul 23, 2009 at 16:20 UTC
    The first part checks for the filesystem type.. zfs - do nothing, since it doesn't use SVM... the 2nd part of the if statement processes the ufs part, by running metastat -p and counting the number occurrences of the letter d... for a mirror with 2 plexes it should be 3, one for the metadevice, and one for each plex. Anything less than 3 gets thrown out as being unsynched. The $mirror_cmd is the metastat -p |grep m to get the metadevices only.

    Here is the while loop for that part of the code:
    while ( defined ($line = $ssh->read_line()) ) { if($line =~ m/zfs/) { open(MIR,">>$MIRRORS"); print MIR "$_ - has ZFS root, no metadevices e +xist\n"; close(MIR); } else { # Not ZFS, presumably ufs # Execute SVM Mirror check - looking for unsyn +ched mirrors my $mirror_output = $ssh->send("$mirror_cmd"); my $line2; while ( defined ($line2 = $ssh->read_line()) ) + { my $tmpcount = ($line2 =~ tr/d//); if($tmpcount < 3) { open(MIR,">>$MIRRORS"); print MIR "$_: "; chomp(); print MIR "$line2 - Not Mirror +ed\n"; close(MIR); } } } }

    On a side note, apparently setting PS = ; had no effect, because it's still showing the same output as earlier.

    Thanks!

      This doesn't look like expect code. It looks like regular old read-the-file-regex-output code. So, you may be missing the cool expect features.

      I have a guess: ssh and Expect can't really know what's prompt and what's command output. It's all a stream on stdout. You probably have to clear a buffer or something like that after you log in but before you run the command. It's significant that it's the 1st line on server2, because that's what you'd expect to see if you logged in and typed a command.

      Does it always happen on server2? If so, it might be a shell setting specific to that account and that server.

      Finally, you could just regex out the prompt from the input. You don't need to worry about speed so an extra regex on each line won't hurt anyone.

      Really Finally, you could scrap SSH::Expect and use ssh with public keys instead of passwords. That might set up the environment better and not show you the prompt. Since you're not really using the expect features, that might be an easier way to go.

        Currently it is just regular old read/regex code, but I do have plans to add the expect features once I get this part working.... and I do have ssh keys set up. I'm not passing it a password when I create the object (mostly because this is running under an account where not everyone should know the password).

        It always happens on server2, but server2 is no different than server1. They have the same OS version, and the ID is the same for both, (automounted home directory) so it's using the same profile and env variables and all that.

        It's not too much trouble to regex out the prompt, I can do that easily enough, I was just hoping there was some cool Expect feature that would do that for me, or maybe I was doing something wrong.

        One other thing I'm running into, however, is that when I come across a server that my key doesn't work on (ie one that does not use autofs for home directories) I get a password prompt. I'd like to be able to just detect these, add it to my no-connect list with a comment, so I can get a list of servers that need a manual key set up later, but instead it's trying to execute the commands, (in essence pressing enter 3 times) then getting the permission denied keyboard/interactive message, then the SSH connection dies, so I receive the SSHProcessError message.

        It's obvious to me I need to use more of the expect features, as that would (probably) easily fix my most recent problem, but it does lead me to another question:

        If I create an ssh connection, then issue a waitfor (or expect) statement, what keeps the prompt from displaying too fast for the waitfor, so then I'm waiting for nothing until the timeout. For example:

        1. ssh serverX
        2. (serverX asks for a password - Password:)
        3. waitfor "Ppassword:"

        What keeps that from happening, or does it just queue up in STDIN?

        Thanks!