forked from kohler/hotcrp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
closeport
executable file
·95 lines (85 loc) · 1.72 KB
/
closeport
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
$|=1;
my $SSH = "ssh -o StrictHostKeyChecking=no";
# Take paper ID, type of VM to stop and user ID
# and set up SSH tunnel and novnc proxy
my $usage="$0 port vncpass\n";
my %opt = ();
if ($#ARGV < 1)
{
print $usage;
print "DONE";
exit 1;
}
my $port = 10000 + $ARGV[0];
my $vncpass = $ARGV[1];
# Read info from conf/options.php
# to get orgName, dbUser, etc.
my $mytime = time();
print "Closing now time $mytime\n";
my $fh = new IO::File("conf/options.php");
while (<$fh>)
{
if ($_ =~ /\$Opt/ && $_ =~ /\=/)
{
my @arr = split /[\[\]\"\=\s+\;]/, $_;
if ($#arr > 3)
{
my $intake = 0;
for my $a (@arr)
{
if ($a !~ /[\[\]\"\=\s+]/ && $a =~ /[a-zA-Z0-9]/)
{
if ($a =~ /^\$Opt/)
{
$intake = 1;
next;
}
if ($intake =~ /^[0-9]$/ && $intake == 1)
{
$opt{$a} = 0;
$intake = $a;
}
else
{
$opt{$intake} = $a;
last;
}
}
}
}
}
}
close($fh);
my $dsn = "DBI:mysql:database=$opt{'dbName'}";
my $dbh = DBI->connect($dsn, $opt{'dbUser'}, $opt{'dbPassword'});
my $sth = $dbh->prepare(
'SELECT * from VMs where VNCpass = ?')
or die "prepare statement failed: $dbh->errstr()";
$sth->execute($vncpass) or die "execution failed: $dbh->errstr()";
if ($sth->rows() > 0)
{
my $output = `ps axuw |grep "localhost:$port"`;
my @line = split /\n/, $output;
for my $l (@line)
{
print "$l\n";
my @items = split /\s+/, $l;
my $owner = $items[0];
my $pid = $items[1];
if ($owner eq "merge")
{
my $cmd = "sudo -u merge kill -9 " . $items[1];
system($cmd);
print "Closed process $items[1]\n";
}
}
}
else
{
print "No such password $vncpass\n";
}
print "DONE";