1 package FS::part_export::broadband_shellcommands;
4 use vars qw(@ISA %info);
8 @ISA = qw(FS::part_export);
10 tie my %options, 'Tie::IxHash',
11 'user' => { label=>'Remote username', default=>'freeside' },
12 'insert' => { label=>'Insert command',
13 default=>'php provision.php --mac=$mac_addr --plan=$plan_id --account=active',
15 'delete' => { label=>'Delete command',
18 'suspend' => { label=>'Suspension command',
19 default=>'php provision.php --mac=$mac_addr --plan=$plan_id --account=suspend',
21 'unsuspend'=> { label=>'Unsuspension command',
24 'uppercase_mac' => { label => 'Force MACs to uppercase',
29 'svc' => 'svc_broadband',
30 'desc' => 'Run remote commands via SSH, for svc_broadband services',
31 'options' => \%options,
37 sub rebless { shift; }
41 $self->_export_command('insert', @_);
46 $self->_export_command('delete', @_);
51 $self->_export_command('suspend', @_);
54 sub _export_unsuspend {
56 $self->_export_command('unsuspend', @_);
60 my ( $self, $action, $svc_broadband) = (shift, shift, shift);
61 my $command = $self->option($action);
62 return '' if $command =~ /^\s*$/;
64 #set variable for the command
68 ${$_} = $svc_broadband->getfield($_) foreach $svc_broadband->fields;
71 if ( $self->option('uppercase_mac') ) {
72 $mac_addr = uc $mac_addr;
75 #done setting variables for the command
77 $self->shellcommands_queue( $svc_broadband->svcnum,
78 user => $self->option('user')||'root',
79 host => $self->machine,
80 command => eval(qq("$command")),
88 #a good idea to queue anything that could fail or take any time
89 sub shellcommands_queue {
90 my( $self, $svcnum ) = (shift, shift);
91 my $queue = new FS::queue {
93 'job' => "FS::part_export::broadband_shellcommands::ssh_cmd",
98 sub ssh_cmd { #subroutine, not method
101 my $ssh = Net::OpenSSH->new($opt->{'user'}.'@'.$opt->{'host'});
102 die "Couldn't establish SSH connection: ". $ssh->error if $ssh->error;
103 my ($output, $errput) = $ssh->capture2($opt->{'command'});
104 die "Error running SSH command: ". $ssh->error if $ssh->error;
105 die $errput if $errput;
106 die $output if $output;