-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathcommon.sh
executable file
·83 lines (76 loc) · 1.99 KB
/
common.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
#!/bin/bash
function wait_for_network_namespace {
# Wait that the namespace is ready.
COUNTER=0
while [ $COUNTER -lt 40 ]; do
if nsenter --preserve-credentials -U -n --target=$1 true; then
break
else
sleep 0.5
fi
let COUNTER=COUNTER+1
done
}
function wait_for_network_device {
# Wait that the device appears.
COUNTER=0
while [ $COUNTER -lt 40 ]; do
if nsenter --preserve-credentials -U -n --target=$1 ip addr show $2; then
break
else
sleep 0.5
fi
let COUNTER=COUNTER+1
done
}
function wait_process_exits {
COUNTER=0
while [ $COUNTER -lt 40 ]; do
if kill -0 $1; then
sleep 0.5
else
break
fi
let COUNTER=COUNTER+1
done
}
function wait_for_ping_connectivity {
COUNTER=0
while [ $COUNTER -lt 40 ]; do
if nsenter --preserve-credentials -U -n --target=$1 ping -c 1 -w 1 $2; then
break
else
sleep 0.5
fi
let COUNTER=COUNTER+1
done
}
function wait_for_connectivity {
COUNTER=0
while [ $COUNTER -lt 40 ]; do
if echo "wait_for_connectivity" | nsenter --preserve-credentials -U -n --target=$1 ncat -v $2 $3; then
break
else
sleep 0.5
fi
let COUNTER=COUNTER+1
done
}
function wait_for_file_content {
# Wait for a file to get the specified content.
COUNTER=0
while [ $COUNTER -lt 20 ]; do
if grep $1 $2; then
break
else
sleep 0.5
fi
let COUNTER=COUNTER+1
done
}
function expose_tcp() {
apisock=$1 hostport=$2 guestport=$3
json="{\"execute\": \"add_hostfwd\", \"arguments\": {\"proto\": \"tcp\", \"host_addr\": \"0.0.0.0\", \"host_port\": $hostport, \"guest_addr\": \"10.0.2.100\", \"guest_port\": $guestport}}"
echo -n $json | ncat -U $apisock
echo -n "{\"execute\": \"list_hostfwd\"}" | ncat -U $apisock
}