perl使用已定义的变量发出os命令

时间:2013-07-02 16:49:45

标签: perl unix kerberos

我在我的脚本中添加了功能,以便他们可以使用kerberos身份验证自动运行并在执行时使用安全协议。我有我的功能为shell脚本工作,完全符合我的要求,但是我有问题将它移植到perl以在我的perl脚本中工作,因为我是perl的新手。这是我的工作shell代码,并试图在perl中获得相同的功能:

#!/bin/sh
ticketFileName=`basename $0-$$` #set filename variable to name of script plus the PID
krb5CacheLocation=/tmp/$ticketFileName #set ticket cache location to /tmp + script name
/usr/share/centrifydc/kerberos/bin/kinit -c $krb5CacheLocation -kt /root/.ssh/someaccount.keytab someaccount #get TGT and specifiy ticket cache location on kinit
export KRB5CCNAME=$krb5CacheLocation #set the KRB5CCNAME variable to tell ssh where to look

我在perl中尝试过的事情:

#!/usr/bin/perl
my $ticketFileName = `basename $0-$$`;
my $krb5CacheLocation = '/tmp/'.$ticketFileName;
`export KRB5CCNAME=$krb5CacheLocation`;
`/usr/share/centrifydc/kerberos/bin/kinit -c $krb5CacheLocation -kt /root/.ssh/someaccount.keytab someaccount`;

似乎它不喜欢我在OS命令中引用的传递变量。有人有任何想法或建议吗?

3 个答案:

答案 0 :(得分:1)

a 问题是您将KRB5CCNAME作为环境变量建立在一个立即丢弃的shell中。迈克尔有一个替代方案。您还可以使用以下语法在kinit调用上传递环境变量:

`KRB5CCNAME=$krb5CacheLocation /usr/share/centrifydc/kerberos/bin/kinit etc.`

虽然当你丢弃输出时,只需使用system,或者,如果这也是你要做的最后一件事,exec

exec "KRB5CCNAME=$krb5CacheLocation /usr/share/centrifydc/kerberos/bin/kinit etc."

%ENV操纵的好处是您可以使用多参数exec

答案 1 :(得分:0)

#!/usr/bin/perl

   # what is the value of $0 , which script? a: set to current script
   my $ticketFileName = `basename $0`;
   chomp($ticketFileName);
   $ticketFileName .= $$;
   my $krb5CacheLocation = '/tmp/'.$ticketFileName;
   $ENV{'KRB5CCNAME'}=$krb5CacheLocation;
   @x = qx(/usr/share/centrifydc/kerberos/bin/kinit -c $krb5CacheLocation -kt                    
     /root/.ssh/unixmap0000.keytab unixmap0000);
   print join "\n",@x,"\n";

答案 2 :(得分:0)

我想你想要这样的东西:

use File::Basename;

# set filename variable to name of script plus the PID
my $ticket_file_name = basename "$0" . "-$$";

# set ticket cache location to /tmp + script name
my $krb5CacheLocation = "/tmp/$ticket_file_name";

# set the KRB5CCNAME variable to tell ssh where to look
$ENV{KRB5CCNAME} = $krb5CacheLocation;

# get TGT and specifiy ticket cache location on kinit
system( "/usr/share/centrifydc/kerberos/bin/kinit -c $krb5CacheLocation -kt /root/.ssh/someaccount.keytab someaccount" );