import paramiko vip='192.168.83.6' # 虚拟IP # direct_server_information ds_info={ 'ip':'192.168.83.5', 'user':'root', 'pwd':'root' } # real_server_information real_server_ip=['192.168.83.3','192.168.83.4'] real_server_user=['root','root'] real_server_pwd=['root','root'] ds_cmd = ''' yum install ipvsadm; ipvsadm -C; ipvsadm -A -t {vip}:80 -s wrr; ipvsadm -a -t {vip}:80 -r {rs1}:80 -w 1 -g; ipvsadm -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} dev ens33:0; service httpd restart; systemctl stop firewalld; setenforce 0 '''.format(vip=vip, rs1=real_server_ip[0], rs2=real_server_ip[1]) server_cmd = ''' ifconfig lo:0 {vip} broadcast {vip} netmask 255.255.255.255 up; route add -host {vip} dev lo:0; echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore; echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce; echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore; echo "2">/proc/sys/net/ipv4/conf/all/arp_announce; service httpd restart; systemctl stop firewalld; setenforce 0 '''.format(vip=vip) #配置调度器direct_server def direct_server(): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=ds_info['ip'],port=22,username=ds_info['user'],password=ds_info['pwd']) stdin, stdout, stderr = ssh.exec_command(ds_cmd) print(stderr.read().decode('utf-8')) print(stdout.read().decode('utf-8')) ssh.close() print('dicret_server配置完成!') #配置real_server def real_server(): k=0 while k<len(real_server_ip): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=real_server_ip[k],port=22,username=real_server_user[k],password=real_server_pwd[k]) stdin, stdout, stderr = ssh.exec_command(server_cmd) print(stderr.read().decode('utf-8')) print(stdout.read().decode('utf-8')) ssh.close() k+=1 print('real_server配置完成!') def main(): direct_server() real_server() if __name__ == '__main__': main()