Related
Below is code that correctly outputs a random password based on the sequence, using multiple for loops. I want to loop the function 10 times to output 10 passwords, but I can't seem to figure out what variable to loop in this case.
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M','N','O','P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
num_letters= random.randint(3, 6)
num_numbers = random.randint(1, 4)
num_symbols = (10 - num_letters - num_numbers)
print(f'Your requested {num_letters} letters, {num_numbers} numbers, and {num_symbols} symbols:')
password_list = []
for num in range(num_letters):
`letter = random.choice(letters)`
`password_list.append(letter)`
for num in range(num_numbers):
`number = random.choice(numbers)`
`password_list.append(number)`
for num in range(num_symbols):
`symbol = random.choice(symbols)`
`password_list.append(symbol)`
random.shuffle(password_list)
password = ''
for char in password_list:
`password = password + char`
print(password)
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M','N','O','P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
def generator():
print("Welcome to the PyPassword Generator!")
num_letters= random.randint(3, 6)
num_numbers = random.randint(1, 4)
num_symbols = (10 - num_letters - num_numbers)
print(f'Your requested {num_letters} letters, {num_numbers} numbers, and {num_symbols} symbols:')
password_list = []
for num in range(num_letters):
letter = random.choice(letters)
password_list.append(letter)
for num in range(num_numbers):
number = random.choice(numbers)
password_list.append(number)
for num in range(num_symbols):
symbol = random.choice(symbols)
password_list.append(symbol)
random.shuffle(password_list)
password = ''
for char in password_list:
password = password + char
print(password)
return password
print([generator() for i in range(10)])
Just put this generator in a function!
As such I have a main function to run the program and ask how many times you would like to iterate the password generator function.
import random
def main():
print("Welcome to the PyPassword Generator!")
val = input("Enter number of passwords you want to generate: ")
itterate = int(val)
#print("Generated passwords: ")
for i in range(itterate):
passwordGenerator()
def passwordGenerator():
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M','N','O','P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
num_letters= random.randint(3, 6)
num_numbers = random.randint(1, 4)
num_symbols = ((10 - num_letters - num_numbers) + 1)
print(f'Your requested {num_letters} letters, {num_numbers} numbers, and {num_symbols} symbols:')
password_list = []
for num in range(num_letters):
letter = random.choice(letters)
password_list.append(letter)
for num in range(num_numbers):
number = random.choice(numbers)
password_list.append(number)
for num in range(num_symbols):
symbol = random.choice(symbols)
password_list.append(symbol)
random.shuffle(password_list)
password = ''
for char in password_list:
password = password + char
print(password)
if __name__ == "__main__":
main()
Sample output:
Welcome to the PyPassword Generator!
Enter number of passwords you want to generate: 3
Your requested 3 letters, 3 numbers, and 5 symbols:
(7I3$7&%)vm
Your requested 4 letters, 1 numbers, and 6 symbols:
&*PV#6u#N&)
Your requested 3 letters, 2 numbers, and 6 symbols:
8#*)8!!lt)h
I put all your loops in the function to make your code easy to read and undrestand
no_passwords = 10 i make new variable to specify the number of passwords you want
for _ in range(no_passwords): generat_password() make loop run in range of the variable we made called no_passwords and each time the code will call the function generat_password() to generate a new password
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M','N','O','P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
def generat_password():
password_list = []
for num in range(num_letters):
letter = random.choice(letters)
password_list.append(letter)
for num in range(num_numbers):
number = random.choice(numbers)
password_list.append(number)
for num in range(num_symbols):
symbol = random.choice(symbols)
password_list.append(symbol)
random.shuffle(password_list)
password = ''
for char in password_list:
password = password + char
print(password)
print("Welcome to the PyPassword Generator!")
num_letters= random.randint(3, 6)
num_numbers = random.randint(1, 4)
num_symbols = (10 - num_letters - num_numbers)
no_passwords = 10
print(f'Your requested {num_letters} letters, {num_numbers} numbers, and {num_symbols} symbols:')
for _ in range(no_passwords):
generat_password()
I'm currently trying to write a script so that I can display network information on a graph. Unfortunately, I'm not having too much success in what I need to get done. Here is the output I have to work with:
Temperature Sensors:
Unit Sensor Description Temp (C) State Max_Temp (C)
1 1 PHY 32 Normal 40
1 2 REAR 38 Normal 45
1 3 CPU 40 Normal 48
Temperature Status: Normal
Fan Duty Level: 46%
Fans:
Unit Fan Description Type Speed Duty level State
1 1 FAN-1 Fixed 6617 46% Operational
1 2 FAN-2 Fixed 6482 46% Operational
With that being said, I'm only concerned about the Fan speed (not percentage) and the 3 fields (PHY, CPU, and REAR) with their corresponding Temp values (not the max value, just the current value). What I would like my output to look like is the following:
{'PHY': '32', 'REAR': '38', 'CPU': '40', 'FAN-1': 6617, 'FAN-2': 6482}
The reason behind the key value pairs is that I utilize a tool called Logicmonitor that can take external scripting from network equipment and draw the values on a graph that we can track historically.
The closest I've gotten my output to is the following:
['\n', '1', '1', 'P', 'H', 'Y', '3', '2', 'N', 'o', 'r', 'm', 'a', 'l', '4', '0', '\n', '1', '2', 'R', 'E', 'A', 'R', '3', '8', 'N', 'o', 'r', 'm', 'a', 'l', '4', '5', '\n', '1', '3', 'C', 'P', 'U', '4', '0', 'N', 'o', 'r', 'm', 'a', 'l', '4', '8', '\n', '\n', 'T', 'e', 'm', 'p', 'e', 'r', 'a', 't', 'u', 'r', 'e', 'S', 't', 'a', 't', 'u', 's', ':', 'N', 'o', 'r', 'm', 'a', 'l', '\n', '\n', 'F', 'a', 'n', 'D', 'u', 't', 'y', 'L', 'e', 'v', 'e', 'l', ':', '4', '6', '%', '\n', '\n', 'F', 'a', 'n', 's', ':', '\n', 'U', 'n', 'i', 't', 'F', 'a', 'n', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', 'T', 'y', 'p', 'e', 'S', 'p', 'e', 'e', 'd', 'D', 'u', 't', 'y', 'l', 'e', 'v', 'e', 'l', 'S', 't', 'a', 't', 'e', '\n', '1', '1', 'F', 'A', 'N', '-', '1', 'F', 'i', 'x', 'e', 'd', '6', '6', '5', '0', '4', '6', '%', 'O', 'p', 'e', 'r', 'a', 't', 'i', 'o', 'n', 'a', 'l', '\n', '1', '2', 'F', 'A', 'N', '-', '2', 'F', 'i', 'x', 'e', 'd', '6', '4', '7', '4', '4', '6', '%', 'O', 'p', 'e', 'r', 'a', 't', 'i', 'o', 'n', 'a', 'l', '\n', '\n']
At this point, I know I'm in the right general direction, but I'm unable to figure out where my end destination needs to be. Here is my current script:
#!/usr/bin/python3
import netmiko
connection =netmiko.ConnectHandler(ip="1.2.3.4", port="22", device_type="ubiquiti_edgeswitch", username="admin", password="password", secret="password")
#defining a list to be used
output = {}
#assigning the list to the output
output = connection.send_command("show environment | exclude -- begin PHY")
#stripping all leading and trailing spaces from a list of strings
output_new = [item.replace(' ','') for item in output]
#removing any unnecessary spaces from list
while('' in output_new):
output_new.remove('')
print(output_new)
connection.disconnect()
Under the assumption that the "arguments" remain their position in relation to each key (e.g. the value for PHY always comes directly after the keyword PHY), you could use a dictionary that maps each key to what the index offset of the value from the keyword is in the output, and then construct your dictionary from that.
So, for example, in the case of the string
1 1 PHY 32 Normal 40 1 2 REAR 38 Normal 45 1 3 CPU 40 Normal 48, the "value" of the keyword PHY has an offset of 1. This means that if we split the string by spaces, we know that the value of PHY will be found at the index of PHY + 1.
This idea is implemented in the following code-snippet:
offsets = {
"PHY": 1,
"REAR": 1,
"CPU": 1,
"FAN-1": 2,
"FAN-2": 2,
}
data = {}
words = output.split()
for key in offsets:
index = words.index(key) + offsets[key]
data[key] = words[index]
Adding this logic to your existing code, we would get:
#!/usr/bin/python3
import netmiko
connection =netmiko.ConnectHandler(ip="1.2.3.4", port="22", device_type="ubiquiti_edgeswitch", username="admin", password="password", secret="password")
#defining a list to be used
output = {}
#assigning the list to the output
output = connection.send_command("show environment | exclude -- begin PHY")
words = output.split()
# Defining the offsets of the "values" for each keyword
offsets = {
"PHY": 1,
"REAR": 1,
"CPU": 1,
"FAN-1": 2,
"FAN-2": 2,
}
# The processed data
data = {}
# Loop over each keyword and extract its value
for key in offsets:
index = words.index(key) + offsets[key]
data[key] = words[index]
print(data)
connection.disconnect()
This is not an answer to your question, but the recommended way to solve the problem:
There are parsers that can read structured data from network show commands. Netmiko currently supports textFSM, ttp and genie. You can see how to use them in netmiko in the examples section: netmiko examples
So i want it to pick a random digit from a list 2 times nd then mix it together in order to get a password. I wanted the ready_password to print a list and it works but, it also prints [] and ''. So i decided to make it a tuple but i don't know how to mix a tuple. This is the error that I'm getting:
TypeError: sample() missing 1 required positional argument: 'k'
code-
import random
lower_case = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
upper_case = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
punctuation = ['!', '#', '#', '$', '%', '^', '&', '*', '(']
lower1 = random.choice(lower_case)
lower2 = random.choice(lower_case)
upper1 = random.choice(upper_case)
upper2 = random.choice(upper_case)
number1 = random.choice(numbers)
number2 = random.choice(numbers)
punctuation1 = random.choice(punctuation)
punctuation2 = random.choice(punctuation)
password_not_ready = (lower1 + lower2 + upper1 + upper2 + number1 + number2 + punctuation1 +
punctuation2)
ready_password = random.sample(password_not_ready)
print(ready_password)
You might want to use random.shuffle() to mix up the characters instead of having them in order:
from random import sample as sp
from random import shuffle as sh
lower_case = 'abcdefghijklmnopqrstuvwxyz'
upper_case = lower_case.upper()
numbers = '1234567890'
punctuation = '!##$%^&*('
ready_password = sp(lower_case,2)+sp(upper_case,2)+sp(punctuation,2)+sp(numbers,2)
sh(ready_password)
print(''.join(ready_password))
Output:
c3D#0hV%
sample() takes 2 arguments, the second being the length of the list to return, however I think what you meant to do was use shuffle(), to reorder all the randomly picked characters (sample will not shuffle the list). Change this line
ready_password = random.sample(password_not_ready)
to
ready_password = random.shuffle(password_not_ready, len(password_not_ready))
See the documentation for shuffle and for sample for more information.
K=8 for 8 digit password
import random
lower_case = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
upper_case = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
punctuation = ['!', '#', '#', '$', '%', '^', '&', '*', '(']
lower1 = random.choice(lower_case)
lower2 = random.choice(lower_case)
upper1 = random.choice(upper_case)
upper2 = random.choice(upper_case)
number1 = random.choice(numbers)
number2 = random.choice(numbers)
punctuation1 = random.choice(punctuation)
punctuation2 = random.choice(punctuation)
password_not_ready = (lower1 + lower2 + upper1 + upper2 + number1 + number2 + punctuation1 + punctuation2)
ready_password = random.sample(password_not_ready, k=8)
print(ready_password)
As per your requirement-
if you want to use random.sample you need to pass the second param k (length of the random sample from the password_not_ready.
so using
ready_password = random.sample(password_not_ready, k) #k is the desire length of the sample
print(ready_password)
with your existing code should work.
Links - https://www.geeksforgeeks.org/python-random-sample-function/
https://docs.python.org/3/library/random.html
I am writing a python program which has lists:
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z', 'A', 'B', 'C' , 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'Z' ]
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ]
How can I create a list or something so that my selection statement can identify
Identifiers = Letter { Letter | Digit }
Integers = Digit { Digit }
Any help would be appreciated.
Python strings have built in checks for letters and digits:
s = "test1"
for char in s:
is_digit = char.isdigit()
is_letter = char.isalpha() or is_digit
print char, is_letter, is_digit
If you are attempting to determine if a character is a letter or digit you should use this.
Since to qualify as an integer the entire item must be ints we can use .isdigit() if item passes this test we can append to integers if it fails we can append to identifiers
List comprehension:
identifiers = [i for i in l if not i.isdigit()]
integers = [i for i in l if i.isdigit()]
Expanded:
l = ['Aa', 'a3', '12']
identifiers = []
integers = []
for i in l:
if i.isdigit():
integers.append(i)
else:
identifiers.append(i)
print(identifiers)
print(integers)
['Aa', 'a3']
['12']
So I'm trying to get a handle on nested for loops in python, so i'm making a combination maker that loops until it finds the right combination of letters, numbers, and special characters. I know that intertools is the easiest way to do this, but I need to get a better understanding of how the nested for loops work. Here is how I would like the code to function.
a
b
c
...
1
2
3
...
~
#
#
...
A
B
C
...
aa
ab
ac
so on and so forth.
it continues to go through the lists I have until the correct set of characters. it will end up being quite a number of iterations by the end of it.
Here is my code
# this program is a password cracker that uses loops to find an appropriate character.
# It is capable of using numbers, letters, basic symbols, capitol letters, and up to 8 characters.
# It checks the whole list at once against the other whole list
lowalpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z']
upalpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z']
special = ['!', '#', '#', '$', '%', '^', '&', '*', '~', '?']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
hiddenpass = ['D', 'r', 'i', 'n', 'k', 'c', 'o', 'f', 'f', 'e', 'e']
i = len(hiddenpass)
password = ['']*i
low = 0
up = 0
sp = 0
num = 0
X = 0
Y = 0
Z = 1
def iterator():
global low, up, sp, num, X, password, lowalpha, upalpha, special, numbers
for low in range(len(lowalpha)):
password[X] = lowalpha[low]
for up in range(len(upalpha)):
password[X] = upalpha[up]
for sp in range(len(special)):
password[X] = special[sp]
for num in range(len(numbers)):
password[X] = numbers[num]
if password != hiddenpass:
for Y in range(len(password)):
for Z in range(len(lowalpha)):
password[Y] = lowalpha[Z]
iterator()
X = X + 1
low = 0
up = 0
sp = 0
num = 0
X = 0
Y = 0
Z = 1
for Z in range(len(upalpha)):
password[Y] = upalpha[Z]
iterator()
X = X + 1
low = 0
up = 0
sp = 0
num = 0
X = 0
Y = 0
Z = 1
for Z in range(len(special)):
password[Y] = special[Z]
iterator()
X = X + 1
low = 0
up = 0
sp = 0
num = 0
X = 0
Y = 0
Z = 1
for Z in range(len(numbers)):
password[Y] = numbers[Z]
iterator()
X = X + 1
else:
print('got em')
As of right now this does not work and fails the first time iterator() is called. it fails at password[X] = lowalpha[low] inside that function. https://imgur.com/a/4NZli Here is the error code.
I can't test with all combinations due to my processor heating issue but i can give you little idea what you are looking for :
import sys
lowalpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z']
upalpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z']
def repeat(*args, repeat=3):
result_1 = [tuple(i) for i in args] * repeat
final_list=[[]]
for i in result_1:
copy_final=final_list
final_list=[]
for j in copy_final:
for k in i:
find=([k]+j)
if find==['a','n','k']:
print("Here is find",find)
sys.exit()
final_list.append(find)
repeat(lowalpha,lowalpha)