TCL programming

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/expect -f
# TCL script. Beware whitespace matter!

# To set multi-line comments do the following

# set comment {
#      your multi-line comment

# }

# ###################################################

# ALL functions Start
# ###################################################

if {[llength $argv] == 0} {
  send_user "Usage: scriptname FUNCTION_NAME\n"
  send_user "Example script.exp updateSomething\n"
  exit 1
}
set functionName [lindex $argv 0]

proc simpleTest {} {
    set aProductionServers  {your_server_4 your_server_5 your_server_6 your_server_1 your_server_2 your_server_3}
    for {set i 0} {$i < [llength $aProductionServers]} {incr i}  {
        send_user "Processing ... '[lindex $aProductionServers $i]'\n"
        set timeout 60
        spawn ssh root@[lindex $aProductionServers $i]

        expect "*assword: "
        send "\r"

        send "hostname\r"
        expect "[lindex $aProductionServers $i]"

        send "date\r"
        expect "2015"
        send "exit\r"
    }
}
# simpleTest

proc printPass {} {
    send_user "\n\[PASS\] \n"
}

proc printFail {} {
    send_user "\n\[FAIL\] \n"
    exit 1
}

#Untar Lucene indexes

proc UntarLuceneindexes {} {
    set aLuceneServers  {your_server_1 your_server_2 your_server_3}
    set sCurrentLuceneTar "index2_20151009.tar.bz2"
    for {set i 0} {$i < [llength $aLuceneServers]} {incr i}  {
        send_user "\n\n"
        send_user "================================================\n"
        send_user "Processing ... '[lindex $aLuceneServers $i]'\n"
        send_user "================================================\n"
        set timeout 60
        spawn ssh root@[lindex $aLuceneServers $i]

        expect "*assword: "
        send "YOUR_SERVER_PASSWORD\r"
       
        send "hostname\r"
        expect "[lindex $aLuceneServers $i]"
       
        send "su - path\r"
        send "pwd\r"
        expect "/home/path"
        send "id\r"
        expect "path"
       
        send "ls $sCurrentLuceneTar|wc -l\r"
        expect "1"
       
        send "tar -xjvf $sCurrentLuceneTar &\r"
        send "sleep 2\r"
        send "ps aux|grep index2|grep tar|wc -l\r"
        send "disown %1"
        expect "1"
       
        send_user "Done processing ... '[lindex $aLuceneServers $i]'"
    }
}


proc updateDBRef {} {
    set apathServers  {your_server_6}
    set newDB "some_db_20150925"
    for {set i 0} {$i < [llength $apathServers]} {incr i}  {
        send_user "\n\n"
        send_user "================================================\n"
        send_user "Processing ... '[lindex $apathServers $i]'\n"
        send_user "================================================\n"
        set timeout 60
        spawn ssh root@[lindex $apathServers $i]

        expect "*assword: "
        send "YOUR_SERVER_PASSWORD\r"
       
        send "hostname\r"
        expect "[lindex $apathServers $i]"
       
        send "su - path\r"
        send "pwd\r"
        expect "/home/path"
        send "id\r"
        expect "path"
       
        send "cat /home/path/path-current/SomeConfig.properties |grep -v 'com.somecompany.setting.server.db.pathdb2.url=' > /home/path/path-current/SomeConfig.properties.new"
        send "echo 'com.somecompany.setting.server.db.pathdb2.url=jdbc:postgresql://127.0.0.1:5432/$newDB' >> /home/path/path-current/SomeConfig.properties.new"
       
        #  ps aux | grep jar | grep 8806

        #  kill xxxxxx
       
       
        send_user "Done processing ... '[lindex $apathServers $i]'"
    }
}

set aLuceneServers  {your_server_1 your_server_2 your_server_3}
proc stopLuceneServices {} {
    global aLuceneServers
    for {set i 0} {$i < [llength $aLuceneServers]} {incr i}  {
        send_user "\n\n"
        send_user "================================================\n"
        send_user "Processing ... '[lindex $aLuceneServers $i]'\n"
        send_user "================================================\n"
        set timeout 60
        spawn ssh root@[lindex $aLuceneServers $i]

        expect "*assword: "
        send "YOUR_SERVER_PASSWORD\r"
       
        send "hostname\r"
        expect "[lindex $aLuceneServers $i]"
       
        send "service lucene stop\r"
        expect {
            "stopped PID" { printPass }
            "lucene is not running" { printFail }
            "lucene is not running x" { printPass }
            timeout { exit 1 }
            }
       
        send_user "Done processing ... '[lindex $aLuceneServers $i]' \n"
    }
}

proc startLuceneServices {} {
    global aLuceneServers
   
    for {set i 0} {$i < [llength $aLuceneServers]} {incr i}  {
        send_user "\n\n"
        send_user "================================================\n"
        send_user "Processing ... '[lindex $aLuceneServers $i]'\n"
        send_user "================================================\n"
        set timeout 60
        spawn ssh root@[lindex $aLuceneServers $i]

        expect "*assword: "
        send "YOUR_SERVER_PASSWORD\r"
       
        send "hostname\r"
        expect "[lindex $aLuceneServers $i]"
       
        send "service lucene start\r"
        expect "started PID"
       
        send_user "Done processing ... '[lindex $aLuceneServers $i]'"
    }
}

# ###################################################

# ALL functions End
# ###################################################

set timeout 60
log_file -noappend expect.log ;# Default to append to a file. To disable append use: log_file -noappend


# ###################################################

# Calling functionName from, which supplied from command line
# ###################################################

$functionName
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


eight + = 13