I want to list the networks from a airodump command and then assign a number for each network.Output of airodump is like this
CH 1 ][ Elapsed: 24 s ][ 2022-06-22 11:12
BSSID PWR RXQ Beacons #Data, #/s CH MB ENC CIPHER AUTH WPS ESSID
60:8D:26:XX:XX:XX -87 29 38 36 1 1 195 WPA2 CCMP PSK 2.0 network1
34:57:60:XX:XX:XX -93 0 8 0 0 6 130 WPA2 CCMP PSK 2.0 MOVISTAR_XXX
C8:B4:22:XX:XX:XX -90 6 23 0 0 11 130 WPA2 CCMP PSK Locked MOVISTAR_XXXX
94:6A:B0:XX:XX:XX -89 2 34 3 0 6 130 WPA2 CCMP PSK 2.0 MiFibra-XXXX
i want to do something like this with a select number to choose the network
0. 60:8D:26:XX:XX:XX -87 29 38 36 1 1 195 WPA2 CCMP PSK 2.0 network1
1. 34:57:60:XX:XX:XX -93 0 8 0 0 6 130 WPA2 CCMP PSK 2.0 MOVISTAR_XXX
2. C8:B4:22:XX:XX:XX -90 6 23 0 0 11 130 WPA2 CCMP PSK Locked MOVISTAR_XXXX
3. 94:6A:B0:XX:XX:XX -89 2 34 3 0 6 130 WPA2 CCMP PSK 2.0 MiFibra-XXXX
Here is my script
#!/usr/bin/env ruby
require 'open3'
require 'highline/import'
def error
puts "\e[1;31m[*] ERROR: exit in 5s.\e[0m"
sleep 5
abort("BYE..")
end
WAIT_FOR_AIRODUMP = 20 #seconds
puts "\e[1;33m[*] wifi card:\e[0m"
system"airmon-ng"
puts "Choose wifi card: "
$wlan_name = gets.chomp
puts "#{$wlan_name} choosen"
if $wlan_name.chomp.start_with?('wlan', 'wlx', 'wlp')
`echo #{$wlan_name} > scancarte.txt`
puts "\e[1;32m[*] checking... OK.\e[0m"
else
error()
end
def get_wlan_interface
wlan_interface = $wlan_name
if wlan_interface[wlan_interface.length-3..-1] == 'mon'
monitor_enabled = true
else
monitor_enabled = false
end
if wlan_interface.length > 0
if monitor_enabled
puts "Monitor in: #{wlan_interface}"
else
puts "Card: #{wlan_interface}"
end
else
puts "No card detected, exit..."
exit!
end
{
name: wlan_interface,
monitor: monitor_enabled
}
end
def run_airmon_zc(wlan_name)
`ip link set #{wlan_name} down`.delete! ":"
puts "Starting monitor mode: #{wlan_name}".delete! ":"
system("iw dev #{wlan_name} set type monitor")
`ip link set #{wlan_name} up`.delete! ":"
end
def run_wash(wlan_name)
puts"#{wlan_name}".delete! ":"
cmd = "airodump-ng --encrypt wpa #{wlan_name}"
puts "Starting airodump: #{cmd} , wait #{WAIT_FOR_AIRODUMP} seconds to see networks..."
# sleep 30
p = IO.popen(cmd)
process_line = false
wifi_points = []
Thread.new do
p.each_line do |line|
if process_line
wifi_hash = process_wash_line(line)
if wifi_hash[:enc] == "WPA2" or wifi_hash[:enc] == "WPA3"
wifi_points << wifi_hash
end
end
if line.include?('Elapsed')
process_line = true
end
end
end
Thread.new do
start_time = Time.now
while true do
sleep(5)
diff = Time.now - start_time
print "%.0f" % diff + '... '
if diff > WAIT_FOR_AIRODUMP
puts "\nExiting airodump..."
`killall airodump-ng`
break
end
end
end.join
wifi_points
end
def process_wash_line(line)
fields = line.split(" ")
wifi_point = {
bssid: fields[0],
channel: fields[1],
power: fields[2],
version: fields[3],
enc: fields[4],
essid: fields[5]
}
wifi_point
end
def select_point_to_crack(access_points, wlan_iface_name)
access_points = access_points.sort_by { |access_point| access_point[:power] }
puts "Choose a number (between 0 and #{access_points.length - 1}):"
access_points.each_with_index do |access_point, idx|
puts "#{idx}. #{access_point[:essid]} #{access_point[:bssid]} #{access_point[:power]}dB Canal: #{access_point[:channel]}"
end
print "Choose a correct number:"
n = input_number(access_points.length)
point_to_crack = access_points[n]
`echo #{point_to_crack[:channel]} > canal.txt`
`echo #{point_to_crack[:bssid]} > bssid.txt`
`echo #{point_to_crack[:essid]} > ssid.txt`
cmd = "nextscript.rb"
puts "Name of network: #{point_to_crack[:essid]}"
exec(cmd)
end
def input_number(max)
while true
input = gets
if input.match(/^\d+$/)
n = input.to_i
if n < max
break
end
end
print "Chosse a number between 0 and #{max - 1}:"
end
n
end
# check_packages
wlan_iface = get_wlan_interface
wlan_iface_name = wlan_iface[:name]
if !wlan_iface[:monitor]
run_airmon_zc(wlan_iface[:name])
wlan_iface_name += ''
end
wifi_access_points = run_wash(wlan_iface_name)
select_point_to_crack(wifi_access_points, wlan_iface_name)
There is no error message when the script is launched but it just wait 20 seconeds and i can't see the networks appear with a number for each of them like expected.Code produce the followig output
Starting airodump: airocump-ng --encrypt wlan1 , wait 20 seconds to see networks..."
5... 10... 15... 20...
Exiting airodump...
Choose a number (between 0 and )
What im missing ?
Related
i'm trying ruby. I want to make a program which can continue the sequence.
1
11
21
1211
111221
(which next line is a calculated each number of previous line)
For example last line is(see previous) one of 1, one of 2, and two of 1.
I make a code and it works fine:
5.times do
result_seq = []
count = 1
puts initial_seq.join
initial_seq.size.times do
if (value = initial_seq.shift) == initial_seq.first
count += 1
else
result_seq << count << value
count = 1
end
end
initial_seq = result_seq
end
But now I want to write a simple method called Next
I want to make:
sec = Sequence.new(1)
sec.next -> will return 11
sec.next.next -> will return 21
sec.next.next.next -> will return 1211
How can i write it correctly using my code?
UPD
I wrote the tests for it:
require "spec_helper"
require "sequence"
describe Sequence do
let(:sequence) { Sequence.new("1") }
describe "to_s" do
it "return initial value" do
expect(sequence.to_s).to eql "1"
end
end
describe "next" do
it "generate next state" do
expect(sequence.next.to_s).to eql "11"
end
it "return Sequence instance" do
expect(sequence.next).to be_an_instance_of(Sequence)
end
it "generate next state 2 times" do
expect(sequence.next.next.to_s).to eql "21"
end
it "generate next state 3 times" do
expect(sequence.next.next.next.to_s).to eql "1211"
end
end
end
class Sequence
attr_reader :initial_seq
def initialize(initial_seq = [])
#initial_seq = initial_seq
print_next
end
def print_next
result_seq = []
count = 1
puts initial_seq.join
initial_seq.size.times do
if (value = initial_seq.shift) == initial_seq.first
count += 1
else
result_seq << count << value
count = 1
end
end
#initial_seq = result_seq
self #<===== The most important part for being able to chain `print_next`
end
end
Usage:
Sequence.new([1]).print_next.print_next.print_next.print_next
1
11
21
1211
111221
edit
If you want to initialize it with integer argument, not array:
def initialize(number)
#initial_seq = [number]
print_next
end
Sequence.new(1).print_next.print_next
1
11
21
Or, if you do not want initialize to accept an argument (assuming, it will always start with 1):
def initialize
#initial_seq = [1]
print_next
end
Sequence.new.print_next.print_next
1
11
21
Ruby provides Enumerators, which behave almost like in OP. Leaving the original code almost unaltered:
seq = Enumerator.new do |yielder|
initial_seq = [1]
loop do #endless loop, but don't worry, its lazy
result_seq = []
count = 1
yielder << initial_seq.join
initial_seq.size.times do
if (value = initial_seq.shift) == initial_seq.first
count += 1
else
result_seq << count << value
count = 1
end
end
initial_seq = result_seq
end
end
5.times{puts seq.next}
puts seq.next
I'm struggling to figure out how to loop numbers in a single line on ruby.
x = 0
while x <= 9
puts x
x +=1
end
This would give you
0
1
2
3
4
5
6
7
8
9
Each on different lines.
But what I want is to get this on a single line so like
01234567891011121314151617181920
Also not limited to just 0-9 more like 0 to infinity on a single line.
The purpose is to make an triangle of any size that follows this pattern.
1
12
123
1234
12345
123456
Each of these would be on a different line. The formatting here won't let me put in on different lines.
Would really like to solve this. It is hurting my head.
try this:
(1..9).each { |n| print n }
puts
You said you want "to make a triangle of any size that follows this pattern", so you should not make assumptions about how that should be done. Here are two ways to do that.
#1
def print_triangle(n)
(1..n).each.with_object('') { |i,s| puts s << i.to_s }
end
print_triangle(9)
1
12
123
1234
12345
123456
1234567
12345678
123456789
#2
def print_triangle(n)
s = (1..n).to_a.join
(1..n).each { |i| puts s[0,i] }
end
print_triangle(9)
1
12
123
1234
12345
123456
1234567
12345678
123456789
how about this solution:
last_num = 9
str = (1..last_num).to_a.join # create string 123456789
0.upto(last_num-1){ |i| puts str[0..i] } # print line by line
puts (1..9).map(&:to_s).join
Regarding your final aim there are lots of (probably easier) ways, but here's one:
def print_nums k
k.times { |n| puts (1..(n+1)).map { |i| i*10**(n+1-i) }.inject(:+) }
end
print_nums 9
#1
#12
#123
#1234
#12345
#123456
#1234567
#12345678
#123456789
This approach generates the actual numbers using units, tens, hundreds etc in relation to the line number i.
Thought Process
Looking at a basic example of four lines:
1
12
123
1234
is the same as:
1*10**0 #=> 1
1*10**1 + 2*10**0 #=> 12
1*10**2 + 2*10**1 + 3*10**0 #=> 123
1*10**3 + 2*10**2 + 3*10**1 + 4*10**0 #=> 1234
which in Ruby can be generated with:
(1..1).map { |i| i*10**(1-i) }.inject(:+) #=> 1
(1..2).map { |i| i*10**(2-i) }.inject(:+) #=> 12
(1..3).map { |i| i*10**(3-i) }.inject(:+) #=> 123
(1..4).map { |i| i*10**(4-i) }.inject(:+) #=> 1234
looking for a pattern we can generalise and put in a method:
def print_nums k
k.times { |n| puts (1..(n+1)).map { |i| i*10**(n+1-i) }.inject(:+) }
end
You could (and should) of course ignore all of the above and just extend the excellent answer by #seph
3.times { |i| (1..(i+1)).each { |n| print n }; puts }
#1
#12
#123
The simplest way if you want to start from 1
9.times {|n| puts n+1}
try if you want to start from 0
10.times {|n| puts n}
if you want pyramid format this is one way to do
9.times{|c| puts (1..c+1).to_a.join}
this is the ouput
2.3.0 :025 > 9.times{|c| puts (1..c+1).to_a.join}
1
12
123
1234
12345
123456
1234567
12345678
123456789
I wrote a program to create histograms of IP addresses, URLs, error codes, and frequency of IP visits, and would like to obtain the size, in bytes, of the data collected for each histogram. I looked around and saw a bit about the bytes method, but can't seem to get it to function.
Any idea on how to do that to this bit of code? I'd like to add the "byte puts" line after displaying the filename in each method.
class CommonLog
def initialize(logfile)
#logfile = logfile
end
def readfile
#readfile = File.readlines(#logfile).map { |line|
line.split()
}
#readfile = #readfile.to_s.split(" ")
end
def ip_histogram
#ip_count = 0
#readfile.each_index { |index|
if (#readfile[index] =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ )
puts #readfile[index]
puts #ip_count += 1
end
}
puts File.basename #logfile
end
def url_histogram
#url_count = 0
#readfile.each_index { |index|
if (#readfile[index] =~ /\/{1}(([a-z]{4,})|(\~{1}))\:{0}\S+/ )
puts #readfile[index]
puts #url_count += 1
end
}
puts File.basename #logfile
end
def requests_per_hour
#time_count2 = 0
#time_count3 = 0
#time_count4 = 0
#time_count5 = 0
#time_count6 = 0
#readfile.each_index { |index|
if (#readfile[index] =~ /\:\d{2}\:/ )
#new = #readfile[index].split(":")
if #new[1] == "02"
#time_count2 += 1
elsif #new[1] == "03"
#time_count3 += 1
elsif #new[1] == "04"
#time_count4 += 1
elsif #new[1] == "05"
#time_count5 += 1
elsif #new[1] == "06"
#time_count6 += 1
end
end
}
puts "#{#time_count2} instances during hour 2"
puts "#{#time_count3} instances during hour 3"
puts "#{#time_count4} instances during hour 4"
puts "#{#time_count5} instances during hour 5"
puts "#{#time_count6} instances during hour 6"
puts File.basename #logfile
end
def sorted_list
#codearray = Array.new
#http_code_count = 0
#count200 = 0
#count304 =0
#count301 = 0
#count403 = 0
#readfile.each_index { |index|
if #readfile[index] =~ /([2][0][0]")|([3][0][4])|([3][0][1])|([4][0][3])/
#codearray << #readfile[index]
#http_code_count += 1
if #readfile[index] == '200'
#count200 += 1
elsif #readfile[index] == "304"
#count304 += 1
elsif #readfile[index] == "301"
#count301 += 1
elseif #readfile[index] == "403"
#count403 += 1
end
end
}
#hash_count = 0
#frequencies = Hash.new(0)
#codearray.each { |word| #frequencies[word] += 1 }
#frequencies = #frequencies.sort_by { |a, b| a}
#frequencies.each { |word, frequency| #hash_count += frequency}
#frequencies.each { |key, value|
puts "Error #{key} : #{(value.to_f/#hash_count.to_f)*100}%"
}
puts File.basename #logfile
end
end
my_file = CommonLog.new("test_log")
my_file.readfile
my_file.ip_histogram
my_file.url_histogram
my_file.requests_per_hour
my_file.sorted_list
Assuming that the number of bytes processed is the entire size of each log file you could do something like this:
class CommonLog
attr_reader :bytes_read
def initialize(logfile)
#logfile = logfile
#bytes_read = File.size(logfile)
end
# ... now simply print "bytes_read" when desired ...
How to combine next with puts "#{web_url.status[0].to_i} together and display it in console but only if if part is true.
tried:
next if ... && puts "{web_url.status[0].to_i}"
web_url = open(url, :proxy => BaseParser::PROXY, "User-Agent" => BaseParser.rand_ua_string() )
next if web_url.nil?
next if web_url.status[0].to_i == 410
next if web_url.status[0].to_i == 310
next if web_url.status[0].to_i == 404
next if web_url.status[0].to_i == 530
Here it is :
next puts "#{web_url.status[0].to_i}" if x == 2 # expression here
Example :
x = 1
until x > 3
next puts("#{x} matched"),x+=1 if x == 2
puts "this iteration has number #{x}"
x += 1
end
# >> this iteration has number 1
# >> 2 matched
# >> this iteration has number 3
Go through the documentation keywords.
I am trying to set up a simple ruby program with a two additional threads.
One thread is to check the serial port for data and populate a variable if anything is found.
The second thread is is a call to 'gets', which when returned will stop the program.
The main thread simply ouputs the values stored by the first additional thread to the console (The program is a console based data logger).
My problem is that the second thread starts ok, as you can see from the "Press Enter..." note in the output.
D:\Documents and Settings\JMEDDING\My Documents\Ruby scripts>c-scope.rb -t
"Press 'Enter' to end process"
511, 511, 485 | | | | | + | | | | | 536
511, 511, 485 | | | | | + | | | | | 536
512, 511, 485 | | | | | XO | | | | | 536
512, 512, 485 | | | | | |+ | | | | | 536
but any data returned from
input = gets
does not make it into the program. Instead, it can be seen on the console command line after the program is killed with ctrl-c.
I am running this in the ruby console on windows. This used to work properly before, but I re-arranged the code at one point and now it is not cooperating. I have copied the entire program below. I guess this is something very simple, but I'm totally stuck, so thanks for taking a look.
Jon
require 'serialport'
TEST = true if ARGV[0] == '--test' || ARGV[0] == '-t'
# Edit the two settings below
port = "COM6" #"/dev/tty.usbserial-A600b1Q6"
baud = 9600
begin
if RUBY_PLATFORM =~ /win32/ || RUBY_PLATFORM == "i386-mingw32"
require 'Win32/Console/ANSI'
end
rescue LoadError
raise "You must 'gem install win32console' to use color on Windows"
end
if TEST
sp = nil
else
sp = SerialPort.new port, baud, 8, 1, SerialPort::NONE
end
symbols = ["X", "O", "#", "#"]
vals = [] #max four values to plot
input = nil
len = 50
max = 0
min = 1200
if TEST
vals = [511, 511]
end
def colorize(text, color_code)
"\033[1;#{color_code}m#{text}\033[0m"
end
def red(text); colorize(text, "31"); end
def green(text); colorize(text, "32"); end
def blue(text); colorize(text, "34"); end
def color_the_symbols(text, symbols)
color = 31 # "+" = red, then green, yellow, blue, pink
chars = ["+"] + symbols
chars.each_with_index do |s,i|
text.gsub!(s, colorize(s, "#{color + i}"))
end
text
end
def base_string (len, min, max, vals, symbols)
s = ""
(0..len).each {|i| s += i%5 == 0 ? "|" : " "}
vals.each_with_index do |val, i|
pos = len * (val - min)/ (max - min)
char = symbols.include?(s[pos] ) ? "+" : symbols[i]
s[pos] = char
end
color_the_symbols s, symbols
end
#listen to STDIN for stop signal
Thread.new do
p "Press 'Enter' to end process"
input = gets #pressing enter will stop the other loops
end
sleep(1)
#listening thread
Thread.new do
while !input
if TEST
(0...vals.count).each {|i| vals[i] += rand(3) - 1}
else
c = ""
word = ""
until c == "\n"
c = sp.getc
word << c.to_s
#p "Flushing buffer #{c}"
end
#p word.chomp
vals[0] = Integer(word.chomp)
end
sleep(0.5)
end
end
while !input #will be true until enter key is pressed
while vals.count == 0
#wait for firs data to show up
sleep (0.1)
end
for i in (0...vals.count) do
#validate min and max
max = (vals[i] * 1.05).to_i if vals[i] > max
min = (vals[i] * 0.95).to_i if vals[i] < min
end
output = base_string(len, min, max, vals, symbols)
output = " #{red(min.to_s)} " + output + " #{red(max.to_s)}"
color = 32
vals.each_with_index do |val,i|
output = " #{colorize("%4d," % val, "#{color + i}")}" + output
end
puts output
sleep(1)
end
I think there was a error somewhere, but the notification was being obscured by the threading behavior. I made some unrelated changes and after some debugging, it started working again. Sorry I can't be more specific.
You need to have
thread = Thread.new
# blah
end
thread.join
If you want that thread to finish its work before the program finishes.