import paramiko

#让132成为主网关
def change1():
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # 连接服务器
    ssh.connect(hostname='192.168.111.138', port=22, username='xumingyang', password='185102')
    # 执行命令 关闭主网关 这样的话
    stdin, stdout, stderr = ssh.exec_command("echo '185102' | sudo -S systemctl stop keepalived.service")

    # 等待命令执行完成
    stdout.channel.recv_exit_status()
    # 获取命令输出
    keepalived_state = stdout.read().decode('utf-8')
    print(keepalived_state)

    # 关闭连接
    ssh.close()

def change2():
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # 连接服务器
    ssh.connect(hostname='192.168.111.138', port=22, username='xumingyang', password='185102')
    # 执行命令 关闭主网关 这样的话
    stdin, stdout, stderr = ssh.exec_command("echo '185102' | sudo -S systemctl start keepalived.service")
    # 等待命令执行完成
    stdout.channel.recv_exit_status()
    # 获取命令输出
    keepalived_state = stdout.read().decode('utf-8')
    print(keepalived_state)

    # 关闭连接
    ssh.close()