From 7bcdd02c9ec619dd450e6f0e171442e94127e835 Mon Sep 17 00:00:00 2001 From: Jin-Guk Kwon Date: Wed, 11 Dec 2019 05:11:48 +0100 Subject: [PATCH 1/4] Add new file for getting VM List --- vm_list | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 vm_list diff --git a/vm_list b/vm_list new file mode 100644 index 0000000..7d77eee --- /dev/null +++ b/vm_list @@ -0,0 +1,19 @@ +#!/bin/bash -e +#option $1 is ldap password +#option $2 is ou + + +uid_list=( $(ldapsearch -x -H ldaps://ldap1.ungleich.ch:636 -D cn=manager,dc=ungleich,dc=ch -w $1 -b "ou=$2,dc=ungleich,dc=ch" | grep uid: | awk '{print $2}') ) + +for ((i=0; i<${#uid_list[@]}; i++)) do + list_email[$i]=$(ldapsearch -x -H ldaps://ldap1.ungleich.ch:636 -D cn=manager,dc=ungleich,dc=ch -w $1 -b "uid=${uid_list[$i]},ou=customer,dc=ungleich,dc=ch" | grep mail: | awk '{print $2}' ) + list_vmid=() + list_vmid=( $(onevm list | grep ${list_email[$i]} | grep runn | awk '{print $1}' ) ) + for ((j=0; j<${#list_vmid[@]}; j++)) do + temp=$(onevm show ${list_vmid[$j]} | grep PORT) + temp1="${temp#*\"}" + port="${temp1%%\"*}" + host=$(onevm show ${list_vmid[$j]} | grep HOST | grep ungleich | awk '{print $3}') + echo ${uid_list[$i]} ${list_vmid[$j]} $port $host >> ~/vm_vnc_list + done +done \ No newline at end of file From cdd7779e2ce2654a089e20ad09deab5e68d0f2ab Mon Sep 17 00:00:00 2001 From: Jin-Guk Kwon Date: Wed, 11 Dec 2019 05:12:52 +0100 Subject: [PATCH 2/4] Update vm_list --- vm_list | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm_list b/vm_list index 7d77eee..95f64be 100644 --- a/vm_list +++ b/vm_list @@ -6,7 +6,7 @@ uid_list=( $(ldapsearch -x -H ldaps://ldap1.ungleich.ch:636 -D cn=manager,dc=ungleich,dc=ch -w $1 -b "ou=$2,dc=ungleich,dc=ch" | grep uid: | awk '{print $2}') ) for ((i=0; i<${#uid_list[@]}; i++)) do - list_email[$i]=$(ldapsearch -x -H ldaps://ldap1.ungleich.ch:636 -D cn=manager,dc=ungleich,dc=ch -w $1 -b "uid=${uid_list[$i]},ou=customer,dc=ungleich,dc=ch" | grep mail: | awk '{print $2}' ) + list_email[$i]=$(ldapsearch -x -H ldaps://ldap1.ungleich.ch:636 -D cn=manager,dc=ungleich,dc=ch -w $1 -b "uid=${uid_list[$i]},ou=$2,dc=ungleich,dc=ch" | grep mail: | awk '{print $2}' ) list_vmid=() list_vmid=( $(onevm list | grep ${list_email[$i]} | grep runn | awk '{print $1}' ) ) for ((j=0; j<${#list_vmid[@]}; j++)) do From c7f02bce5c9d0307a7e1d07006d9a87c490b5bb4 Mon Sep 17 00:00:00 2001 From: Jin-Guk Kwon Date: Wed, 11 Dec 2019 05:23:28 +0100 Subject: [PATCH 3/4] Add new file --- create-guacamole-session-ldap-DB | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 create-guacamole-session-ldap-DB diff --git a/create-guacamole-session-ldap-DB b/create-guacamole-session-ldap-DB new file mode 100644 index 0000000..25819e2 --- /dev/null +++ b/create-guacamole-session-ldap-DB @@ -0,0 +1,39 @@ +#!/bin/bash +#option $1 is vm_list file name +#option $2 id DB location +#option $3 is DB user +#option $4 is DB name + +host='localhost' + +user_arr=( $(cat $1 | awk '{print $1}' )) +vmid_arr=( $(cat $1 | awk '{print $2}' )) +port_arr=( $(cat $1 | awk '{print $3}' )) +place_arr=( $(cat $1 | awk '{print $4}' )) + +for ((i=0; i<${#user_arr[@]}; i++)) do + #create user + psql -h $2 -U $3 -d $4 -tAc "INSERT INTO guacamole_entity (name, type) VALUES ('${user_arr[i]}','USER');" + en_id=$(psql -h $2 -U $3 -d $4 -tAc "SELECT entity_id FROM guacamole_entity WHERE name = '${user_arr[i]}';") + psql -h $2 -U $3 -d $4 -tAc "INSERT INTO guacamole_user(entity_id, password_hash, password_date) VALUES ('$en_id', '\x74657374', now());" + + #create connection + cn=${user_arr[i]}${vmid_arr[i]} + echo $cn + if [ 0 -eq $(psql -h $2 -U $3 -d $4 -tAc "SELECT connection_id FROM guacamole_connection WHERE connection_name = '$cn';" | wc -l) ]; then + psql -h $2 -U $3 -d $4 -tAc "INSERT INTO guacamole_connection (connection_name, protocol) VALUES ('$cn', 'vnc');" + cn_id=$(psql -h $2 -U $3 -d $4 -tAc "SELECT MAX(connection_id) FROM guacamole_connection WHERE connection_name = '$cn' AND parent_id IS NULL;") + + psql -h $2 -U $3 -d $4 -tAc "INSERT INTO guacamole_connection_parameter VALUES ('$cn_id','hostname','$host');" + psql -h $2 -U $3 -d $4 -tAc "INSERT INTO guacamole_connection_parameter VALUES ('$cn_id','port','${port_arr[i]}');" + + #connection permission + psql -h $2 -U $3 -d $4 -tAc "INSERT INTO guacamole_connection_permission(entity_id, connection_id, permission) VALUES ('$en_id', '$cn_id', 'READ');" + + else + cn_id=$(psql -h $2 -U $3 -d $4 -tAc "SELECT MAX(connection_id) FROM guacamole_connection WHERE connection_name = '$cn' AND parent_id IS NULL;") + psql -h $2 -U $3 -d $4 -tAc "UPDATE guacamole_connection_parameter SET parameter_value='$host' where connection_id='$cn_id' and parameter_name='hostname';" + psql -h $2 -U $3 -d $4 -tAc "UPDATE guacamole_connection_parameter SET parameter_value='${port_arr[i]}' where connection_id='$cn_id' and parameter_name='port';" + fi + +done \ No newline at end of file From 36cc9b3c50ace54519c62bae43f32c2deeda4f1d Mon Sep 17 00:00:00 2001 From: Jin-Guk Kwon Date: Wed, 11 Dec 2019 05:29:47 +0100 Subject: [PATCH 4/4] Add new file --- create-guacamole-session-ldap-file | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 create-guacamole-session-ldap-file diff --git a/create-guacamole-session-ldap-file b/create-guacamole-session-ldap-file new file mode 100644 index 0000000..c11b4bc --- /dev/null +++ b/create-guacamole-session-ldap-file @@ -0,0 +1,38 @@ +#!/bin/bash +#option $1 is vm_list file name +#option $2 is DB name +#this script should be run on guacamole server + + +host='localhost' +user_arr=( $(cat $1 | awk '{print $1}' )) +vmid_arr=( $(cat $1 | awk '{print $2}' )) +port_arr=( $(cat $1 | awk '{print $3}' )) +place_arr=( $(cat $1 | awk '{print $4}' )) + +for ((i=0; i<${#user_arr[@]}; i++)) do + #create user + su - postgres -c "psql postgres -d $2 -tAc \"INSERT INTO guacamole_entity (name, type) VALUES ('${user_arr[i]}','USER');\"" + en_id=$(su - postgres -c "psql postgres -d $2 -tAc \"SELECT entity_id FROM guacamole_entity WHERE name = '${user_arr[i]}';\"") + su - postgres -c "psql postgres -d $2 -tAc \"INSERT INTO guacamole_user(entity_id, password_hash, password_date) VALUES ('$en_id', '\x74657374', now());\"" + + #create connection + cn=${user_arr[i]}${vmid_arr[i]} + + if [ 0 -eq $(su - postgres -c "psql postgres -d $2 -tAc \"SELECT connection_id FROM guacamole_connection WHERE connection_name = '$cn';\"" | wc -l) ]; then + su - postgres -c "psql postgres -d $2 -tAc \"INSERT INTO guacamole_connection (connection_name, protocol) VALUES ('$cn', 'vnc');\"" + cn_id=$(su - postgres -c "psql postgres -d $2 -tAc \"SELECT MAX(connection_id) FROM guacamole_connection WHERE connection_name = '$cn' AND parent_id IS NULL;\"") + + su - postgres -c "psql postgres -d $2 -tAc \"INSERT INTO guacamole_connection_parameter VALUES ('$cn_id','hostname','$host');\"" + su - postgres -c "psql postgres -d $2 -tAc \"INSERT INTO guacamole_connection_parameter VALUES ('$cn_id','port','${port_arr[i]}');\"" + + #connection permission + su - postgres -c "psql postgres -d $2 -tAc \"INSERT INTO guacamole_connection_permission(entity_id, connection_id, permission) VALUES ('$en_id', '$cn_id', 'READ');\"" + + else + cn_id=$(su - postgres -c "psql postgres -d $2 -tAc \"SELECT MAX(connection_id) FROM guacamole_connection WHERE connection_name = '$cn' AND parent_id IS NULL;\"") + su - postgres -c "psql postgres -d $2 -tAc \"UPDATE guacamole_connection_parameter SET parameter_value='$host' where connection_id='$cn_id' and parameter_name='hostname';\"" + su - postgres -c "psql postgres -d $2 -tAc \"UPDATE guacamole_connection_parameter SET parameter_value='${port_arr[i]}' where connection_id='$cn_id' and parameter_name='port';\"" + fi + +done \ No newline at end of file