1 package FS::part_export::phone_shellcommands;
4 use vars qw(@ISA %info);
6 use String::ShellQuote;
10 @ISA = qw(FS::part_export);
13 #- modify command (get something from freepbx for changing PINs)
14 #- suspension/unsuspension
16 tie my %options, 'Tie::IxHash',
17 'user' => { label=>'Remote username', default=>'root', },
18 'useradd' => { label=>'Insert command', },
19 'userdel' => { label=>'Delete command', },
20 'usermod' => { label=>'Modify command', },
21 'suspend' => { label=>'Suspension command', },
22 'unsuspend' => { label=>'Unsuspension command', },
23 'mac_insert' => { label=>'Device MAC address insert command', },
24 'mac_delete' => { label=>'Device MAC address delete command', },
28 'svc' => [qw( svc_phone part_device )],
29 'desc' => 'Run remote commands via SSH, for phone numbers',
30 'options' => \%options,
32 Run remote commands via SSH, for phone numbers. You will need to
33 <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.9:Documentation:Administration:SSH_Keys">setup SSH for unattended operation</a>.
34 <BR><BR>Use these buttons for some useful presets:
37 <INPUT TYPE="button" VALUE="FreePBX (build_exten CLI module needed)" onClick='
38 this.form.user.value = "root";
39 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";
40 this.form.userdel.value = "build_exten.php --delete --exten $phonenum && /usr/share/asterisk/bin/module_admin reload";
41 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";
42 this.form.suspend.value = "";
43 this.form.unsuspend.value = "";
44 '> (Important note: Reduce freeside-queued "max_kids" to 1 when using FreePBX integration)
47 The following variables are available for interpolation (prefixed with new_ or
48 old_ for replace operations):
50 <LI><code>$countrycode</code> - Country code
51 <LI><code>$phonenum</code> - Phone number
52 <LI><code>$sip_password</code> - SIP secret (quoted for the shell)
53 <LI><code>$pin</code> - Personal identification number
54 <LI><code>$cust_name</code> - Customer name (quoted for the shell)
55 <LI><code>$pkgnum</code> - Internal package number
56 <LI><code>$custnum</code> - Internal customer number
57 <LI><code>$phone_name</code> - Phone name (quoted for the shell)
58 <LI><code>$mac_addr</code> - MAC address (Device MAC address insert and delete commands only)
59 <LI><code>$devicename</code> - Device type (Device type insert and delete commands only)
64 sub rebless { shift; }
68 $self->_export_command('useradd', @_);
73 $self->_export_command('userdel', @_);
78 $self->_export_command('suspend', @_);
81 sub _export_unsuspend {
83 $self->_export_command('unsuspend', @_);
86 sub export_device_insert {
87 my( $self, $svc_phone, $phone_device ) = @_;
88 $self->_export_command('mac_insert', $svc_phone,
89 mac_addr => $phone_device->mac_addr,
90 devicename => $phone_device->part_device->devicename,
94 sub export_device_delete {
95 my( $self, $svc_phone, $phone_device ) = @_;
96 $self->_export_command('mac_delete', $svc_phone,
97 mac_addr => $phone_device->mac_addr,
98 devicename => $phone_device->part_device->devicename,
102 sub _export_command {
103 my ( $self, $action, $svc_phone, %addl_vars) = @_;
104 my $command = $self->option($action);
105 return '' if $command =~ /^\s*$/;
107 if ( $FS::svc_Common::noexport_hack ) {
108 carp "_export_command($action) suppressed by noexport_hack"
109 if $self->option('debug');
113 #set variable for the command
117 ${$_} = $svc_phone->getfield($_) foreach $svc_phone->fields;
118 ${$_} = $addl_vars{$_} foreach keys %addl_vars;
120 my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
121 my $pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
122 my $custnum = $cust_pkg ? $cust_pkg->custnum : '';
123 my $cust_name = $cust_pkg ? $cust_pkg->cust_main->name : '';
124 $cust_name = shell_quote $cust_name;
125 my $sip_password = shell_quote $svc_phone->sip_password;
126 my $phone_name = shell_quote $svc_phone->phone_name;
127 #done setting variables for the command
129 $self->shellcommands_queue( $svc_phone->svcnum,
130 user => $self->option('user')||'root',
131 host => $self->machine,
132 command => eval(qq("$command")),
136 sub _export_replace {
137 my($self, $new, $old ) = (shift, shift, shift);
138 my $command = $self->option('usermod');
140 #set variable for the command
144 ${"old_$_"} = $old->getfield($_) foreach $old->fields;
145 ${"new_$_"} = $new->getfield($_) foreach $new->fields;
148 my $old_cust_pkg = $old->cust_svc->cust_pkg;
149 my $old_pkgnum = $old_cust_pkg ? $old_cust_pkg->pkgnum : '';
150 my $old_custnum = $old_cust_pkg ? $old_cust_pkg->custnum : '';
151 my $cust_pkg = $new->cust_svc->cust_pkg;
152 my $new_pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
153 my $new_custnum = $new_cust_pkg ? $new_cust_pkg->custnum : '';
154 my $new_cust_name = $cust_pkg ? $cust_pkg->cust_main->name : '';
155 $new_cust_name = shell_quote $new_cust_name;
156 #done setting variables for the command
158 $self->shellcommands_queue( $new->svcnum,
159 user => $self->option('user')||'root',
160 host => $self->machine,
161 command => eval(qq("$command")),
165 #a good idea to queue anything that could fail or take any time
166 sub shellcommands_queue {
167 my( $self, $svcnum ) = (shift, shift);
168 my $queue = new FS::queue {
170 'job' => "FS::part_export::phone_shellcommands::ssh_cmd",
172 $queue->insert( @_ );
175 sub ssh_cmd { #subroutine, not method
177 &Net::SSH::ssh_cmd( { @_ } );