-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerate_ssh_configs.php
More file actions
executable file
·103 lines (76 loc) · 2.94 KB
/
generate_ssh_configs.php
File metadata and controls
executable file
·103 lines (76 loc) · 2.94 KB
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
96
97
98
99
100
101
102
103
<?php
$zoneApiKeys = [
// 'username' => 'apikeyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
];
// directories used as SSH FS mountpoints are created here
$sshFsMount = getenv( 'HOME' ) . '/sshfs';
// template for .ssh/config entries
$sshConfigTemplate = '
Host {{HOST}}
HostName {{HOST}}
ForwardAgent yes
User {{USER}}
';
$sshFsAliasTemplate = '
alias mount_{{HOST}}="mkdir -p {{MOUNT}}/{{HOST}} && sshfs -o follow_symlinks -o volname={{HOST}} {{USER}}@{{HOST}}: {{MOUNT}}/{{HOST}}"
';
$sshFsFunctionTemplate = '
mount_{{HOST_SAFE}} () {
mkdir -p {{MOUNT}}/{{HOST}}
sshfs -o follow_symlinks {{USER}}@{{HOST}}: {{MOUNT}}/{{HOST}}
}
';
foreach ( $zoneApiKeys as $userName => $apiKey ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://api.zone.eu/v2/vserver' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_USERPWD, $userName . ":" . $apiKey );
$headers = [];
$headers[] = "Content-Type: application/json";
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
$result = curl_exec( $ch );
curl_close( $ch );
$vservers = json_decode( $result, true );
$sshConfig = '';
$sshFsFunctions = '';
$sshFsAliases = '';
foreach ( $vservers as $vserver ) {
$sshConfig .= template_replace( $sshConfigTemplate, $vserver );
$sshFsFunctions .= template_replace( $sshFsFunctionTemplate, $vserver );
$sshFsAliases .= template_replace( $sshFsAliasTemplate, $vserver );
}
// generate ssh config - can be included into main .ssh/config:
//
file_put_contents( getenv( 'HOME' ) . '/.ssh/' . $userName . '.config', $sshConfig );
// generate sshfs aliases - can be sourced into .bash_profile:
// for f in ~/*.alias; do source "$f"; done
// alias umount_sshfs="for f in ~/sshfs/*; do umount \"$f\"; done"
file_put_contents( getenv( 'HOME' ) . '/' . $userName . '.alias', $sshFsAliases );
// generate sshfs mount functions (good for cron and other cases where aliases don't work)
// $sshMountsFile = getenv( 'HOME' ) . '/' . $userName . '_mounts.sh';
// file_put_contents( $sshMountsFile, $sshFsFunctions );
// chmod( $sshMountsFile, 0744 );
}
echo 'All done!
Include generated configs by adding to your ~/.ssh/config:
Include *.config
To include generated aliases and allow autocomplete for ssh add to your .bash_profile:
# aliases generated from Zone API
for f in ~/*.alias; do source "$f"; done
alias umount_sshfs=\'for f in ~/sshfs/*; do umount "$f"; done\'
# *.config files generated from Zone API
complete -W "$(echo `cat ~/.ssh/config ~/.ssh/*.config | grep -E \'^Host\' | cut -d" " -f2- | tr " " "\n" | grep -v "*" | sort | uniq`;)" ssh
';
function template_replace( $template, $vserver ) {
global $sshFsMount;
$tags = [
'{{HOST}}' => $vserver['name'],
'{{HOST_SAFE}}' => str_replace( '.', '_', $vserver['name'] ),
'{{USER}}' => $vserver['group'],
'{{MOUNT}}' => $sshFsMount,
];
foreach ( $tags as $tag => $value ) {
$template = str_replace( $tag, $value, $template );
}
return $template;
}