TCL programming


#!/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

Leave a Reply

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


four + 8 =