| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | use Net::DVBStreamer::Client; | ||
| 4 | use Term::ReadLine; | ||
| 5 | use Getopt::Std; | ||
| 6 | use vars qw/ %opt /; | ||
| 7 | use Data::Dumper; | ||
| 8 | use strict; | ||
| 9 | |||
| 10 | $|=1; | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | # Parse options from the command line | ||
| 15 | getopts( "h:a:f:u:p:", \%opt ) or usage(); | ||
| 16 | |||
| 17 | |||
| 18 | # Defaults for settings | ||
| 19 | my $host = $opt{h} || 'localhost'; | ||
| 20 | my $adaptor = $opt{a} || 0; | ||
| 21 | my $username = $opt{u} || 'dvbstreamer'; | ||
| 22 | my $password = $opt{p} || undef; | ||
| 23 | my $file = $opt{f} || undef; | ||
| 24 | |||
| 25 | |||
| 26 | # Connect to remote server | ||
| 27 | my $dvbs = new Net::DVBStreamer::Client( $host, $adaptor ); | ||
| 28 | |||
| 29 | if (defined $username and defined $password) { | ||
| 30 | if (!$dvbs->authenticate( $username, $password )) { | ||
| 31 | die "Failed to authenticate with server: ".$dvbs->response()."\n"; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | ## Process commands from a file? | ||
| 37 | if (defined $file) { | ||
| 38 | |||
| 39 | open( FILE, $file ) or die "Failed to open input file: $!"; | ||
| 40 | |||
| 41 | while( <FILE> ) { | ||
| 42 | chomp(); | ||
| 43 | last if(my_send_command( $dvbs, $_ )); | ||
| 44 | } | ||
| 45 | |||
| 46 | close( FILE ); | ||
| 47 | |||
| 48 | |||
| 49 | ## Process command from command-line? | ||
| 50 | } elsif ( @ARGV ) { | ||
| 51 | |||
| 52 | # Send command to server | ||
| 53 | my_send_command( $dvbs, @ARGV ); | ||
| 54 | |||
| 55 | |||
| 56 | ## Process commands from STDIN | ||
| 57 | } else { | ||
| 58 | |||
| 59 | # Create new terminal | ||
| 60 | my $term = new Term::ReadLine 'PerlDVBCtrl'; | ||
| 61 | |||
| 62 | print "Connected to $host, adaptor $adaptor (server version ".$dvbs->server_version().").\n"; | ||
| 63 | |||
| 64 | # Read commands from the console | ||
| 65 | while ( defined (my $cmd = $term->readline('PerlDVBCtrl>')) ) { | ||
| 66 | |||
| 67 | # Trim whitespace | ||
| 68 | chomp($cmd); | ||
| 69 | |||
| 70 | # End of session? | ||
| 71 | if ($cmd eq 'quit' or $cmd eq 'logout') { | ||
| 72 | $dvbs->send_command( 'logout' ); | ||
| 73 | last; | ||
| 74 | } | ||
| 75 | |||
| 76 | # Send command to server | ||
| 77 | my_send_command( $dvbs, $cmd ); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | sub my_send_command { | ||
| 83 | my $dvbs = shift; | ||
| 84 | |||
| 85 | my (@result) = $dvbs->send_command( @_ ); | ||
| 86 | if (defined $result[0]) { | ||
| 87 | |||
| 88 | # Success | ||
| 89 | print join( "\n", @result )."\n"; | ||
| 90 | return 0; | ||
| 91 | |||
| 92 | } else { | ||
| 93 | |||
| 94 | # Error | ||
| 95 | print "Error: ".$dvbs->response()."\n"; | ||
| 96 | return $dvbs->errno(); | ||
| 97 | |||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | sub usage { | ||
| 103 | |||
| 104 | print "Usage: $0 [options] [command]\n\n"; | ||
| 105 | print " -h host Host to connect to (default localhost).\n"; | ||
| 106 | print " -a adaptor Adaptor on host to connect to (default 0).\n"; | ||
| 107 | print " -f file File to read list of commands from.\n"; | ||
| 108 | print " -u username Username to authenticate with (default 'dvbstreamer').\n"; | ||
| 109 | print " -p password Password to authenticate with.\n"; | ||
| 110 | print "\n"; | ||
| 111 | |||
| 112 | exit(-1); | ||
| 113 | } | ||
| 114 | |||
| 115 |