Skip to content

Instantly share code, notes, and snippets.

@rsff
Last active March 28, 2021 11:53
Show Gist options
  • Save rsff/723a20e6790ef103d0b0 to your computer and use it in GitHub Desktop.
Save rsff/723a20e6790ef103d0b0 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import paramiko
import cmd, sys, string, os
import argparse, threading
lock = threading.Lock()
def conn(host,user,cmd):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username = user)
print "[*]Connected to " + host +" sending "+ cmd
stdin, stdout, stderr = ssh.exec_command(cmd)
stdin.flush()
print "[*]Flushed"
with lock:
print stdout.readlines()
print stderr.readlines()
def main():
parser = argparse.ArgumentParser(description='iweb haproxy balancer push/pop.')
group = parser.add_mutually_exclusive_group()
group.add_argument('-off','--pop', help = 'pop host off balancer', action='store_true')
group.add_argument('-on','--push', help = 'push host on balancer', action='store_true')
parser.add_argument('-host','--lehost', help = 'host to put or take of the haproxy')
parser.add_argument('-user','--username', help = 'username to use to connect to users public key is assumed')
args = parser.parse_args()
hosts=[']
if "1" in args.lehost:
host_parsed="prod-iweb-as1"
elif "2" in args.lehost:
host_parsed="prod-iweb-as2"
else:
sys.exit(0)
sudo = "sudo sh -c "
socat = "| socat stdio /var/lib/haproxy/stats\""
cmd_on = sudo+"\"echo 'enable server tomcat/"+host_parsed+"'"+socat
cmd_off = sudo+"\"echo 'disable server tomcat/"+host_parsed+"'"+socat
threads = []
if args.pop:
print "inside pop"
for h in hosts:
t = threading.Thread(target=conn,args=(h,args.username,cmd_off))
t.start()
threads.append(t)
for t in threads:
t.join
elif args.push:
for h in hosts:
t = threading.Thread(target=conn,args=(h,args.username,cmd_on))
t.start()
threads.append(t)
for t in threads:
t.join
else:
print 'else'
sys.exit(0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment