1 package FS::part_export::router;
3 =head1 FS::part_export::router
5 This export connects to a router and transmits commands via telnet or SSH.
6 It requires the following custom router fields:
10 =item admin_address - IP address (or hostname) to connect
12 =item admin_user - username for admin access
14 =item admin_password - password for admin access
18 The export itself needs the following options:
22 =item insert, replace, delete - command strings (to be interpolated)
24 =item Prompt - prompt string to expect from router after successful login
26 =item Timeout - time to wait for prompt string
30 (Prompt and Timeout are required only for telnet connections.)
34 use vars qw(@ISA @saltset);
35 use String::ShellQuote;
38 @ISA = qw(FS::part_export);
40 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
42 sub rebless { shift; }
46 $self->_export_command('insert', @_);
51 $self->_export_command('delete', @_);
56 $self->_export_command('suspend', @_);
59 sub _export_unsuspend {
61 $self->_export_command('unsuspend', @_);
65 my ( $self, $action, $svc_broadband) = (shift, shift, shift);
66 my $command = $self->option($action);
67 return '' if $command =~ /^\s*$/;
72 ${$_} = $svc_broadband->getfield($_) foreach $svc_broadband->fields;
75 my $router = $svc_broadband->addr_block->router;
77 $r{$_} = $router->getfield($_) foreach $router->virtual_fields;
79 #warn eval(qq("$command"));
81 warn "admin_address: '$r{admin_address}'";
83 if ($r{admin_address} ne '') {
84 $self->router_queue( $svc_broadband->svcnum, $self->option('protocol'),
85 user => $r{admin_user},
86 password => $r{admin_password},
87 host => $r{admin_address},
88 Timeout => $self->option('Timeout'),
89 Prompt => $self->option('Prompt'),
90 command => eval(qq("$command")),
99 # We don't handle the case of a svc_broadband moving between routers.
100 # If you want to do that, reprovision the service.
102 my($self, $new, $old ) = (shift, shift, shift);
103 my $command = $self->option('replace');
107 ${"old_$_"} = $old->getfield($_) foreach $old->fields;
108 ${"new_$_"} = $new->getfield($_) foreach $new->fields;
111 my $router = $new->addr_block->router;
113 $r{$_} = $router->getfield($_) foreach $router->virtual_fields;
115 if ($r{admin_address} ne '') {
116 $self->router_queue( $new->svcnum, $self->option('protocol'),
117 user => $r{admin_user},
118 password => $r{admin_password},
119 host => $r{admin_address},
120 Timeout => $self->option('Timeout'),
121 Prompt => $self->option('Prompt'),
122 command => eval(qq("$command")),
129 #a good idea to queue anything that could fail or take any time
132 my( $self, $svcnum, $protocol ) = (shift, shift, shift);
133 my $queue = new FS::queue {
136 $queue->job ("FS::part_export::router::".$protocol."_cmd");
137 $queue->insert( @_ );
140 sub ssh_cmd { #subroutine, not method
142 &Net::SSH::ssh_cmd( { @_ } );
152 my $t = new Net::Telnet (Timeout => $arg{Timeout},
153 Prompt => $arg{Prompt});
154 $t->open($arg{host});
155 $t->login($arg{user}, $arg{password});
156 my @error = $t->cmd($arg{command});
157 die @error if (grep /^ERROR/, @error);
160 #sub router_insert { #subroutine, not method
162 #sub router_replace { #subroutine, not method
164 #sub router_delete { #subroutine, not method