简体   繁体   English

使用 Perl 中的 SSH 编辑远程文件

[英]Edit a remote file using SSH in Perl

Below is the code snippet I have written.下面是我写的代码片段。

  1. This tries to fetch the server name and file name through the arguments这会尝试通过 arguments 获取服务器名称和文件名
  2. I am stuck at editing the remote file after the server name and corresponding file name are fetched.在获取服务器名称和相应的文件名后,我一直在编辑远程文件。

Command run: server.pl sox3d1 TEST50命令运行: server.pl sox3d1 TEST50

File server.pl :文件server.pl

    use strict;
    use warnings;
    use Data::Dumper;
    use XML::Simple;
    
    my $host=$ARGV[0];
    my $db_host=$ARGV[1];
    
    my %servers;
    $servers{"ser14316.local.net"}=["sox3d1","sox3d2"] ;
    $servers{"ser143hn.local.net"}=["sox4d1","sox4d2"] ;
    $servers{"ser14441.local.net"}=["sox6d1","sox6d2"] ;
    $servers{"ser18163.local.net"}=["soxuat61","soxuat62","soxuat63"] ;
    $servers{"ser1444r.local.net"}=["soxuat51","soxuat52","soxuat53"] ;
    
    
    my $files=
    {
      db=>"/appl/$host/db_info.ref",
    };
    
    my %groups_by_host;
    for my $group (keys(%servers)) {
       for my $host (@{ $servers{$group} }) {
          push @{ $groups_by_host{$host} }, $group;
       }
    }
    
    
    my $server = $groups_by_host{$host};  # we get the server name via variable $host
    my ( $ssh_to_server )=@$server; # This is the server to ssh
    
    my $dest_file=$files->{"db"} ; #This will fetch the file from the remote server
    
    print "$ssh_to_server","->","$dest_file \n"; 

The file in this server has to be edited and below are the steps to do that via ssh.必须编辑此服务器中的文件,以下是通过 ssh 执行此操作的步骤。

  1. Search for the string $host (ie sox3d3 ) in the remote file $dest_file by doing ssh to server $ssh_to_server通过对服务器$ssh_to_server执行 ssh 在远程文件$dest_file中搜索字符串$host (即sox3d3
  2. Replace the string TEST30 with TEST50将字符串TEST30替换为TEST50

The file is a semicolon-separated file该文件是分号分隔的文件

File content in remote server ( db_info.ref ):远程服务器中的文件内容( db_info.ref ):

sox3d3   ;/appl/sox3d3/current   ;TEST30     ;TEST30      ;USER_10   ;USER_30
sox4d4   ;/appl/sox4d4/current   ;TEST40     ;TEST40      ;USER_20   ;USER_40

So, the 3rd and 4th columns ( TEST30;TEST30 ) should be replaced with TEST50 as per input.因此,应根据输入将第 3 列和第 4 列( TEST30;TEST30 )替换为TEST50

Have tried the below code to get it working已尝试以下代码以使其正常工作

as i said i was trying for a one line and had many variables in the command, the below worked actually正如我所说,我正在尝试一行并在命令中有很多变量,下面的实际工作

Update 1:更新1:

This basically ssh's the server and changes the file accordingly这基本上是 ssh 的服务器并相应地更改文件

  system(
     'ssh' => ('-q',$ser),
     'sed' => ('-i -E', qq('/$host/s#([^;]+;[^;]+;)[^;]+;[^;]+#\\1$db_host 
     ;$db_host #') , $dest_file),

Tried something like this?尝试过这样的事情吗?

my $destination = sprintf( '%s@%s', $user, $host );
my $file        = '/path/to/some/file.txt';
system(
  'ssh' => $destination,
  'sed' => ( '-i', 's/TEST30/TEST50/g', $file ),
);

The ssh documentation shows you can pass a command after the ssh destination. ssh文档显示您可以在 ssh 目标之后传递命令。 When you do this, ssh will log into the server, run that command, then log out again, instead of dropping you into the shell on the server.当您执行此操作时,ssh 将登录到服务器,运行该命令,然后再次注销,而不是让您进入服务器上的 shell。

So you can use that to run sed to edit the file.所以你可以使用它来运行sed来编辑文件。

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

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