写出批量查询100台linux系统中所有用户密码是否过期以及账户是否锁定的python脚本,查询账户是否锁定用 pam_tally2 --user
时间: 2023-12-10 22:38:29 浏览: 124
以下是一个简单的Python脚本,用于批量查询100台Linux系统中所有用户密码是否过期以及账户是否锁定:
```python
import paramiko
# 定义主机列表和ssh连接的用户名和密码
host_list = ['192.168.1.1', '192.168.1.2', '192.168.1.3', ..., '192.168.1.100']
username = 'your_username'
password = 'your_password'
# 建立ssh连接
def ssh_connect(ip, username, password):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=username, password=password)
return ssh
for host in host_list:
ssh = ssh_connect(host, username, password)
# 查询所有用户密码是否过期
stdin, stdout, stderr = ssh.exec_command("grep '^\\+\\w*\\:\\*\\:' /etc/shadow | cut -d ':' -f 1")
accounts = stdout.readlines()
for account in accounts:
account = account.strip()
stdin, stdout, stderr = ssh.exec_command("chage -l " + account + " | grep 'Password expires' | awk '{print $4}'")
password_expire = stdout.read().strip()
if password_expire == "never":
print("Account {0} on {1} does not have a password expiration date.".format(account, host))
else:
print("Account {0} on {1} has a password expiration date of {2}.".format(account, host, password_expire))
# 查询所有用户账户是否锁定
stdin, stdout, stderr = ssh.exec_command("grep '^\\+\\w*\\:\\*\\:' /etc/shadow | cut -d ':' -f 1")
accounts = stdout.readlines()
for account in accounts:
account = account.strip()
stdin, stdout, stderr = ssh.exec_command("pam_tally2 --user=" + account + " | grep 'locked' | awk '{print $6}'")
if stdout.read().strip() == "locked":
print("Account {0} on {1} is locked.".format(account, host))
else:
print("Account {0} on {1} is not locked.".format(account, host))
ssh.close()
```
注意:此脚本假设您已经在所有主机上安装了paramiko和pam_tally2,否则需要先安装。此外,此脚本仅用于演示目的,实际使用时请根据需要进行修改和测试。
阅读全文