简体   繁体   中英

backtick in Perl printing output on terminal

I am trying to get the output of a command in a variable and checking whether its matching with other variable.

   $login1=`ssh ****************** date`;

This command when typed manually will expect a prompt " Password: " . When i run it from the script it is ruuning that command and printing that prompt waiting for user to enter, but i dont need that. I just need to get that output and compare

     if($login1=~ /Password:/)
            {   
              print " yes";
            }
         else
              {
                print "No ";
               }

However the script is just stopping at Password prompt . Please suggest me on how to achieve this .

You might want to look at the -f flag for ssh :

  -f Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background. This implies -n. The recommended way to start X11 programs at a remote site is with something like ssh -f host xterm. 

If you want to avoid passwords, set up a public/private key pair with no passphrase (dangerous, but much less dangerous than putting a password in a script) and copy the public key to the remote site. IIRC, it goes something like this:

localhost $ ssh-keygen -b 2048 -t ecdsa -N '' -f ./datekey
localhost $ scp ./datekey.pub remotehost:/tmp
localhost $ ssh remotehost
(login)
remotehost $ cat /tmp/datekey.pub >> ~/.ssh/authorized_keys
remotehost $ logout

localhost $ ssh -i ./datekey remotehost date

Make sure you store ./datekey somewhere no other user can access it at all -- not even read access.


If you're just trying to detect, you might simply need to feed it EOF to get it to move along:

$login1=`ssh ****************** date < /dev/null`;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM