python批量修改远程VM配置

vm list:

servername1    172.28.0.xx
servername2    172.28.0.xx
servername3    172.28.0.xx

import paramiko		#paramiko lib for remote ssh connection						
								
cmd_linefeed = "sudo sh -c 'echo >> /etc/ssh/sshd_config'"   #linefeed 文件末尾追加换行								
cmd_modify_sshd = "echo 'Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com'|sudo tee -a /etc/ssh/sshd_config"								
cmd_restart_sshd = "sudo systemctl restart sshd"								
								
with open('autolist', 'r') as file_to_read:					# prodlist is the file name 			
    while True:								
        line = file_to_read.readline()								
        if not line:								
            break								
        line = line.strip('\n')								
        print(line)								
        try:								
            trans = paramiko.Transport(line, 22)					# Try to connect via port 22			
        except:								
            print("connection error!")					# in case connection failed			
            continue								
        else:								
            try:								
                trans.connect(username="username", password="passw0rd")			# try password "passw0rd"
            except:								
                trans.close()								
                trans = paramiko.Transport(line, 22)								
                trans.connect(username="username", password="passw0rd")			# try password ""passw0rd""
            finally:								
                ssh = paramiko.SSHClient()								
                ssh._transport = trans								
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())			# ignore if it's asked to input "yes"
                stdin, stdout, stderr = ssh.exec_command(cmd_linefeed, get_pty=True, timeout=20)								
                print(stdout.read().decode())								
                print(stderr.read().decode())								
                stdin, stdout, stderr = ssh.exec_command(cmd_modify_sshd, get_pty=True, timeout=20)								
                print(stdout.read().decode())								
                print(stderr.read().decode())								
                stdin, stdout, stderr = ssh.exec_command(cmd_restart_sshd, get_pty=True, timeout=20)								
                print(stdout.read().decode())								
                print(stderr.read().decode())								
                ssh.close()								

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值