1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#!/usr/bin/perl
use warnings;
use strict;
use feature qw(say);
use JSON;
use DateTime;
use DateTime::Format::ISO8601;
use Net::OpenSSH;
sub check_env {
# Log into remote server
my $host = $ENV{"TW_HOOK_REMIND_REMOTE_HOST"}
or die "Cannot get TW_HOOK_REMIND_REMOTE_HOST environment variable";
my $user = $ENV{"TW_HOOK_REMIND_REMOTE_USER"}
or die "Cannot get TW_HOOK_REMIND_REMOTE_USER environment variable";
return ($host, $user);
}
sub get_connection {
my $host = shift;
my $port = shift;
my $user = shift;
# use correct port
if ( $host =~ m/.*\.xyz$/ ) { $port = 2222 }
say "Trying to establish connection at $host:$port ...";
my $ssh = Net::OpenSSH->new( $host, user => $user, port => $port );
$ssh->error and die "Couldn't establish SSH connection: " . $ssh->error;
return $ssh;
}
sub check_remind_file_exists {
my $ssh = shift;
my $host = shift;
my $remfile = shift;
#
# Check for presence or remind file
if ( $ssh->test("ls $remfile") != 1 ) {
die "Cannot find $remfile on $host.";
}
# If it is there, back it up
$ssh->system("cp $remfile $remfile.bak")
or die "Cannot create a back-up of remind file.";
}
sub append_to_remfile {
my $ssh = shift;
my $host = shift;
my $remfile = shift;
my $remline = shift;
# Append the Remind formatted line to the original remind file
$ssh->system( { stdin_data => $remline }, "cat >> $remfile" )
or die "Cannot append text: " . $ssh->error;
# $ssh->system("echo >> $remline $remfile")
# or die "Cannot append text: " . $ssh->error;
# Get content of remind file
my @out_file = $ssh->capture("cat $remfile");
print qq/
Contents of $remfile on $host is now:\n/, @out_file;
}
my $remfile = "~/.reminders/work.rem";
my ($host, $user) = check_env();
my $ssh = get_connection($host, 2222, $user);
check_remind_file_exists($ssh, $host, $remfile );
append_to_remfile($ssh, $host, $remfile, 'REM 16 Sept 2022 AT 11:11 MSG TEST2 %1');
exit;
|