Python之paramiko远程配置Linux安全基线

raspberry.py

import paramiko
import yaml


class Raspberry(object):

    def __init__(self):
        # 实例化SSHClient
        self.ssh_client = paramiko.SSHClient()
        # 自动添加策略,保存服务器的主机名和密钥信息,如果不添加,那么不再本地
        # know_hosts文件中记录的主机将无法连接 ,此方法必须放在connect方法的前面
        self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    @staticmethod
    def server_info():
        with open('host_info.yaml') as f:
            data = f.read()
            return yaml.load(data)

    def execute_cmd(self):
        for host_info in self.server_info():
            print(host_info)
            self.ssh_client.connect(
                hostname=host_info.get('hostname'),
                port=host_info.get('port'),
                username=host_info.get('username'),
                password=host_info.get('password')
            )
            for command in host_info.get('exec_command'):
                std_in, stdout, stderr = self.ssh_client.exec_command(command)
                print(stdout.read().decode('utf-8'))

    def close_connect(self):
        self.execute_cmd()
        self.ssh_client.close()


if __name__ == '__main__':
    r = Raspberry()
    r.close_connect()

host_info.yaml

- hostname: 192.168.201.3
  username: root
  password: raspberry
  port: 22
  exec_command:
  - ls -l
  - df -Th
  - du -sh *
  - ifconfig|grep -E -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
  - echo "hello world" > /root/paramiko.txt
  - sed -i "s/hello world/Hello World/g" /root/paramiko.txt
  - iptables -nvL
  - find  / -name  .netrc -exec rm -rf {} \;  && find  / -name  .rhosts -exec rm -rf {} \;
  - echo "TMOUT=180" >> /etc/profile
  - echo "auth required pam_tally.so deny=10 unlock_time=300" >> /etc/pam.d/system-auth
  - for PART in `grep -v ^# /etc/fstab | awk '($6 != "0") {print $2 }'`;do find $PART -nouser -o -nogroup -print 2>>/dev/null;done
  - echo "password  requisite  pam_cracklib.so  difok=3 minlen=8  ucredit=-1 lcredit=-1  dcredit=-1 ocredit=-1" >> /etc/pam.d/system-auth
  - sed -i 's/PermitRootLogin yes/#PermitRootLogin yes/g' /etc/ssh/sshd_config && service sshd restart
  - sed -i 's/PASS_MAX_DAYS/#PASS_MAX_DAYS/g'  /etc/login.defs
  - sed -i 's/PASS_MIN_DAYS/#PASS_MIN_DAYS/g'  /etc/login.defs
  - sed -i 's/PASS_WARN_AGE/#PASS_WARN_AGE/g'  /etc/login.defs
  - echo "PASS_MAX_DAYS 90" >> /etc/login.defs && echo "PASS_MIN_DAYS 2"  >> /etc/login.defs && echo "PASS_WARN_AGE 7" >> /etc/login.defs
  - echo "net.ipve.icmp_echo_ignore_broadcasts=1" >> /etc/sysctl.conf
  - rpm -e wget --nodeps
  - echo "PermitRootLogin no" >> /etc/ssh/sshd_config

- hostname: 192.168.244.135
  username: root
  password: raspberry
  port: 22
  exec_command:
  - ls -l
  - df -Th
  - du -sh *
  - ifconfig|grep -E -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
  - echo "hello world" > /root/paramiko.txt



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值