python2报错NameError: name 'characters1' is not defined ``` from root_password import root_password ``` 库为 ``` # encoding: utf-8 import time ### 时间 import os import random import subprocess class root_password: Ap='QAZWSXEDCRFVTGBYHNUJMIKOLP' Bp='qazwsxedcrfvtgbyhnujmikolp' Cp='!@#$%^&*' Dp='0123456789' a=time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time())) characters = 'QAZWSXEDCRFVTGBYHNUJMIKLOP' Dpass = random.choice(characters) print(Dpass) characters = '!@$%^&*' #random_characters = random.choice(characters) #fpass = ''.join(random_characters) fpass = ''.join(random.choice(characters)) print(fpass) characters = '' characters = Bp + Ap #passfa1 = ''.join(random.choices(characters, k=2)) passfa1 = ''.join(random.choice(characters) for _ in range(2)) print(passfa1) characters = Bp + Dp #passfa2 = ''.join(random.choices(characters, k=2)) passfa2 = ''.join(random.choice(characters) for _ in range(2)) print(passfa2) characters = Bp + Cp #passfa3 = ''.join(random.choices(characters, k=3)) passfa3 = ''.join(random.choice(characters) for _ in range(3)) print(passfa3) characters = Ap + Dp #passfa4 = ''.join(random.choices(characters, k=2)) passfa4 = ''.join(random.choice(characters) for _ in range(2)) print(passfa4) characters = Ap + Cp #passfa5 = ''.join(random.choices(characters, k=2)) passfa5 = ''.join(random.choice(characters) for _ in range(2)) print(passfa5) characters = Dp + Cp #passfa6 = ''.join(random.choices(characters, k=3)) passfa6 = ''.join(random.choice(characters) for _ in range(3)) print(passfa6) NEWPASS=Dpass+passfa3+passfa6+fpass+passfa1+passfa4+passfa5 print(NEWPASS) #command = f'echo {NEWPASS} | sudo passwd --stdin root'#python3.6 command = 'echo '+NEWPASS+' | sudo passwd --stdin root' subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) #command1 = f'echo {a}--root >> /root/pass.log'#python3.6 #command2 = f'echo {NEWPASS} >> /root/pass.log'#python3.6 command1 = 'echo '+a+'--root >> /root/pass.log' command2 = 'echo '+NEWPASS+' >> /root/pass.log' subprocess.run(command1, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run(command2, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) ```
时间: 2024-04-13 14:28:13 浏览: 88
在你的代码中,`characters1` 是在 `root_password` 类内部定义的局部变量。如果你想从外部访问它,你需要将其定义为类的属性。你可以通过在 `characters1` 前面加上 `self.` 来实现。修改后的代码如下所示:
```python
# encoding: utf-8
import time
import os
import random
import subprocess
class root_password:
Ap = 'QAZWSXEDCRFVTGBYHNUJMIKOLP'
Bp = 'qazwsxedcrfvtgbyhnujmikolp'
Cp = '!@#$%^&*'
Dp = '0123456789'
def __init__(self):
self.characters = 'QAZWSXEDCRFVTGBYHNUJMIKLOP'
self.Dpass = random.choice(self.characters)
print(self.Dpass)
self.characters = '!@$%^&*'
self.fpass = ''.join(random.choice(self.characters))
print(self.fpass)
self.characters1 = self.Bp + self.Ap
self.passfa1 = ''.join(random.choice(self.characters1) for _ in range(2))
print(self.passfa1)
self.characters2 = self.Bp + self.Dp
self.passfa2 = ''.join(random.choice(self.characters2) for _ in range(2))
print(self.passfa2)
self.characters3 = self.Bp + self.Cp
self.passfa3 = ''.join(random.choice(self.characters3) for _ in range(3))
print(self.passfa3)
self.characters4 = self.Ap + self.Dp
self.passfa4 = ''.join(random.choice(self.characters4) for _ in range(2))
print(self.passfa4)
self.characters5 = self.Ap + self.Cp
self.passfa5 = ''.join(random.choice(self.characters5) for _ in range(2))
print(self.passfa5)
self.characters6 = self.Dp + self.Cp
self.passfa6 = ''.join(random.choice(self.characters6) for _ in range(3))
print(self.passfa6)
self.NEWPASS = self.Dpass + self.passfa3 + self.passfa6 + self.fpass + self.passfa1 + self.passfa4 + self.passfa5
print(self.NEWPASS)
command = 'echo ' + self.NEWPASS + ' | sudo passwd --stdin root'
subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
command1 = 'echo ' + time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time())) + '--root >> /root/pass.log'
command2 = 'echo ' + self.NEWPASS + ' >> /root/pass.log'
subprocess.run(command1, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.run(command2, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
```
然后,你可以通过以下方式导入 `root_password` 类并使用它:
```python
from root_password import root_password
rp = root_password()
```
这样就可以成功创建 `root_password` 类的实例,并执行其中的代码了。
阅读全文