1 package FS::part_export::phone_shellcommands;
4 use vars qw(@ISA %info);
6 use String::ShellQuote;
9 @ISA = qw(FS::part_export);
12 #- modify command (get something from freepbx for changing PINs)
13 #- suspension/unsuspension
15 tie my %options, 'Tie::IxHash',
16 'user' => { label=>'Remote username', default=>'root', },
17 'useradd' => { label=>'Insert command', },
18 'userdel' => { label=>'Delete command', },
19 'usermod' => { label=>'Modify command', },
20 'suspend' => { label=>'Suspension command', },
21 'unsuspend' => { label=>'Unsuspension command', },
22 'mac_insert' => { label=>'Device MAC address insert command', },
23 'mac_delete' => { label=>'Device MAC address delete command', },
27 'svc' => [qw( svc_phone part_device )],
28 'desc' => 'Run remote commands via SSH, for phone numbers',
29 'options' => \%options,
31 Run remote commands via SSH, for phone numbers. You will need to
32 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Administration:SSH_Keys">setup SSH for unattended operation</a>.
33 <BR><BR>Use these buttons for some useful presets:
36 <INPUT TYPE="button" VALUE="FreePBX (build_exten CLI module needed)" onClick='
37 this.form.user.value = "root";
38 this.form.useradd.value = "build_exten.php --create --exten $phonenum --directdid 1$phonenum --sip-secret $sip_password --name $cust_name --vm-password $pin && /usr/share/asterisk/bin/module_admin reload";
39 this.form.userdel.value = "build_exten.php --delete --exten $phonenum && /usr/share/asterisk/bin/module_admin reload";
40 this.form.usermod.value = "build_exten.php --modify --exten $new_phonenum --directdid 1$new_phonenum --sip-secret $new_sip_password --name $new_cust_name --vm-password $new_pin && /usr/share/asterisk/bin/module_admin reload";
41 this.form.suspend.value = "";
42 this.form.unsuspend.value = "";
43 '> (Important note: Reduce freeside-queued "max_kids" to 1 when using FreePBX integration)
46 The following variables are available for interpolation (prefixed with new_ or
47 old_ for replace operations):
49 <LI><code>$countrycode</code> - Country code
50 <LI><code>$phonenum</code> - Phone number
51 <LI><code>$sip_password</code> - SIP secret (quoted for the shell)
52 <LI><code>$pin</code> - Personal identification number
53 <LI><code>$cust_name</code> - Customer name (quoted for the shell)
54 <LI><code>$pkgnum</code> - Internal package number
55 <LI><code>$mac_addr</code> - MAC address (Device MAC address insert and delete commands only)
60 sub rebless { shift; }
64 $self->_export_command('useradd', @_);
69 $self->_export_command('userdel', @_);
74 $self->_export_command('suspend', @_);
77 sub _export_unsuspend {
79 $self->_export_command('unsuspend', @_);
82 sub export_device_insert {
83 my( $self, $svc_phone, $phone_device ) = @_;
84 $self->_export_command('mac_insert', $svc_phone,
85 'mac_addr'=>$phone_device->mac_addr
89 sub export_device_delete {
90 my( $self, $svc_phone, $phone_device ) = @_;
91 $self->_export_command('mac_delete', $svc_phone,
92 'mac_addr'=>$phone_device->mac_addr
97 my ( $self, $action, $svc_phone, %addl_vars) = @_;
98 my $command = $self->option($action);
99 return '' if $command =~ /^\s*$/;
101 #set variable for the command
105 ${$_} = $svc_phone->getfield($_) foreach $svc_phone->fields;
106 ${$_} = $addl_vars{$_} foreach keys %addl_vars;
108 my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
109 my $pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
110 my $cust_name = $cust_pkg ? $cust_pkg->cust_main->name : '';
111 $cust_name = shell_quote $cust_name;
112 my $sip_password = shell_quote $svc_phone->sip_password;
113 #done setting variables for the command
115 $self->shellcommands_queue( $svc_phone->svcnum,
116 user => $self->option('user')||'root',
117 host => $self->machine,
118 command => eval(qq("$command")),
122 sub _export_replace {
123 my($self, $new, $old ) = (shift, shift, shift);
124 my $command = $self->option('usermod');
126 #set variable for the command
130 ${"old_$_"} = $old->getfield($_) foreach $old->fields;
131 ${"new_$_"} = $new->getfield($_) foreach $new->fields;
134 my $old_cust_pkg = $old->cust_svc->cust_pkg;
135 my $old_pkgnum = $old_cust_pkg ? $old_cust_pkg->pkgnum : '';
136 my $cust_pkg = $new->cust_svc->cust_pkg;
137 my $new_pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
138 my $new_cust_name = $cust_pkg ? $cust_pkg->cust_main->name : '';
139 $new_cust_name = shell_quote $new_cust_name;
140 #done setting variables for the command
142 $self->shellcommands_queue( $new->svcnum,
143 user => $self->option('user')||'root',
144 host => $self->machine,
145 command => eval(qq("$command")),
149 #a good idea to queue anything that could fail or take any time
150 sub shellcommands_queue {
151 my( $self, $svcnum ) = (shift, shift);
152 my $queue = new FS::queue {
154 'job' => "FS::part_export::phone_shellcommands::ssh_cmd",
156 $queue->insert( @_ );
159 sub ssh_cmd { #subroutine, not method
161 &Net::SSH::ssh_cmd( { @_ } );