Retrying with different port number - arrays

I have a program that will ping an IP address, then log the IP to a file if it pings successfully:
Invalid IP: 128.201.166.30
Proxy created: 66.25.173.128:80
Invalid IP: 225.195.111.59
Invalid IP: 249.133.221.70
Invalid IP: 40.21.11.99
Invalid IP: 201.27.136.108
Invalid IP: 152.77.109.45
Invalid IP: 120.76.159.122
Invalid IP: 108.244.67.42
Invalid IP: 73.231.16.193
Proxy created: 146.134.102.95:3128
Invalid IP: 133.216.1.59
Proxy created: 118.75.196.75:3128
Now what I would like to do is have the "good" IP address use each port in the following array: port = %w(80 3128 8080 8090 8888 8898 9999), for example:
Proxy created: 66.25.173.128:80
Proxy created: 66.25.173.128:3128
Proxy created: 66.25.173.128:8080
Proxy created: 66.25.173.128:8090
Proxy created: 66.25.173.128:8888
Proxy created: 66.25.173.128:8898
Proxy created: 66.25.173.128:9999
#Creates an IP with a port extension with each port number
I think I have a general idea on how I could do this:
File.open("example.txt", "a+"){
|s| s.puts("#{ip}:#{port[0]}",
"#{ip}:#{port[1]}",
"#{ip}:#{port[2]}"
#etc...
)}
I'm not entirely sure if that would work the way I'm expecting it to though, and even if it does, I'm 100% sure there's a better way to do, any help with this would be greatly appreciated, thank you.
Source:
require 'colored'
require 'timeout'
def create_possibles
port = %w(80 3128 8080 8090 8888 8898 9999).each do |port|
10.times do
ip = Array.new(4){rand(256)}.join('.')
Timeout::timeout(5) do
ping = `ping -n 1 #{ip}`
if ping =~ /Received = 1/
proxy = "#{ip}:#{port}"
puts "Proxy created: #{proxy}".green.bold
File.open("proxies.txt", "a+") {|s| s.puts(proxy)}
else
puts "Invalid IP: #{ip}".red.bold
next
end
end
end
end
end
create_possibles
EDIT:
I attempted my general idea of how to do this:
require 'colored'
require 'timeout'
def create_possibles
w%(80 3128 8080 8090 8888 8898 9999).each do |port|
1.times do
ip = Array.new(4){rand(256)}.join('.')
Timeout::timeout(5) do
ping = `ping -n 1 #{ip}`
if ping =~ /Received = 1/
# proxy = "#{ip}:#{port}"
puts "[SUCCESS]Proxy created for IP: #{ip}".green.bold
File.open("proxies.txt", "a+") {|s| s.puts("#{ip}:#{port[0]}",
"#{ip}:#{port[1]}",
"#{ip}:#{port[2]}",
"#{ip}:#{port[3]}",
"#{ip}:#{port[4]}",
"#{ip}:#{port[5]}",
"#{ip}:#{port[6]}",
"#{ip}:#{port[7]}")}
else
puts "[ERROR]IP failed to ping: #{ip}".red.bold
next
end
end
end
end
end
create_possibles
When run:
[ERROR]IP failed to ping: 185.105.73.104
[ERROR]IP failed to ping: 93.182.117.11
[ERROR]IP failed to ping: 112.210.73.187
[ERROR]IP failed to ping: 111.109.127.178
[SUCCESS]Proxy created for IP: 201.153.205.131
[ERROR]IP failed to ping: 128.236.57.123
[ERROR]IP failed to ping: 248.84.17.31
It ends up outputting information that looks like this:
201.153.205.131:0
201.153.205.131:0
201.153.205.131:0
201.153.205.131:1
201.153.205.131:1
201.153.205.131:1
201.153.205.131:0
201.153.205.131:1

I figured it out! The problem had to do with the .each do |ports|;
If I take that out like this:
require 'colored'
require 'timeout'
def create_possibles
ports = %w(80 3128 8080 8090 8888 8898 9999)
5.times do
ip = Array.new(4){rand(256)}.join('.')
Timeout::timeout(5) do
ping = `ping -n 1 #{ip}`
if ping =~ /Received = 1/
# proxy = "#{ip}:#{port}"
puts "[SUCCESS]Proxy created for IP: #{ip}".green.bold
File.open("proxies.txt", "a+") {|s| s.puts("#{ip}:#{ports[0]}",
"#{ip}:#{ports[1]}",
"#{ip}:#{ports[2]}",
"#{ip}:#{ports[3]}",
"#{ip}:#{ports[4]}",
"#{ip}:#{ports[5]}",
"#{ip}:#{ports[6]}")}
else
puts "[ERROR]IP failed to ping: #{ip}".red.bold
next
end
end
end
end
create_possibles
Then run it as is, it will output:
[ERROR]IP failed to ping: 111.20.77.200
[SUCCESS]Proxy created for IP: 217.252.149.35
[ERROR]IP failed to ping: 49.214.128.47
[ERROR]IP failed to ping: 116.101.28.115
[ERROR]IP failed to ping: 75.49.120.242
And will log:
217.252.149.35:80
217.252.149.35:3128
217.252.149.35:8080
217.252.149.35:8090
217.252.149.35:8888
217.252.149.35:8898
217.252.149.35:9999

Related

Check username/password in Linux without root

If I have a username and password pair how can I verify that they are actually correct in a Linux system? I know I can use passwd to do so but I want to do it programatically using C.
I should not require root privileges (so reading the shadow file is not an option).
Thank you.
If you are using a PAM, you might be able to make use of checkpassword-pam.
The manual has an example command (with debugging) which should give you a good place to start.
echo -e "username\0password\0timestamp\0" \
| checkpassword-pam -s SERVICE \
--debug --stdout -- /usr/bin/id 3<&0
This is a simple example to check the password with
some python code. No root privilegs are needed.
#!/usr/bin/python3
# simple linux password checker with
# standard python
import os, pty
def check_pass(user, passw):
# returns: 0 if check ok
# 1 check failed
# 2 account locked
if type(passw) is str:
passw = passw.encode()
pid, fd = pty.fork()
# try to su a fake shell which returns '-c OK' on ok
if not pid:
# child
argv = ('su', '-c', 'OK', '-s', '/bin/echo', user)
os.execlp(argv[0], *argv)
return # SHOULD NEVER REACHED
okflg = False
locked = False
while True:
try:
data = os.read(fd, 1024)
##print('data:', data, flush=True)
except OSError:
break
if not data:
break
data = data.strip()
if data == b'Password:':
os.write(fd, passw + b'\r\n')
elif data.endswith(b'OK'):
okflg = True
break
elif data.find(b'locked') > -1:
# show that account is locked
locked = True
print(data, flush=True)
break
os.close(fd)
# check result from su and okflg
if (not os.waitpid(pid, 0)[1]) and okflg:
return 0
return 2 if locked else 1
if __name__ == '__main__':
print(check_pass('xx', 'yy'))

Cache file content and then extract matches using regex

Please forgive a bash newbie for any silly questions.
I am really stuck here and I would love to know how this works and what I am doing wrong.
I have written this script which is supposed to capture syslog server based on protocol.
The input is as follows:
sys syslog {
include "destination remote_server {tcp(\"10.1.0.100\" port (514));tcp(\"192.168.1.5\" port (514));udp(\"192.168.1.60\" port (514));};filter f_alllogs {level (debug...emerg);};log {source(local);filter(f_alllogs);destination(remote_server);};"
remote-servers {
mysyslog {
host 192.168.1.1
}
remotesyslog1 {
host 192.168.1.2
}
remotesyslog2 {
host 192.168.1.3
local-ip 10.0.0.50
}
}
}
From this I would like to get something like in the end:
tcp=10.1.0.100
tcp=192.168.1.50
udp=192.168.1.60
udp=192.168.1.1
udp=192.168.1.2
udp=192.168.1.3
So I started with a bash script to parse the output.
#!/bin/bash
#Save output to file
syslogoutput=$(< /home/patrik/input)
echo "Testing variable:"
echo $syslogoutput
echo ""
#Declare array
tcpservers=()
echo $syslogoutput | while read line ; do
matches=($(echo $line | grep -Po '(tcp\("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")'))
#If number of matches is greater than 0, save them to tcpservers
if [ ${#matches[#]} -gt 0 ]; then
tcpservers=("${matches[#]}")
#Echoing matches
echo "Testing matches in loop:"
for i in "${matches[#]}"; do
echo $i
done
fi
done
echo "Testing output:"
for i in "${tcpservers[#]}"; do
echo $i
done
I expected something like this:
...input file separated by line breaks
Testing matches in loop:
tcp("10.1.0.100"
tcp("192.168.1.5"
Testing output:
tcp("10.1.0.100"
tcp("192.168.1.5"
But instead I get:
sys syslog { include "destination remote_server {tcp(\"10.1.0.100\" port (514));tcp(\"192.168.1.5\" port (514));udp(\"192.168.1.60\" port (514));};filter f_alllogs {level (debug...emerg);};log {source(local);filter(f_alllogs);destination(remote_server);};" remote-servers { mysyslog { host 192.168.1.1 } remotesyslog1 { host 192.168.1.2 } remotesyslog2 { host 192.168.1.3 local-ip 10.0.0.50 } } }
Testing matches in loop:
tcp("10.1.0.100"
tcp("192.168.1.5"
Testing output:
So on to my questions:
Why isn't tcpservers=("${matches[#]}") working?
Why isn't the output cached with line breaks?
Why does bash scripting make me want to jump from a tall building every time I try it?
/Patrik
Don't use redirection, as it starts the loop in a subshell, and variables form a subshell don't propagate into the parent shell.
while read line ; do
# ...
done <<< "$syslogoutput"
You also overwrite the tcpservers on each iteration. Change the assignment to
tcpservers+=("${matches[#]}")
# ^
# |
# add to an array, don't overwrite

Expect script fails on foreach loop

I have an expect script that cycles through a file with a list of hosts, ssh's into each one, sudo's to root, and runs id. The problem is it never returns to the source servers shell. It attempts to run from the host it has ssh'd into, thus messing up the logic.
set file [open "hosts.test"]
set hosts [split [read -nonewline $file] "\n"]
close $file
foreach host $hosts {
puts $host
spawn ssh -q -o StrictHostKeyChecking=no [lindex $argv 0]#$host
expect "Password: "
send "[lindex $argv 1]\r"
expect -re "(>|#) "
send "sudo su -\r"
expect "Enter YOUR password: "
send "[lindex $argv 1]\r"
expect -re "(>|#) "
send "id\r"
expect -re "(>|#) "
send "exit\r"
expect -re "(>|#) "
send "logout\r"
close $spawn_id
}
Here is the error:
send: sending "logout\r" to { exp4 }
server.blah.com
spawn ssh -q -o StrictHostKeyChecking=no user#server
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {21856}
expect: does "" (spawn_id exp5) match glob pattern "Password: "? no
Password:
expect: does "Password: " (spawn_id exp5) match glob pattern "Password: "? yes
expect: set expect_out(0,string) "Password: "
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) "Password: "
send: sending "passwordhere\r" to { exp5 }
Gate keeper glob pattern for '(>|#) ' is ''. Not usable, disabling the performance booster.
expect: does "" (spawn_id exp5) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no
expect: does "\r\n" (spawn_id exp5) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no
Password:
expect: does "\r\nPassword: " (spawn_id exp5) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no
expect: timed out
send: sending "sudo su -\r" to { exp5 }
expect: does "\r\nPassword: " (spawn_id exp5) match glob pattern "Enter YOUR password: "? no
expect: does "\r\nPassword: \r\n" (spawn_id exp5) match glob pattern "Enter YOUR password: "? no
Password:
expect: does "\r\nPassword: \r\nPassword: " (spawn_id exp5) match glob pattern "Enter YOUR password: "? no
expect: timed out
send: sending "passwordhere\r" to { exp5 }
Gate keeper glob pattern for '(>|#) ' is ''. Not usable, disabling the performance booster.
expect: does "\r\nPassword: \r\nPassword: " (spawn_id exp5) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no
expect: does "\r\nPassword: \r\nPassword: \r\n" (spawn_id exp5) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no
expect: read eof
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) "\r\nPassword: \r\nPassword: \r\n"
send: sending "id\r" to { exp5 send: spawn id exp5 not open
while executing
"send "id\r""
("foreach" body line 11)
invoked from within
"foreach host $hosts {
puts $host
spawn ssh -q -o StrictHostKeyChecking=no [lindex $argv 0]#$host
expect "Password: "
send "[lindex $argv 1]\r"
e..."
(file "./bootstrap.exp" line 19)

Error while issuing ping command inside monkeyrunner script

i am facing some issues in device.shell('ping -c 2 192.168.1.1') inside a monkeyrunner script.
Its throwing below error:-
120202 20:12:17.192:S [main] [com.android.chimpchat.adb.AdbChimpDevice] Error executing command: ping -c 2 192.168.1.1
while (count<1000) :
device.shell('dmesg -c')
print '****swithing OFF wifi in loop NO-',count
device.touch(400,155,MonkeyDevice.DOWN_AND_UP)
time.sleep(10)
print '****switching ON wifi in loop NO-',count
device.touch(400,155,MonkeyDevice.DOWN_AND_UP)
time.sleep(25)
fd=open('pingstats.txt','a+b')
fd.write('***Loop-%i \n************\n%s\n****************\n' % (int(count),ping))
ping = device.shell('ping -c 2 192.168.1.1')
status=re.search('unreachable',ping)
if status:
dmesg=device.shell('dmesg')
fd.write(logcat)
fd.close()
count = count + 1
please see above script...
Someone please help....

Powershell Parsing Help - Intermediate-sortaIntermediate Question

I have a text file with the following format:
Rtmp: 2a1234bzcde9 SL ID: 1549566 IP: 192.168.0.1
Rtmp: 45a1234b4erde9 SL ID: 1549566 IP: 192.168.0.1
Rtmp: a1254bcde9 SL ID: 1549566 IP: 192.168.0.1
Rtmp: 23a1ft4bcde9 SL ID: 1549566 IP: 192.168.0.1
Rtmp: a125egbcde9 SL ID: 1549566 IP: 192.168.0.1
I have several hundred entries involved here.
The problem is that I need to get the second entry in each line of text (inbetween rtmp and sl). Each of those numbers has a length between 6 to 15 characters, all random characters.
How would I pull it into an array of those numbers? I want to use these numbers to make them the names of user accounts in 2008r2 (not using AD) and create folders in the inetpub/ftproot and create the folders in there with aliases and link each account to its corresponding virtual folder.
The last bit I can do.. it's manipulating text files that I suck at!! Here's what I've got so far:
$items = Get-Content $HOME/desktop/info.txt
$splitItems = #()
$splitItems = foreach ($item in $items)
{
$item.split(" ")
}
That splits each line, so that splitItems[0] is the first line of text, now split into multiple lines of text because of the space delimiter.
If I tried to take the SplitItems aray and use the same type of foreach to further split it. It gave me back an array of chars. USEFUL LOL haha. well.. each way I try I keep getting mumbo jumbo or it's not a string type (though get type seems to say it's a string). I think in the process the string type changes to a generic io.object type?
Any ideas or help would be immensely appreciated.!!!
The regex answers may be the right way to go, but if your input file is really as consistently formatted as you suggest, you may have been on the right track to begin with. Try something like this:
$accounts = gc info.txt | % { ($_ -split ' ')[1] }
$accounts
Given your text file is "c:\temp\t.txt", can you test this :
switch -regex -file "c:\temp\t.txt"
{
"^Rtmp: (.*) SL.*$" {$matches[1]}
}
$a = "Rtmp: 2a1234bzcde9 SL ID: 1549566 IP: 192.168.0.1 Rtmp: 45a1234b4erde9 SL ID: 1549566 IP: 192.168.0.1 Rtmp: a1254bcde9 SL ID: 1549566 IP: 192.168.0.1 Rtmp: 23a1ft4bcde9 SL ID: 1549566 IP: 192.168.0.1 Rtmp: a125egbcde9 SL ID: 1549566 IP: 192.168.0.1"
[regex]$regex = "Rtmp:\s(\S+)\sSL"
[regex]::matches($a,$regex) | foreach-object {$_.groups[1].value}
2a1234bzcde9
45a1234b4erde9
a1254bcde9
23a1ft4bcde9
a125egbcde9
If you want to extract one part from a string, and this string is following a defined pattern, simply use '-replace' and a regular expression.
$numbers = ($items -replace '^Rtmp: (\S+) SL.*$','$1')
This line will iterate through each item, extract the string and output it to the new array.
Some meanings:
\S+ means "One or more characters that are not whitespace"
$1 means "The part within the first set of brackets"

Resources