Skip to content

Commit 1216718

Browse files
committed
Bash script for generate dynamic profiles for iTerm2
0 parents  commit 1216718

File tree

7 files changed

+574
-0
lines changed

7 files changed

+574
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea/
2+
/local/
3+

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Bash script for generate dynamic profiles for iTerm2
2+
3+
`actual version: 1.0.0`
4+
5+
## Command
6+
```
7+
profiles.sh -l <hosts_file_name> [-s <settings_file_name>] [-t <template_file_name>] [-o <output_file_name>]
8+
```
9+
* -l host's list file, required
10+
* -s settings file, optional, default use `settings.cfg`
11+
* -t template file, optional, default use `template.cfg`
12+
* -o output file, optional, default use `<name_of_host_file>.json`
13+
14+
## List file
15+
List file should have 2 or 3 parameters where:
16+
* domain name
17+
* ip
18+
* display name into profiles(if not exist will take first part from `domain name`)
19+
```
20+
domain.com 127.0.0.1 ProfileName
21+
```
22+
23+
## Template file patterns
24+
* `__SERVER_COMMAND__` - used for full command
25+
* `__SERVER_GROUP__` - used for `tag`
26+
* `__SERVER_NAME__` - used for `display name` and `tag`
27+
* `__SERVER_INITIAL_TEXT__` - used for initial text

font.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FONT_BOLD='\033[1m'
2+
FONT_BLINK='\033[5m'
3+
COLOR_HEADER='\033[95m'
4+
COLOR_OKBLUE='\033[94m'
5+
COLOR_OKGREEN='\033[92m'
6+
COLOR_WARNING='\033[93m'
7+
COLOR_FAIL='\033[91m'
8+
COLOR_END='\033[0m'

hosts.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mydomain.com 127.0.0.1 ProfileName

profiles.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/bin/bash
2+
3+
source font.cfg
4+
5+
echo -e $COLOR_HEADER$FONT_BOLD"Profiles generator for iTerm2"$COLOR_END
6+
profiles_message=$FONT_BOLD"profiles.sh -l <hosts_file_name> [-s <settings_file_name>] [-t <template_file_name>] [-o <output_file_name>]"$COLOR_END
7+
8+
while getopts ":l:s:t:o:" opt; do
9+
case $opt in
10+
l)
11+
l=$OPTARG
12+
echo -e $COLOR_OKGREEN"Use list from $l"$COLOR_END
13+
;;
14+
s)
15+
s=$OPTARG
16+
echo -e $COLOR_OKGREEN"Use settings from $s"$COLOR_END
17+
;;
18+
t)
19+
t=$OPTARG
20+
echo -e $COLOR_OKGREEN"Use template from $t"$COLOR_END
21+
;;
22+
o)
23+
o=$OPTARG
24+
echo -e $COLOR_OKGREEN"For output use $o"$COLOR_END
25+
;;
26+
\?)
27+
echo -e "$profiles_message"
28+
echo -e $COLOR_FAIL"Invalid option: -$OPTARG"$COLOR_END
29+
exit 1
30+
;;
31+
:)
32+
echo -e "$profiles_message"
33+
echo -e $COLOR_FAIL"Option -$OPTARG requires an argument."$COLOR_END
34+
exit 1
35+
;;
36+
esac
37+
done
38+
if [ -z $l ]; then
39+
echo -e "$profiles_message"
40+
echo -e $COLOR_FAIL"Option -l required."$COLOR_END
41+
exit 1
42+
fi
43+
if [ -z $s ]; then
44+
s="settings.cfg"
45+
fi
46+
if [ -z $t ]; then
47+
t="template.cfg"
48+
fi
49+
if [ -z $o ]; then
50+
o="$l.json"
51+
fi
52+
echo -e $COLOR_OKGREEN"For output use $o"$COLOR_END
53+
echo ""
54+
55+
56+
source "$s"
57+
source "$t"
58+
59+
ALL_TEMPLATES=""
60+
first=0
61+
62+
while IFS='\n' read -r line || [[ -n "$line" ]]; do
63+
if [ -n "$line" ]; then
64+
IFS=' ' list=($line)
65+
if [ -n "${list[2]}" ]; then
66+
profile_name=${list[2]}
67+
else
68+
profile_name=$(echo ${list[0]} | sed -E 's/^([a-z0-9]*)\.(.*)/\1/')
69+
fi
70+
profile_ip=$(echo ${list[1]} | sed -E 's/^([0-9]*)\.([0-9]*)\.([0-9]*)\.([0-9]*)(.*)/\1.\2.\3.\4/')
71+
if [[ $first == 1 ]]; then
72+
ALL_TEMPLATES="$ALL_TEMPLATES ,"
73+
fi
74+
profile_command="$command_prefix$profile_ip$command_postfix"
75+
ALL_TEMPLATES=$ALL_TEMPLATES" "$(echo $TEMPLATE | sed -E "s/__SERVER_COMMAND__/$profile_command/g" | sed -E "s/__SERVER_GROUP__/$profile_group/g" | sed -E "s/__SERVER_NAME__/$profile_name/g" | sed -E "s/__SERVER_INITIAL_TEXT__/$initial_text/g")
76+
first=1
77+
fi
78+
done < "$l"
79+
80+
echo "{
81+
\"Profiles\": [
82+
$ALL_TEMPLATES
83+
]
84+
}" > "$o"

settings.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
profile_group='test'
2+
initial_text='screen -S dw-screen'
3+
command_prefix='ssh -A dmitrij.waskowski@'
4+
command_postfix=''

0 commit comments

Comments
 (0)