简体   繁体   English

在终端上的Perl打印输出中反引号

[英]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: " . 手动键入此命令时,将提示您输入“ 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 : 您可能要查看ssh-f标志:

  -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: IIRC,它是这样的:

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. 确保将./datekey存储在其他用户根本无法访问的位置-甚至没有读取权限。


If you're just trying to detect, you might simply need to feed it EOF to get it to move along: 如果您只是想检测,则可能只需要提供EOF即可继续前进:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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