perl bzw. sh Problem

Bevor du nicht die komplette Kommandozeile postest, die fehlschlägt (das war der Hinweis auf die Debug-Ausgabe), kann dir hier niemand helfen. Glaskugeln sind nunmal Mangelware.

Rob

Ausführung
Code:
....
{                               
        $random = $ls_out[rand($anzahl)];
        system("echo \"$random\" > /tmp/vpn_connection");
        system("chmod 660 /tmp/vpn_connection");
        my $cmd_1 = "doas sh -c 'openvpn --config $path/$random;chown root:wheel /tmp/vpn_connection'";
        warn "$cmd_1\n";
        system("$cmd_1");
}
...

Ausgabe vom Debug
Code:
doas sh -c 'openvpn --config /etc/openvpn/stadt.ovpn
;chown root:wheel /tmp/vpn_connection'
doas (user@example.com) password: 
sh: syntax error: `;' unexpected

Wenn ich die Ausgabe vom Debug ausführe kommts aufs gleiche raus (klar wegen des Semicolons). Sorry seh grade wie doof es war die Ausgabe vom Debug nicht direkt zu posten, hätte sicherlich Zeit gespart.
 
Hehe. Das Problem ist nicht das Semikolon, sondern dass du die Ausgabe von ls nicht filterst. Du also hast Newlines am Ende jedes Dateinamen, als Workaround nutze Folgendes:

Code:
@ls_out = `ls $path | perl -lane 'print if /.ovpn/'`;
foreach(@ls_out) 
    {
    s/\n$//;
    }
Diesen Fehler würdest du nicht machen, wenn du das Verzeichnis mit opendir() + readdir() einlesen würdest, statt den Umweg über ls.

Rob
 
Code:
#!/usr/bin/env perl
#===============================================================================
#
#         FILE: ovpn.pl
#
#        USAGE: ./ovpn.pl  
#
#  DESCRIPTION: 
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: xxxx (), 
# ORGANIZATION: 
#      VERSION: 1.0
#      CREATED: 05/15/17 15:09:33
#     REVISION: ---
#===============================================================================

use strict;
use warnings;
use utf8;

my @ls_out;
my $count = 0;
my $zaehler;
my $anzahl;
my $input;
my $random;

my $path = "/etc/openvpn";              # Path to folder with *.ovpn files

opendir my($dh), $path;
@ls_out = readdir $dh;
closedir $dh;

@ls_out = grep (/.ovpn/, @ls_out);

$anzahl = @ls_out;

print "0.) Kill openvpn and change /etc/resolv.conf\n";

while ( $anzahl > $count )
{
    $zaehler = $count + 1;
    print "$zaehler.) $ls_out[$count]\n";
    $count += 1;
}

print "99.) Random connection\n";

print "Choose: \n";
chomp( $input = <STDIN> );

if ( $input > 0 )
{
    if ( $input == 99 )             # Connection via random Array -> Random-VPN
    {                               # /tmp/vpn_connection gets deleted by $path/up.sh
        $random = $ls_out[rand($anzahl)];
        system("echo \"$random\" > /tmp/vpn_connection");
        system("chmod 400 /tmp/vpn_connection");
        system("doas sh -c 'chown root:wheel /tmp/vpn_connection;openvpn --config $path/$random'");
    }
    else                            # Connection with VPN out of list
    {                               # /tmp/vpn_connection gets deleted by $path/up.sh
        $input -= 1;
        system("echo \"$ls_out[$input]\" > /tmp/vpn_connection");
        system("chmod 400 /tmp/vpn_connection");
        system("doas sh -c 'chown root:wheel /tmp/vpn_connection;openvpn --config $path/$ls_out[$input]'");
    }
}
else                                # killing process and resetting nameserver
{
    system("doas sh -c 'pkill openvpn;echo \"nameserver 127.0.0.1\" > /etc/resolv.conf'");
}
Habe meinen Fehler verstanden und möchte mich nochmal für die Hilfe bedanken. Verbesserte Version siehe Codeblock
 
Zurück
Oben