Browse Source

shell script

master
Alexander Avery 3 weeks ago
parent
commit
1343fe30b6
  1. 10
      Makefile
  2. 12
      README.md
  3. 140
      popcorn.c
  4. 25
      popcorn.sh

10
Makefile

@ -1,10 +0,0 @@
CC = gcc
CFLAGS = -g -Wall
LIBS = -lssh
TARGET = popcorn
BUILDDIR = build
$(TARGET): $(TARGET).c
$(CC) $(CFLAGS) -o $(BUILDDIR)/$(TARGET) $(TARGET).c $(LIBS)

12
README.md

@ -1,12 +1,4 @@
# popcorn # popcorn
Simple program for finding and reading remote videos with libssh Simple shell program for finding and watching remote videos over SFTP.
Use this if you must, but please prefer 9p.
Inspired by the shell script:
```bash
HOST=192.168.1.2
USER=pi
PATH=$((ssh $USER@$HOST find /var/www/public/media -type f -name "*mp4" | rev | cut -d'/' -f1 | rev | sort) | dmenu -l 30 | xargs -I% ssh $USER@$HOST find /var/www/public/media -name '"%"')
/usr/bin/vlc "sftp://$USER@$HOST$PATH"
```

140
popcorn.c

@ -1,140 +0,0 @@
#include <libssh/libssh.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int verify_host(ssh_session session) {
enum ssh_known_hosts_e state;
state = ssh_session_is_known_server(session);
// only handling known hosts for now
// will implement other responses later.
switch (state) {
case SSH_KNOWN_HOSTS_OK:
break;
default:
return -1;
}
return 0;
}
int get_movies(ssh_session session, char **buffer, size_t size) {
ssh_channel channel;
int rc;
char read_buffer[256];
int nbytes;
size_t len = 0;
char *temp;
channel = ssh_channel_new(session);
if (channel == NULL)
return SSH_ERROR;
rc = ssh_channel_open_session(channel);
if (rc != SSH_OK) {
fprintf(stderr, "Failed to open channel");
ssh_channel_free(channel);
return rc;
}
rc = ssh_channel_request_exec(
channel, "find /var/www/public/media -type f -name \"*mp4\"");
if (rc != SSH_OK) {
fprintf(stderr, "Failed to request exec");
ssh_channel_close(channel);
ssh_channel_free(channel);
return rc;
}
nbytes = ssh_channel_read(channel, read_buffer, sizeof(read_buffer), 0);
while (nbytes > 0) {
if ((len += nbytes) > size) {
size = len;
temp = realloc(*buffer, len);
if (!temp) {
fprintf(stderr, "Failed to reallocate char buffer");
ssh_channel_close(channel);
ssh_channel_free(channel);
return -1;
}
*buffer = temp;
}
memcpy(*buffer + len - nbytes, read_buffer, nbytes);
nbytes = ssh_channel_read(channel, read_buffer, sizeof(read_buffer), 0);
}
if (nbytes < 0) {
fprintf(stderr, "Failed to read bytes from channel");
ssh_channel_close(channel);
ssh_channel_free(channel);
return SSH_ERROR;
}
ssh_channel_send_eof(channel);
ssh_channel_close(channel);
ssh_channel_free(channel);
return SSH_OK;
}
int main(int argc, char *argv[]) {
char *host = "localhost";
char *user = getlogin();
int opt;
int rc;
while ((opt = getopt(argc, argv, "h:u:")) != -1)
switch (opt) {
case 'h':
host = optarg;
break;
case 'u':
user = optarg;
break;
case '?':
printf("Unknown option\n");
break;
}
ssh_session session = ssh_new();
if (session == NULL) {
fprintf(stderr, "Failed to get new ssh_session");
exit(-1);
}
ssh_options_set(session, SSH_OPTIONS_HOST, host);
ssh_options_set(session, SSH_OPTIONS_USER, user);
if (ssh_connect(session) != SSH_OK) {
fprintf(stderr, "Unable to connect to host: %s\n", ssh_get_error(session));
exit(-1);
}
if (verify_host(session) != 0) {
fprintf(stderr, "Attempted to connect to unknown host\n");
ssh_disconnect(session);
ssh_free(session);
exit(-1);
}
rc = ssh_userauth_publickey_auto(session, NULL, NULL);
if (rc == SSH_AUTH_ERROR) {
fprintf(stderr, "Authentication failed: %s\n", ssh_get_error(session));
exit(-1);
}
int size = 16;
char *b = malloc(size);
memset(b, 0, size);
if (get_movies(session, &b, size) != SSH_OK) {
fprintf(stderr, "Unable to list movies: %s\n", ssh_get_error(session));
exit(-1);
}
ssh_disconnect(session);
ssh_free(session);
}

25
popcorn.sh

@ -0,0 +1,25 @@
#!/bin/sh
usage="$0 [-u <user> -h <host> -p <path> -m <menu> -v <videoplayer>]"
host=localhost
path="~"
menu='dmenu-wl'
video='mpv'
while getopts ':u:h:m:p:v' opt
do
case $opt in
u) USER=$OPTARG;;
h) host=$OPTARG;;
p) path=$OPTARG;;
m) menu=$OPTARG;;
v) video=$OPTARG;;
?) echo "error: invalid option: $usage"
exit 1;;
esac
done
find="cd $path; find ~+ -type f -name \"*.mp4\""
user=$USER
ssh $user@$host $find | $menu | xargs -I% $video "sftp://$user@$host%"
Loading…
Cancel
Save