fs.pl 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/perl
  2. use FreeSWITCH::Client;
  3. use Data::Dumper;
  4. use Term::ReadLine;
  5. my $password = "ClueCon";
  6. my $fs = init FreeSWITCH::Client {-password => $password} or die "Error $@";
  7. my $term = new Term::ReadLine "FreeSWITCH CLI";
  8. my $prompt = "FreeSWITCH>";
  9. my $OUT = $term->OUT .. \*STDOUT;
  10. my $pid;
  11. my $log = shift;
  12. $SIG{CHLD} = sub {$fs->disconnect(); die "done"};
  13. if ($log) {
  14. $pid = fork;
  15. if (!$pid) {
  16. my $fs2 = init FreeSWITCH::Client {-password => $password} or die "Error $@";
  17. $fs2->sendmsg({ 'command' => "log $log" });
  18. while (1) {
  19. my $reply = $fs2->readhash(undef);
  20. if ($reply->{socketerror}) {
  21. die "socket error";
  22. }
  23. if ($reply->{body}) {
  24. print $reply->{body};
  25. }
  26. }
  27. exit;
  28. }
  29. }
  30. while ( defined ($_ = $term->readline($prompt)) ) {
  31. if ($_) {
  32. if ($_ =~ /exit/) {
  33. last;
  34. }
  35. my $reply = $fs->command($_);
  36. if ($reply->{socketerror}) {
  37. $fs2->disconnect();
  38. die "socket error";
  39. }
  40. print "$reply\n";
  41. }
  42. $term->addhistory($_) if /\S/;
  43. }
  44. if ($pid) {
  45. kill 9 => $pid;
  46. }