WTem has asked for the wisdom of the Perl Monks concerning the following question:

Hello PerlMonks !! Here i am seeking for your Wisdom again ^^

My goal is to auto save the configuration parameters of a router Netcomm 140w.

The procedure is to connect to the router with ssh and use the following commands to create the backup conf file:

# cd /tmp # dbcfg_export -o /tmp/filename.cfg -p "password" # tar -C /usr/local/cdcs -zcvf - ipsec.d openvpn-keys ssh-hostkeys | o +penssl des3 -salt -k "password" | dd of=/tmp/vpn.des3 # tar -zcvf /opt/cdcs/upload/filename.cfg.tar.gz filename.cfg vpn.des3

The second command will create the file "/tmp/filename.cfg". In the end the backup conf file created is "/opt/cdcs/upload/filename.cfg.tar.gz" wich I will next recover via SCP

Below an exemple with filename = test and with no password. Don't bother with the missing files with the 1st tar command warning;

root:/# cd /tmp root:/tmp# dbcfg_export -o /tmp/test.cfg -p "" root:/tmp# tar -C /usr/local/cdcs -zcvf - ipsec.d openvpn-keys ssh-hos +tkeys | openssl des3 -salt -k "" | dd of=/tmp/vpn.des3 ipsec.d/ ipsec.d/crls/ ipsec.d/aacerts/ ipsec.d/policies/ ipsec.d/cacerts/ ipsec.d/certs/ ipsec.d/rsakey/ ipsec.d/private/ ipsec.d/ocspcerts/ tar: openvpn-keys: Cannot stat: No such file or directory ssh-hostkeys/ ssh-hostkeys/ssh_host_dsa_key ssh-hostkeys/ssh_host_rsa_key.pub ssh-hostkeys/ssh_host_ecdsa_key.pub ssh-hostkeys/ssh_host_key ssh-hostkeys/ssh_host_rsa_key ssh-hostkeys/ssh_host_dsa_key.pub ssh-hostkeys/ssh_host_key.pub ssh-hostkeys/ssh_host_ecdsa_key WARNING: can't open config file: /lib/ssl/openssl.cnf tar: Exiting with failure status due to previous errors 9+1 records in 9+1 records out 4792 bytes (4.7KB) copied, 0.081756 seconds, 57.2KB/s root:/tmp# tar -zcvf /opt/cdcs/upload/test.cfg.tar.gz test.cfg vpn.des +3 test.cfg vpn.des3

Now here my perl code, filename = test2 with no password

#!/usr/bin/perl -w ###################################### Perl Script ################### +############################### ## + ## ## + ## ## Auteur: WILLIAMS Temoe + ## ## Date: 21/04/16 + ## ## Description: The script will create a backup conf fi +le of a router Netcomm 140W used for business clients + ## ## ## ## + ## ## + ## ###################################################################### +############################### use strict; use warnings; #### Déclaration Bibliothèques use Net::SSH2; use Time::Piece; #### Déclaration Variables globales my $date = Time::Piece->new->strftime('%d-%m-%Y'); my $user='root'; my $password='W1m4x21h'; my $portssh=22; my $pwdfile=""; ################# Netcomm140w Backup process via SSH connexion ####### +################## ################# Use router @IP and client name as variables ######## +################# sub sauvegarde_netcomm { #### Variable declaration my($ip, $filename) = @_; #### Initialize SSH connexion my $ssh = Net::SSH2->new(); $ssh->connect($ip, $portssh) or die; if ($ssh->auth_password($user,$password)) { ## Command lines for the backup process my $chan = $ssh->channel(); $chan->shell(); $chan->blocking(0); print $chan "cd /tmp\n"; print $chan "dbcfg_export -o /tmp/$filename.cfg -p '$pwdfile +'\n"; print $chan "tar -C /usr/local/cdcs -zcvf - ipsec.d openvpn- +keys ssh-hostkeys | openssl des3 -salt -k '$pwdfile' | dd of=/tmp/vpn +.des3\n"; print $chan "tar -zcvf /opt/cdcs/upload/$filename.cfg.tar.gz + $filename.cfg vpn.des3\n"; #print $chan "rm -f /tmp/$filename.cfg\n"; #### Close SSH connexion $chan->close(); sleep (1); }else { #### Error message when ssh connexion fail warn "auth failed.\n"; } } ################# Main Program ################################ +######################## ################# Netcomm140w Backup ################################# +####################### sauvegarde_netcomm ('192.168.200.62' , 'test2');

Now we can see the problem by comparing the size of the created files

root:/tmp# ll /tmp/t* -rw-r--r-- 1 root root 31331 Apr 28 13:55 /tmp/test.cfg -rw-r--r-- 1 root root 31331 Apr 28 13:58 /tmp/test2.cf +g root:/tmp# ll /opt/cdcs/upload/ -rw-r--r-- 1 root root 12548 Apr 28 13:55 test.cfg.tar. +gz -rw-r--r-- 1 root root 20 Apr 28 13:58 test2.cfg.tar +.gz

I suppose that the problem comes from the 3rd command but I can't explain why

Can you please help me with this ? Best regards. Temoe

Replies are listed 'Best First'.
Re: Backup Script Pb
by salva (Canon) on Apr 29, 2016 at 08:20 UTC
    If you set non-blocking mode, the print calls may fail.

    Also, if you close the channel before the remote programs end and any of them tries to write data to its stdout or stderr streams it may get an error or a SIGHUP and abort.

      Oh yeah !!

      I deleted "$chan->blocking(0);"

      I added "sleep (15);" after the 2 tar commands line

      There is now only 2 octets difference between file test and test2. I tried the backup file created with the script and it works.

      Thanks for the help !! Once you have the answer it sounds so dump xD

        Instead of sleeping for a fixed amount of time, you can use the wait_eof method.