forked from blockchainvn/hyperledger-fabric-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpectfn.sh
executable file
·111 lines (102 loc) · 2.25 KB
/
expectfn.sh
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
104
105
106
107
108
109
110
111
#!/bin/bash
build(){
echo "Try to install expect:"
cd expect/tcl8.5.15
ARCH=`uname -s | grep Darwin`
if [ "$ARCH" != "Darwin" ]; then
cd unix
else
cd macosx
fi
# build tcl, move to /usr/local
./configure --prefix=/usr/local
make
make install
cd ../../
# build expect, move to /usr/local
cd expect5.45
./configure --prefix=/usr/local
make
make install
cd ../
}
if [ ! `command -v expect` ];then
build
fi
CONFIG=$1
first=${CONFIG%%@*}
last=${CONFIG##*@}
user=${first%%:*}
keypasswd=${first##*:}
server=${last%%:*}
base_dir=${last##*:}
# SSH_KEY=/Users/thanhtu/Downloads/azure_cert_node
SSH_KEY=${keypasswd%%,*}
if [[ $keypasswd =~ , ]]; then
passwd=${keypasswd##*,}
fi
shift
if [[ $1 == "sync" ]];then
path=${2:-$PWD}
echo "Sync folder to server"
if [[ ! -z $passwd ]];then
expect << EOF
spawn rsync -e "ssh -i $SSH_KEY -o StrictHostKeyChecking=no" \
-chavP --stats --exclude ".git" \
--exclude "**/node_modules/" \
--exclude "**/vendor/" \
--exclude "bin" \
--exclude "admin/hfc-key-store" \
--exclude "setupCluster/crypto-config" \
--exclude "setupCluster/channel-artifacts" \
$path/ $user@$last
expect "Enter passphrase"
send "$passwd\r"
expect eof
EOF
else
rsync -e "ssh -i $SSH_KEY -o StrictHostKeyChecking=no" \
-chavP --stats --exclude ".git" \
--exclude "**/node_modules/" \
--exclude "**/vendor/" \
--exclude "bin" \
--exclude "admin/hfc-key-store" \
--exclude "setupCluster/crypto-config" \
--exclude "setupCluster/channel-artifacts" \
$path/ $user@$last
fi
elif [[ $1 == "build" ]]; then
#statements
build
elif [[ $1 == "--" ]];then
shift
# parsing params
QUERY=
while [[ ! -z $1 ]];do
if [[ $1 =~ [\ ] ]]; then
QUERY="$QUERY '$1'"
else
QUERY="$QUERY $1"
fi
shift
done
# run expect
if [[ ! -z $passwd ]];then
expect << EOF
spawn ssh -i $SSH_KEY -t $user@$server -o StrictHostKeyChecking=no "sudo su <<\EOF
cd $base_dir
./fn.sh $QUERY
EOF"
expect "Enter passphrase"
send "$passwd\r"
expect eof
EOF
else
ssh -i $SSH_KEY -t $user@$server -o StrictHostKeyChecking=no "sudo su <<\EOF
cd $base_dir
./fn.sh $QUERY
EOF"
fi
else
echo "Unknow command $1" 1>&2
fi