1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Ssh to server |
5
|
|
|
* |
6
|
|
|
* @author Alireza Josheghani <[email protected]> |
7
|
|
|
* @since 29 Sep 2018 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Josh\Console\Commands; |
11
|
|
|
|
12
|
|
|
use Josh\Console\ConsoleStyle as Style; |
13
|
|
|
use Symfony\Component\Console\Helper\Table; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
class ServerSSHCommand extends Command |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* configure command |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function configure() |
29
|
|
|
{ |
30
|
|
|
$this->setName('server:ssh') |
31
|
|
|
->setDescription('ssh to the server'); |
32
|
|
|
|
33
|
|
|
$this->addArgument('server', InputArgument::REQUIRED, "Server id or name"); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* execute command |
38
|
|
|
* |
39
|
|
|
* @param InputInterface $input |
40
|
|
|
* @param OutputInterface $output |
41
|
|
|
*/ |
42
|
|
|
protected function execute(InputInterface $input , OutputInterface $output) |
43
|
|
|
{ |
44
|
|
|
$command = new Style($input, $output); |
45
|
|
|
|
46
|
|
|
$file = $file = $this->getHomeDir() . "/.Josh/servers.json"; |
47
|
|
|
|
48
|
|
|
$servers = json_decode(file_get_contents($file), true); |
49
|
|
|
|
50
|
|
|
$serverIdOrName = $input->getArgument('server'); |
51
|
|
|
|
52
|
|
|
if (count($servers) == 0) { |
53
|
|
|
|
54
|
|
|
$command->line("No server added to the list. use [ server:add ] to add one."); |
55
|
|
|
} else { |
56
|
|
|
|
57
|
|
|
$currentServer = []; |
58
|
|
|
|
59
|
|
|
foreach ($servers as $server){ |
60
|
|
|
|
61
|
|
|
if ($server[0] == $serverIdOrName){ |
62
|
|
|
$currentServer = $server; |
63
|
|
|
} else if ($server[1] == $serverIdOrName){ |
64
|
|
|
$currentServer = $server; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$command->info("Connecting to [$currentServer[1]]..."); |
69
|
|
|
|
70
|
|
|
system("ssh root@$currentServer[2]"); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** Get home directory |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function getHomeDir() |
79
|
|
|
{ |
80
|
|
|
if(empty($_SERVER['HOME'])){ |
81
|
|
|
return posix_getpwuid(posix_getuid()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $_SERVER['HOME']; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|