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