Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Thursday, July 14, 2011

Evince would not start over SSH X-forwarding

Evince, which is the primary PDF and PostScript viewer on Ubuntu, recently stopped working on my Ubuntu/Natty (11.04) over SSH X-forwarding. It works flawlessly inside 'gnome-session'. But, would not run as a regular application (e.g. xpdf or gv) over X-forwarded SSH.

After digging up a little bit, I found a lots of these message in my
dmesg output:

[1305902.871825] type=1400 audit(1310620543.587:75): apparmor="STATUS" operation="profile_remove" name="/usr/bin/evince" pid=20166 comm="apparmor

It was obvious that apparmor [1] was blocking Evince to run outside Gnome session over SSH. A solution to disable the AppArmor block on Evince over SSH connection was suggested here [2]:

#inside the gnome-terminal
sudo ln -s /etc/apparmor.d/usr.bin.evince /etc/apparmor.d/disable
sudo /etc/init.d/apparmor restart


And voila! Now Evince can again be launched over SSH.

[1] http://en.wikipedia.org/wiki/AppArmour
[2] http://ubuntuforums.org/showthread.php?t=1472642

Monday, November 22, 2010

Download Fileserve links from shell

The script assumes, you have
  • ImageMagick
  • Curl
  • Common Unix utilities (eg. sed, awk, cut etc.)
... are installed in the path. The captcha is shown using 'display' component from ImageMagick. You need X forwarding if you are running the script over ssh.


#!/bin/bash
#source /home/babil/Desktop/recaptcha/plowshare-0.9.4/src/lib.sh

PUB_URL='http://www.fileserve.com/file/zXvrJS2'
FILESERVE_RECAPTCHA_PUBKEY='6LdSvrkSAAAAAOIwNj-IY-Q-p90hQrLinRIpZBPi'
IMAGE_VIEWER="display"

CAPTCHA_IMAGE=$(mktemp) #'captcha.jpg'
COOKIE=$(mktemp) #'tmp_cookie.txt'
HTML=$(mktemp) #'tmp_fileserve.htm'

AGENT='Mozilla/5.0 (X11; U; Linux x86_64; en-AU; rv:1.9.2.12) Gecko/20101027 Ubuntu/10.10 (maverick) Firefox/3.6.12'
CUSTOM_HEADERS='-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: en-us,en;q=0.5" -H "Accept-Encoding: gzip,deflate" -H "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" -H "Keep-Alive: 115" -H "Connection: keep-alive"'
XML_HTTP_REQUEST='-H "X-Requested-With: XMLHTTPRequested"'

RED='\e[0;31m'
CYAN='\e[0;36m'
NC='\e[0m' # No Color


function regex_replace() {
RE='s/\(\/\|\\\|&\)/\\&/g'
FROM=$(sed -e "$RE" <<< "$1")
TO=$(sed -e "$RE" <<< "$2")
sed -e "s/$FROM/$TO/g"
}

# Delete leading and trailing spaces, tabs, \r, ...
function strip_whiteSpace() {
echo "$1" | sed "s/^[[:space:]]*//; s/[[:space:]]*$//"
}

# Return uppercase string
function uppercase() {
tr '[a-z]' '[A-Z]'
}

function clean_up()
{
rm -f $COOKIE
rm -f $CAPTCHA_IMAGE
rm -f $HTML
}

function find_fileName()
{

if [ -z $FILE_NAME ]
then
FILE_NAME=$(cat $HTML | grep 'Forum (BBCode)' | sed -n 's/.*File name: \(.*\) File size.*/\1/p')
fi

if [ -z $FILE_NAME ]
then
FILE_NAME=$(cat $HTML | grep 'Forum (BBCode)' | sed -n 's/.*url=\(.*\)\]\[b\].*/\1/p')
fi

if [ -z $FILE_NAME ]
then
FILE_NAME=$(cat $HTML | grep 'Website (HTML)' | sed -n 's/.*href=.\(.*\).>.b.File name.*/\1/p')
fi

if [ -z $FILE_NAME ]
then
FILE_NAME=$(cat $HTML | grep 'Text link' | sed -n 's/.*value="\(.*\)".*/\1/p')
fi

FILE_NAME=$(strip_whiteSpace $FILE_NAME)

}

function delay()
{
for((i=$1;i>=1;i--))
do
echo -n "$i "
sleep 1s
done
echo
}

function check_waitTime()
{
WAIT_TIME=$(cat $HTML | sed -n 's/.*You need to wait \(.*\) seconds to start another download.*/\1/p')
if [ -z $WAIT_TIME ]
then
WAIT_TIME=3
fi
WAIT_TIME=$(strip_whiteSpace $WAIT_TIME)
echo $WAIT_TIME
}

function solve_recaptcha()
{
CAPTCHA_URL='http://www.google.com/recaptcha/api/challenge?k='$FILESERVE_RECAPTCHA_PUBKEY
echo -e "$RED [*] captcha url: $NC $CAPTCHA_URL "

CHALLENGE=$(wget -q --referer $PUB_URL "$CAPTCHA_URL" -O - | grep challenge | sed -n "s/.*'\(.*\)',/\1/p")
echo -e "$RED [*] captcha challenge: $NC $CHALLENGE"
wget -q --referer $PUB_URL http://www.google.com/recaptcha/api/image?c=$CHALLENGE -O $CAPTCHA_IMAGE

#convert -trim $CAPTCHA_IMAGE $CAPTCHA_IMAGE
#$IMAGE_VIEWER "$CAPTCHA_IMAGE" &
#pid=$!
#read -p "[*] enter captcha response: " RESPONSE
#RESPONSE=$(echo $RESPONSE | sed 's/ /+/g')
#disown $(kill -9 $pid) 2>&1 1>/dev/null

echo -e -n "$RED [*] captcha input : $NC"
RESPONSE=$(prompt_captcha.py $CAPTCHA_IMAGE | sed 's/ /+/g')
echo "$RESPONSE"

RESULT=$(curl -s --user-agent "$AGENT" --cookie-jar $COOKIE --referer $PUB_URL http://www.fileserve.com/checkReCaptcha.php --data "recaptcha_challenge_field=$CHALLENGE&recaptcha_response_field=$RESPONSE&recaptcha_shortencode_field=$SHORTENCODE")
}


### - 0
if [ -z "$1" ]
then
echo
echo "[*] usage: $(basename $0) $PUB_URL"
echo
exit -1
else
PUB_URL="$(strip_whiteSpace $1 | sed 's#//fileserve.com#//www.fileserve.com#g')"
fi
echo -e "$RED [*] attempting to download: $CYAN $PUB_URL $NC"


### - 1
clean_up

### - 2
SHORTENCODE=$(echo $PUB_URL | sed -n 's/.*file\/\(.*\)/\1/p' | cut -d '/' -f 1)
SHORTENCODE=$(strip_whiteSpace $SHORTENCODE)

### - 3 save cookie
curl -s --user-agent "$AGENT" --cookie-jar $COOKIE --referer $PUB_URL $PUB_URL --data 'checkTimeLimit=check' --output $HTML

### - 4 immitating "slow download".click()
curl -s --user-agent "$AGENT" --cookie $COOKIE --referer $PUB_URL $PUB_URL --data "recaptcha_shortencode_field=$SHORTENCODE" --output $HTML

### - 5
if [ -z "$2" ]
then
FILE_NAME="$(strip_whiteSpace $2)"
else
FILE_NAME=""
fi
find_fileName
if [ -z $FILE_NAME ]
then
echo "[*] can not find file-name, exiting ..."
exit -1
else
echo -e "$RED [*] file-name found: $NC $FILE_NAME"
fi

### - 6 solve captcha
RESULT="(null)"
while [ $RESULT != "success" ]
do
solve_recaptcha
RESULT=$(echo $RESULT | sed -n 's/{.*"\(.*\)".*/\1/p')
done
echo -e "$RED [*] captcha solved :)$NC"

### - 7 free-user delay
WAIT_TIME=$(curl -s --user-agent "$AGENT" --cookie $COOKIE --referer $PUB_URL $PUB_URL --data "downloadLink=wait" --output -)
echo -e "$RED [*] free-user delay: $NC $WAIT_TIME"
WAIT_TIME=$(echo $WAIT_TIME | sed 's/[^0-9]//g' | bc)
delay $WAIT_TIME
$(curl -s --user-agent "$AGENT" --cookie $COOKIE --referer $PUB_URL $PUB_URL --data "downloadLink=show" --output /dev/null)


### - 8 final download ... should give a 302 redirection
echo -e "$RED [*] Starting download ...$NC"
REDIR=$(curl --user-agent "$AGENT" --cookie $COOKIE --referer $PUB_URL $PUB_URL --data "download=normal" --location --location-trusted --output $FILE_NAME --write-out 'REDIRECT-URL:[%{url_effective}]' | sed -n 's/.*:\[\(.*\)\]/\1/p')

if [[ ! "$REDIR" =~ "/dl/" ]]
then
echo -e "$RED [*] incorrect redirection; delay then restart ...$NC"
mv -f $FILE_NAME $HTML
WAIT_TIME=$(check_waitTime)
delay $WAIT_TIME
$0 $PUB_URL
else
echo -e "$RED [*] done :D. $NC"
clean_up
exit
fi

Tuesday, July 27, 2010

Bash: simple wrapper script for java jar files

There could be thousands of other (probably better) ways of writing a wrapper bash script for executing jar files on command line (instead of having to do "java -jar tool.jar"). For whatever it is worth, here's what I came up in quick 2 minutes. One can optimize according to his/her needs inside the for loop:
    1 #!/bin/bash
2
3 path='/home/USERNAME/android-sdk-linux_86/tools'
4
5 args=''
6 for ((i=$#;i>0;i--))
7 do
8 args="${args} ${1}"
9 shift
10 done
11
12 java -jar $path/tool.jar "$args"
13
14

Monday, February 22, 2010

Decode captcha in bash

  1. convert input image to grayscale using imagemagik
    convert input.png -colorspace Gray output.tif
  2. decode with tesseract
    tesseract output.tif result; cat result.txt

Bash wordlist/dictionary generator for brute-forcing

This example code will generate passwords of 4 characters only. To generate bigger passwords, there has to be more loops (pretty obvious in the code below). Happy Brute-forcing !!
1 #!/bin/bash
2
3 list=`echo {0..9} {a..z} {A..Z}`
4
5 for c1 in $list
6 do
7 for c2 in $list
8 do
9 for c3 in $list
10 do
11 for c4 in $list
12 do
13 echo $c1$c2$c3$c4
14 done
15 done
16 done
17 done
18

Tuesday, February 9, 2010

Automate downloading from hotfile.com

    1 #!/bin/bash
2
3 RED='\e[1;31m'
4 CYAN='\e[1;36m'
5 NC='\e[0m' # No Color
6
7 if [ -z "$1" ]
8 then
9 echo
10 echo -e "${RED}[+] usage: ${CYAN}`basename $0` http://hotfile.com/...
11 /filename.rar.html${NC}"
12 echo
13 exit
14 fi
15
16 url="$1"
17
18 echo
19 echo -e "[+] Input URL : ${RED}$url${NC}"
20
21 tm_='1245072880'
22 tmhash_='e5b845119f0055c5d8554ee5f2ffc7b2d5ef86d7'
23 wait_='30'
24 waithash_='3bf07c5d83f2e652ff22eeaee00a6f08d4d2409a'
25 action_='capt'
26
27 line_with_url='http://hotfile.com/get/'
28
29 url2=`wget -q "$url" --post-data "tm=${tm_}&tmhash=${tmhash_}&wait=${wait_}
30 &waithash=${waithash_}&action=${action_}" -O - | grep "$line_with_url" |
31 sed -s 's/.*<a href="\([^"]*\)".*$/\1/g'`
32
33 echo -e "[+] Real URL : ${RED}$url2 ${NC}"
34 echo
35
36 if [ ! -z "$url2" ]
37 then
38 wget "$url2"
39 fi
40

Tuesday, December 15, 2009

OneManga.com leecher

This one leeches manga from www.onemanga.com. I was too lazy to implement the chapter ripper part. It will only leech the whole series with the right URL as argument.

** code comes with no warranty or whatsoever. I'm not responsible if it breaks anybody's computer. Use it at your own risk. You have been warned !!! **

Example:
onemanga.sh http://www.onemanga.com/School_Rumble/

    1 #!/bin/bash
2
3 RED='\e[0;31m'
4 CYAN='\e[0;36m'
5 NC='\e[0m' # No Color
6
7 if [ -z $1 ]
8 then
9 echo
10 echo "[*] usage: `basename $0` manga_url"
11 echo
12 exit
13 else
14 manga_name=`echo $1 | awk -F '/' '{for(i=NF;i>=0;i--){if(length($i)>0){
15 print $i;break;}}}'`
16 fi
17
18 main_url="http://www.onemanga.com"
19
20 rm -rf ${manga_name}
21
22 ##finding list of chapters
23 echo -n -e "${CYAN}[*]${RED} Finding total chapters in ${CYAN} $manga_name
24 ${NC}= "
25 wget -q -nv ${main_url}/${manga_name} -O tmp.txt
26 chapters=`cat tmp.txt | grep '<td class="ch-subject"><a href="/' | awk -F
27 '"' '{print $4}'`
28
29
30 count=0
31 for c in $chapters
32 do
33 mkdir -p ./$c
34 count=$((count+1))
35 done
36 echo -e "${CYAN}${count}${NC}"
37 ##
38
39 ##parse chapter and download
40 for chapter in $chapters
41 do
42 pwd=`pwd`
43
44 cd ./$chapter
45
46 ## initial wget
47 echo -e "${CYAN}[*]${RED} Trying to find the image base url${NC}"
48
49 ## find collect the first page in the chapter
50 wget -q -nv $main_url/$chapter -O tmp.txt
51 page=`cat tmp.txt | grep "Begin reading" | awk -F '"' '{print $2}'`
52
53 ## now go to that page & find image base
54 wget -q -nv ${main_url}${page} -O tmp.txt 2>/dev/null
55 image=`cat tmp.txt | grep "img_url" | awk -F '"' '{for(i=1;i<NF;i++){if($i
56 ~ "jpg"){print $i}}}' | awk -F '/' '{print $NF}'`
57 image_base=`cat tmp.txt | grep "img_url" | awk -F '"' '{for(i=1;i<NF;i++){
58 if($i ~ "jpg"){print $i}}}' | sed s/"$image"//g`
59 echo -e "${RED}>>${NC} $image_base"
60
61 ## download
62 d=$((d+1))
63 names=`cat tmp.txt | awk '{for(i=1;i<=NF;i++){if($i ~ "selected")go++}{if(
64 go>1){print}}}' | grep -e "</option>" -e 'credits</option>' -e 'extra*<
65 /option>' -e 'cover*</option>' | awk -F '"' '{print $2}'`
66
67 n=0
68 for k in $names
69 do
70 n=$((n+1))
71 done
72
73 echo -e "${CYAN}[*]${RED} Downloading ${CYAN}$n ${RED}images from chapter
74 ${CYAN}$chapter ${RED} ... ##${CYAN}$((count-d+1))${RED}##${CYAN}$count${
75 RED}##${NC}"
76 for k in $names
77 do
78 #echo -e "${RED}>>${NC} ${image_base}${k}.jpg"
79 wget -nv "${image_base}${k}.jpg"
80 done
81
82 cd $pwd
83 done
84 ##

MangaFox.com leecher

This is a bash script to leech a whole series or a particular chapter from www.mangafox.com. I wrote this script for my niece, hope it helps you too. Using is simple, 1st argument is the url to a particular manga and 2nd (optional) argument is the the chapter in that manga.

** code comes with no warranty or whatsoever. I'm not responsible if it breaks anybody's computer. Use it at your own risk. You have been warned !!! **

Example:
mangafox.sh http://www.mangafox.com/manga/school_rumble/ 282
or
mangafox.sh School_Rumble 282
or
mangafox.sh
http://www.mangafox.com/manga/school_rumble/
or
mangafox.sh
School_Rumble

    1 #!/bin/bash
2
3 RED='\e[1;31m'
4 CYAN='\e[1;36m'
5 NC='\e[0m' # No Color
6 YLW='\e[1;33m'
7 WHITE='\e[0;37m'
8
9 main_url="http://www.mangafox.com/manga"
10 wget_param="--tries=10 --retry-connrefused"
11
12 ## usage
13 if [ -z $1 ]
14 then
15 echo
16 echo -e "${CYAN}[*]${RED} usage: `basename $0` manga_url${NC}"
17 echo
18 exit
19 else
20 manga_name=`echo $1 | awk -F '/' '{for(i=NF;i>=0;i--){if(length($i)>0){
21 print $i;break;}}}'`
22 if [ ! -z "$2" ]
23 then
24 specific_chapter="$2"
25 fi
26 fi
27 ##
28
29 function find_chapters()
30 {
31 TMP="${manga_name}_find_chapters.tmp"
32
33 echo -n -e "${CYAN}[*]${RED} Finding total chapters in ${CYAN} $manga_name
34 ${NC}= "
35 wget $wget_param -q -nv "${main_url}/${manga_name}/?no_warning=1" -O $TMP
36 chapters=`cat $TMP | grep -e 'class="chico">' | grep -v -e '</td>' -e
37 '#listing' | awk -F '"' '{print $2}' | sed 's/^\/manga\///g'`
38
39 count=0
40 for c in $chapters
41 do
42 mkdir -p ./$c
43 #echo $c ##debug
44 count=$((count+1))
45 done
46 echo -e "${CYAN}${count}${NC}"
47 }
48
49
50 function parse_chapter_n_download()
51 {
52 PAGES="pages.tmp"
53 PAGE="page_html.tmp"
54
55 for chapter in $chapters
56 do
57 pwd=`pwd`
58
59 if [ "$specific_chapter" ]
60 then
61 mkdir -p "$specific_chapter" 2>/dev/null
62 chapter=$specific_chapter
63 fi
64
65 cd ./$chapter
66
67 ## find total number of pages in chapter
68 echo -n -e "${CYAN}[*]${RED} Total pages in ${CYAN} $chapter ${NC}= "
69 wget -q -nv $wget_param $main_url/$chapter -O $PAGES
70 pages=`cat $PAGES | grep '^.*<option value=.*<\/select>.*$' -m1 | awk '{
71 for(i=1;i<=NF;i++){if($(i-1)~"value"){print $i}}}' | sed -e
72 's/selected//g;s/option//g;s/[<>\/"=]//g;'`
73
74 n=0
75 for k in $pages
76 do
77 #echo $k ##debug
78 n=$((n+1))
79 done
80 echo -e "${CYAN}$n${NC}"
81
82 ## now i have a list of (1,2,3...).html pages
83 for p in $pages
84 do
85 wget $wget_param -q -nv $main_url/$chapter/${p}.html -O $PAGE
86 img_url=`cat $PAGE | grep 'onclick="return enlarge();' | awk '{for(i=1;i<
87 =NF;i++){if($i~"http://"){print $i}}}' | sed 's/src=//g;s/["=]//g'`
88 img=`echo $img_url | awk -F '/' '{print $NF}'`
89 echo -e -n "${CYAN}>>${WHITE} $img_url ${RED} ... ${NC}"
90 wget $wget_param -q -nv $img_url
91 if [ -e $img ]
92 then
93 echo -e "${CYAN}[done]${NC}"
94 else
95 echo -e "${YLW}[failed]${NC}"
96 fi
97 done
98
99 cd $pwd
100
101 if [ "$specific_chapter" ]
102 then
103 exit;
104 fi
105 done
106 }
107
108
109 function main()
110 {
111 rm -rf ${manga_name}
112 find_chapters
113 parse_chapter_n_download
114 }
115
116 main

Thursday, December 10, 2009

Listing directories only in bash

Now this is embarrassing. All these years with an Unix operating system and I couldn't figure it out by myself. Anyways, here is how you do it:

Method 1:

ls -d */
Method 2:
echo */

Thursday, January 15, 2009

Use pastebin.com as your online clipboard

THE SCRIPTS ARE BROKEN AT THE MOMENT DUE TO CHANGE IN PASTEBIN.COM WEBSITE. I HAVE CREATED ANOTHER PASTEBIN SCRIPT USING THEIR API. FIND IT HERE:  http://babilonline.blogspot.com/2010/10/post-to-pastebin-from-command-line.html


I created the following pair of scripts to paste text files on pastebin.com from console and fetching it may be from a different computer on a different network on console. Comes really handy when you just want to share some texts on two diffrent computers. And the best thing is it lets you paste things that are blocked by pastebin ;).

CHANGE THE VARIABLE "USER" TO CREATE YOUR OWN CLIPBOARD ON PASTEBIN.

=============
pastebin.sh
============
=

1 #!/bin/bash
2 #
3
4 USER="YOURNAME_IN_ONE_WORD"
5
6 bold='\e[1;1m';
7 white='\e[1;37m'
8 yellow='\e[1;33m'
9 magenta='\e[1;35m'
10 blue='\e[1;34m'
11 red='\e[1;31m'
12 cyan='\e[1;36m'
13 reset='\e[0m'
14
15
16 if [ -z $1 ]; then
17 echo
18 echo "[*] usage: `basename $0` filename"
19 echo
20 exit
21 fi
22
23 case `file "$1"` in
24 *Bourne*)
25 TYPE="bash";;
26 *python*)
27 TYPE="python";;
28 *perl*)
29 TYPE="perl";;
30 *HTML*)
31 TYPE="html4strict";;
32 *)
33 TYPE="text";;
34 esac
35
36 DATA=`uuencode -m $1 $1`
37 #DATA=`cat "$@" | ./urlencode.sh`
38
39 url=`curl -d format=$TYPE -d code2="$DATA" -d poster=$USER -d expiry=expiry_day -d paste=Send -i -s http://babil.pastebin.com | grep Location | sed 's/Location:[[:blank:]]\+//g;'`
40
41 echo -e "${magenta} ${url} ${reset}"
42
43


============
getbin.sh
============



1 #!/bin/bash
2
3 bold='\e[1;1m';
4 white='\e[1;37m'
5 yellow='\e[1;33m'
6 magenta='\e[1;35m'
7 blue='\e[1;34m'
8 red='\e[1;31m'
9 cyan='\e[1;36m'
10 reset='\e[0m'
11
12 if [ -z $1 ];then
13 n=1
14 else
15 n=`echo $1/1 | bc`
16 fi
17
18 keys=`wget -nv -q -O - http://babil.pastebin.com | grep -E 'ago</li>$' | awk -F '"' '{print $2}'| awk -F '/' '{print $NF}'`
19
20 if [ -z "$keys" ]; then
21 echo
22 echo "[*] no saved post found in pastebin.com"
23 exit
24 fi
25
26 for i in $keys
27 do
28 echo -e "[[${cyan} $i ${reset}]]"
29 wget -nv -q http://babil.pastebin.com/pastebin.php?dl=$i -O - | uudecode -o -
30 n=$((n-1))
31 if [ $n -eq 0 ];then
32 exit
33 fi
34 done
35
36

Monday, December 29, 2008

Bash scripting :: Handling filenames with spaces


The following script moves a specified amount of files from the currect directory to a specified directory. I had a lots of images saved in one folder. Accessing the folder used to take a long time. So I decided to put the files chronologically in seperate folders. The challenge was - lots of my files had spaces in their names. So, a normal "for i in `ls -t`" does not work. So, I came up with the following work around. So far it works pretty good ;)



 1 #!/bin/bash
 2
 3 my_space="XXYYZZ"
 4 me=`basename $0`
 5
 6 if [ -z $1 ] || [ -z $2 ];then
 7         echo
 8         echo "[*] usage:  $me  dirname how_many"
 9         echo
10         exit
11 fi
12
13 if [ ! -d $1 ];then
14         echo
15         echo "[*] '$1' does not exist."
16         echo
17         exit
18 fi
19
20 ii=`ls -t1 | grep -v $me | sed "s/ /$my_space/g"`
21
22 c=0
23 for i in $ii
24 do
25         c=$((c+1))
26         n=`echo $i | sed "s/$my_space/ /g"`
27         echo -n "[$c]  "
28         mv -v -f "$n" $1
29         if [ $c -ge $2 ];then
30                 exit
31         fi
32 done
33

Thursday, November 27, 2008

Controlling netgear ( D834G ) router from shell


You can perform the following operations from the shell
  • Disconnect
  • Connect
  • Reconnect (Disconnect, then connect again)
  • Show status
  • reboot router



1 #!/bin/bash
2
3 router_ip=192.168.0.1
4 username="admin"
5 password="CHANGE-ME"
6
7 debug_url="http://$router_ip/setup.cgi?todo=debug"
8 status_url="http://$router_ip/setup.cgi?next_file=s_status.htm"
9 connection_url="http://$router_ip/setup.cgi?next_file=st_poe.htm"
10 disconnect_url="http://$router_ip/setup.cgi?todo=disconnect&next_file=st_poe.htm"
11 connect_url="http://$router_ip/setup.cgi?todo=connect&next_file=st_poe.htm"
12
13
14 RED='\e[1;31m'
15 CYAN='\e[1;36m'
16 NC='\e[0m' # No Color
17
18 tmp_file="/tmp/babil-router.info"
19 sleep_time=3
20
21
22 function clean_up()
23 {
24 rm -f $tmp_file
25 }
26
27 function reboot_router()
28 {
29 report=`wget -q --user $username --password $password $debug_url -O /dev/null`
30 echo "[+] $report"
31 sleep 2
32 (sleep 2; echo "reboot"; sleep 2) | telnet $router_ip
33 }
34
35
36 function ip()
37 {
38 #curl --basic --user $username:$password $status_url 2>/dev/null | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
39 wget -q --user $username --password $password $status_url -O - | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
40
41 c=1
42 while read line
43 do
44 #echo "$c $line"
45 if [[ $c == 71 ]];then
46 echo -e "[+] outbound ip : ${RED} $line ${NC}"
47
48 elif [[ $c == 83 ]];then
49 echo -e "[+] gateway ip : ${RED} $line ${NC}"
50
51 fi
52
53 c=$((c+1))
54
55 done < $tmp_file
56 clean_up
57 }
58
59
60 function stat()
61 {
62
63 ip
64
65 #curl --basic --user $username:$password $connection_url 2>/dev/null | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
66 wget -q --user $username --password $password $connection_url -O - | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
67
68 c=1
69 while read line
70 do
71 #echo "$c $line"
72 if [[ $c == 61 ]];then
73 echo -e "[+] duration : ${RED} $line ${NC}"
74
75 elif [[ $c == 65 ]];then
76 echo -e "[+] status : ${RED} $line ${NC}"
77
78 fi
79
80 c=$((c+1))
81
82 done < $tmp_file
83
84 clean_up
85
86 }
87
88
89 function connect()
90 {
91 echo -n "[+] connecting "
92 #curl --basic --user $username:$password $connect_url 2>/dev/null | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
93 wget -q --user $username --password $password $connect_url -O - | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
94 for ((i=0;i<$sleep_time;i++))
95 do
96 echo -n "."
97 sleep 1
98 done
99
100 echo
101 stat
102 }
103
104
105 function disconnect()
106 {
107 echo -n "[+] disconnecting "
108 #curl --basic --user $username:$password $disconnect_url 2>/dev/null | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
109 wget -q --user $username --password $password $disconnect_url -O - | sed -e :a -e 's/<[^>]*>//g;/<;/N;//ba' > $tmp_file
110 for ((i=0;i<$sleep_time;i++))
111 do
112 echo -n "."
113 sleep 1
114 done
115
116 echo
117 stat
118 }
119
120 case "$1" in
121
122 connect)
123 echo
124 connect
125 echo
126 ;;
127
128 disconnect)
129 echo
130 disconnect
131 echo
132 ;;
133
134 stat)
135 echo
136 stat
137 echo
138 ;;
139 reboot)
140 echo
141 reboot_router
142 echo
143 ;;
144 reconnect)
145 echo
146 connect
147 echo
148 disconnect
149 echo
150 ;;
151 *)
152 echo
153 echo -e "[+] usage: `basename $0` { ${RED}connect | disconnect | reconnect | stat | reboot${NC} }"
154 echo
155 exit 1
156 ;;
157
158 esac
159
160