Archive

Archive for December, 2013

Send commands using expect scripts

December 29, 2013 Leave a comment

We use tomcat as our application server. Our security policy doesnt allow us to install the manager app. The only way I know of deploying without the manager app is to upload the WAR files to the host, shutdown the tomcat instance, remove the WAR from webapps and work folders, copy the WAR file to the webapps folder and restart tomcat.

This is a script that will automate the deployment process from your local machine.

#!/usr/bin/expect -f

set timeout -1

set VERSION "1.0.0-SNAPSHOT"

set MY_WAR "$WORK_DIR/target/mywar.war"

set USER "XYZ"
set PASSWORD "XYZ"
set SU_USER "XYZ"
set SU_PASSWORD "XYZ"


set HOST {host1 host2 host3 host4}

set MY_WAR_NAME "mywar"

set MY_WAR_DEPLOY_NAME "mywar.war"
	
set CMD ""
set TOMCAT_PATHS {/servers/tomcat_8020/bin /servers/tomcat_8030/bin /servers/tomcat_8040/bin}
foreach TOMCAT_PATH $TOMCAT_PATHS {
	append CMD "cd $TOMCAT_PATH;sh +x shutdown.sh;sleep 3;rm -rf ../webapps/$MY_WAR_NAME*;rm -rf ../work/*;cp /tmp/$MY_WAR_DEPLOY_NAME ../webapps/;sh +x startup.sh ;sleep 3;"
}

foreach SERVER_PATH $HOST {
	spawn ssh -l $USER -o PubkeyAuthentication=no $SERVER_PATH
	expect {
		"password" { send "$PASSWORD\r"; }
	}
	
	expect {
			"bash" { send "su - $SU_USER\r";}
			"denied" { exit; }
		}
		
		expect {
			"assword" { send "$SU_PASSWORD\r"; }
			"$SU_USER" { send "ls -lart;sleep 3\r"; }
		}
	
		expect {
			"$SU_USER" { send "$CMD\r"; }
		}
	
	
	interact timeout 30 return;
}

Categories: Tech stuff Tags: , , , ,

SSH using expect scripts

December 29, 2013 Leave a comment
#!/usr/bin/expect -f

set timeout 30

spawn ssh -l USERNAME -o PubkeyAuthentication=no HOSTNAME
expect {
"password" { send "PASSWORD\r"; }}
expect {
"bash" {
send "sudo su - SU_USER\r";
}
"denied" { exit; }}
expect {
"password" { send "PASSWORD\r"; }}
interact;