import paramiko
vip = '192.168.42.250'
ds = '192.168.42.8'
rs1 = '192.168.42.9'
rs2 = '192.168.42.10'
ds_cmd = ''' #调度器配置
ipvsadm -C;
ipvsadm -A -t {vip}:80 -s wrr;
ipvsadm -a -t {vip}:80 -r {rs1}:80 -w 1 -g;
ipvasdm -a -t {vip}:80 -r {rs2}:80 -w 2 -g;
ifconfig ens33:0 {vip} broadcast {vip} netmask 255.255.255.255 up;
route add -host {vip} ens33:0'''.format(vip=vip,rs1=rs1,rs2=rs2) rs1_cmd = ''' #real_server1配置
ifconfig lo:0 {vip} broadcast {vip} netmask 255.255.255.255 up;
route add -host {vip} lo:0;
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore;
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce;
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore;
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce;
service httpd restart'''.format(vip=vip)
rs2_cmd = ''' #real_server2配置
ifconfig lo:0 {vip} broadcast {vip} netmask 255.255.255.255 up;
route add -host {vip} lo:0;
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore;
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce;
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore;
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce;
service httpd restart'''.format(vip=vip) ssh=paramiko.SSHClient() #连接远程主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) def direct_server():
ssh.connect('{ds}'.format(ds=ds),22,'root','',timeout=3)
stdin,stdout,stderr=ssh.exec_command(ds_cmd)
print(stdout.read().decode('utf-8'))
print(stderr.read().decode('utf-8'))
print('direct_server配置完成') def real_server1():
ssh.connect('{rs1}'.format(rs1=rs1),22,'root','',timeout=3)
stdin,stdout,stderr=ssh.exec_command(rs1_cmd)
print(stdout.read().decode('utf-8'))
print(stderr.read().decode('utf-8'))
print('real_server1配置完成') def real_server2():
ssh.connect('{rs2}'.format(rs2=rs2),22,'root','',timeout=3)
stdin,stdout,stderr=ssh.exec_command(rs2_cmd)
print(stdout.read().decode('utf-8'))
print(stderr.read().decode('utf-8'))
print('real_server2配置完成') def main(): #调用函数
direct_server()
real_server1()
real_server2()
ssh.close()
if __name__ == '__main__':
main()