用于XML编辑的系统(perl-pi-e)

用于XML编辑的系统(perl-pi-e),perl,Perl,可能重复: 谁能帮帮我吗。看着这一行,我快发疯了。我试图使用perl-pi-e来编辑XML文件 system("ssh -t <HOST> \"sudo su - root -c 'perl -pi -e 's/xmlNamespaceAware=\"false\">/xmlNamespaceAware=\"false\"><Alias>$virtualhost1<\/Alias>/g' /home/harrikr/Scripts/TcServ

可能重复:

谁能帮帮我吗。看着这一行,我快发疯了。我试图使用perl-pi-e来编辑XML文件

 system("ssh -t <HOST> \"sudo su - root -c 'perl -pi -e 's/xmlNamespaceAware=\"false\">/xmlNamespaceAware=\"false\"><Alias>$virtualhost1<\/Alias>/g' /home/harrikr/Scripts/TcServerScripts/data.xml'\"");
system(“ssh-t\”sudo su-root-c'perl-pi-e's/xmlNamespaceAware=\'false\”>/xmlNamespaceAware=\'false\”>$virtualhost1/g'/home/harrikr/Scripts/TcServerScripts/data.xml'\);

这不起作用,我已经尝试过所有类型的组合。

你已经问过这个问题,答案是你把所有的引用都弄混了。以下是您所拥有的:

"ssh -t <HOST> \"sudo su - root -c 'perl -pi -e '...' ...'\""
为什么要在一个大的系统调用中这样做?我现在从我自己的工作中了解到这种情况是有必要的,但我也知道我用自己的方法解决了它。它处理所有这些愚蠢的细节


而且,正如前面对同一问题的回答所指出的,那些
sudo
su
调用真的很可怕。

从内到外构建它

use String::ShellQuote qw( shell_quote );

my $host         = 'HOST';
my $path_to_xml  = '/home/harrikr/Scripts/TcServerScripts/data.xml';
my $virtualhost1 = 'VIRTUALHOST1';

my $perl_prog = <<'__EOI__';
    BEGIN { my $vh = shift(@ARGV); }
    s/xmlNamespaceAware="false">\K/<Alias>\Q$vh\E</Alias>/g;
__EOI__

my $perl_cmd = sprintf q{perl -i -pe%s %s %s},
   shell_quote($perl_prog),
   shell_quote($virtualhost1),
   shell_quote($path_to_xml);

my $su_cmd = sprintf q{sudo su - -c%s},
    shell_quote($perl_cmd);

my $ssh_cmd = sprintf q{ssh -t %s %s},
   shell_quote($host),
   shell_quote($su_cmd);

system($ssh_cmd);

“我试图使用perl-pi-e编辑XML文件。”好的,但是编辑该文件的目的是什么?您是否有任何错误?定义“将不起作用”。请告诉我问题是,你试图访问的服务器有某种安全性的外表,你不能像那样进行根操作。我收到错误消息,抱怨操作员。Bareword在Untitled.pl第7行的运算符处找到,在“ssh-t harrikr\@”sudo su-root-c perl-pi-e的{xmlNamespaceAware=“false”字符串处找到,在Untitled.pl第7行的运算符处找到,在“false”附近}{xmlNamespaceAware=“”Bareword在Untitled.pl第7行的运算符处找到,在“>}{xmlNamespaceAware=“false”附近找到(假之前缺少操作员?)@user1272415,你确定sudo和su都是必需的吗?如果你可以调用sudo,那么调用su似乎是多余的。谢谢,我一定错过了最后一个答案,但这非常有用。我认为Net::SSH::Perl::ProxiedIPC将对我必须完成的任务非常有帮助。再次感谢!!!文件归root所有,但测试用例文件归root所有harrikr@user1272415,Net::SSH::Perl::ProxiedIPC有三个影响您的限制。据我所知,ProxiedIPC并没有为您提供执行su和/或sudo的方法(没有外壳,这会让您回到原始问题),ProxiedIPC不允许您将命令行选项传递给Perl,并且ProxiedIPC不允许您将参数传递给Perl脚本。Ike我接受了该代码并输入了我的名字,然后将其ssh发送到远程服务器,但没有修改data.xml。我看不出您在何处定义shell_quote($host)$host,如果您的意思是“我看不出您在哪里定义了shell_quote”,那么答案是“使用字符串::ShellQuote qw(shell_quote);“。否则,我不理解你的问题。这就是我的意思。对不起。我已经做了一周了,而且我对perl还不熟悉。我非常擅长BASH脚本,但我无法完成最后一部分。即使有你的建议,我也可以将数据写入data.xml文件。sudo似乎工作得很好。
my $perl = q(perl -pi -e \\'...\\');
my $command = qq(sudo su - root -c '$perl');
use String::ShellQuote qw( shell_quote );

my $host         = 'HOST';
my $path_to_xml  = '/home/harrikr/Scripts/TcServerScripts/data.xml';
my $virtualhost1 = 'VIRTUALHOST1';

my $perl_prog = <<'__EOI__';
    BEGIN { my $vh = shift(@ARGV); }
    s/xmlNamespaceAware="false">\K/<Alias>\Q$vh\E</Alias>/g;
__EOI__

my $perl_cmd = sprintf q{perl -i -pe%s %s %s},
   shell_quote($perl_prog),
   shell_quote($virtualhost1),
   shell_quote($path_to_xml);

my $su_cmd = sprintf q{sudo su - -c%s},
    shell_quote($perl_cmd);

my $ssh_cmd = sprintf q{ssh -t %s %s},
   shell_quote($host),
   shell_quote($su_cmd);

system($ssh_cmd);
use String::ShellQuote qw( shell_quote );

my $host         = 'harrikr@HOST';
my $path_to_xml  = '/home/harrikr/Scripts/TcServerScripts/data.xml';
my $virtualhost1 = 'VIRTUALHOST1';

my $perl_prog = <<'__EOI__';
    BEGIN { my $vh = shift(@ARGV); }
    s/xmlNamespaceAware="false">\K/<Alias>\Q$vh\E</Alias>/g;
__EOI__

my $perl_cmd = sprintf q{perl -i -pe%s %s %s},
   shell_quote($perl_prog),
   shell_quote($virtualhost1),
   shell_quote($path_to_xml);

my $ssh_cmd = sprintf q{ssh %s %s},
   shell_quote($host),
   shell_quote($perl_cmd);

system($ssh_cmd);