Help me write a bash script

Questions about other topics - please check if your question fits better in another category before posting here
Forum rules
Before you post please read this

Help me write a bash script

Postby NiksaVel on Wed Aug 15, 2007 4:20 pm

Hey if one of you gurus out there could devote a bit of time to help me write a script that I could run on my "download comp" I'd really appreciate it...


here is what I need it to do:

    check if I have internet connectivity... (I suppose it could be done by checking a ping response or something...?)
      if internet connection is present, than wait 1 hour,
      if no internet than execute:
        sudo eth0 down
        stop process amule
        stop process ktorrent
        sudo /etc/init.d/moblock-nfq restart
        wait 5 minutes
        sudo eth0 up
        resume process amule
        resume process ktorrent
        wait 1 hour and goto 1



I simply don't know how to put this into something that the comp will understand, hence I am asking for help here...


FYI, moblock is a program I use to block blacklisted IP ranges to protect my privacy while using P2P.. it is the linux equivalent of Peerguardian... however I am having a particular problem with it - it seems to restart on it's own every morning and for some reason this automated restart is never completed fully and causes my comp to loose all internet connectivity... the manual restart resolves all the problems in 99% of cases...
Windows is extremely fast after a fresh install. If you want to make it stay that way: - don't use it.
-Clem
User avatar
NiksaVel
Level 5
Level 5
 
Posts: 806
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia

Linux Mint is funded by ads and donations.
 

Re: Help me write a bash script

Postby scorp123 on Wed Aug 15, 2007 7:00 pm

NiksaVel wrote: check if I have internet connectivity... (I suppose it could be done by checking a ping response or something...?)
Not sure about that part. I'll have to check. Ping isn't reliable, so I suppose it would have to be a series of pings that fails before action is taken.

NiksaVel wrote: sudo eth0 down
Better + cleaner: sudo /etc/init.d/networking stop

NiksaVel wrote: stop process amule
stop process ktorrent
killall amule && killall ktorrent

NiksaVel wrote: resume process amule
resume process ktorrent
Hmmm ... why not put that into a VNC session that auto-executes those programs upon start-up? With this you'd also have the possibility to remotely connect and take a peek how your downloads are going ....

NiksaVel wrote: wait 1 hour and goto 1
Naaah, use a cron job that auto-executes once every hour. That would be much cleaner I guess.

NiksaVel wrote: FYI, moblock is a program I use to block blacklisted IP ranges to protect my privacy while using P2P.
I thought that you are in Croatia? Do they even have laws against P2P there? Because if the legal situation permits P2P (or doesn't really care about it .... yet) then you shouldn't have to bother about those peer blocking programs ... yet.
User avatar
scorp123
Level 8
Level 8
 
Posts: 2287
Joined: Sat Dec 02, 2006 4:19 pm
Location: Switzerland

Postby facade47 on Wed Aug 15, 2007 7:14 pm

well, I started working on it, but I have a question: are you wanting the torrents to pause, or to actually "kill" the amule and ktorrent programs? This does the latter:

Code: Select all
#!/bin/bash

while [ 1 ]; do

   # Get first two chars of the nslookup
   # If they are ";;", it means no internet connection
   nslookup=`nslookup www.google.com`
   internet=${nslookup:0:2}

   if [[ $internet == ";;" ]]; then
      sudo /etc/init.d/networking stop
      killall amule && killall ktorrent
      sudo /etc/init.d/moblock-nfq restart
      sleep 5m
      sudo eth0 up
      amule &
      ktorrent &
      sleep 1h
   else
      sleep 1h
   fi
done


EDIT: Used scorp's suggestion about sudo eth0 down.... I have no idea how to make/use a VNC session, and I'm not sure how to use cron either...
He will probably tear this script apart :P ... i'm pretty sure "while [ 1 ]" isn't good programming..
User avatar
facade47
Level 3
Level 3
 
Posts: 152
Joined: Fri Feb 16, 2007 4:46 am

Postby NiksaVel on Thu Aug 16, 2007 3:42 am

yes I'm in croatia, no laws yet :) call me prepared in advance :)

I did however start using moblock while I was still mass downloading from my university... they did have a security branch in the croatian academic and research network (CARNet) which was the ISP which started recieving infrigement reports from the RIAA and the likes which were directly forwarded to me as the administrator at the time... it was a bit of an eye opener :) so ... I want my privacy :)


I'm not sure what you mean with the VNC session? I have vino installed which I use to connect from outside...
Windows is extremely fast after a fresh install. If you want to make it stay that way: - don't use it.
-Clem
User avatar
NiksaVel
Level 5
Level 5
 
Posts: 806
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia

Postby scorp123 on Thu Aug 16, 2007 5:50 am

facade47 wrote: He will probably tear this script apart :P ... i'm pretty sure "while [ 1 ]" isn't good programming..
Nope :lol: ... as long as it gets the job done :wink: ... and sometimes that's all that counts :D

One minor correction though: Why "sudo eth0 up" (towards the end of the script)? You are not assigning an IP address and you completely stopped networking early on in the script, so this would probably not lead anywhere network-wise. So instead it should be: sudo /etc/init.d/networking start ... IMO. :wink:

Also ... the script would have to be run from within the GUI desktop session or else the attempt to launch the two GUI programs ktorrent and amule will end with an error message :wink:
User avatar
scorp123
Level 8
Level 8
 
Posts: 2287
Joined: Sat Dec 02, 2006 4:19 pm
Location: Switzerland

Postby NiksaVel on Thu Aug 16, 2007 11:10 am

okay... I'll give it a shot later on today...


one more thing... is there a way for the script to also do the following:

check wether ktorrent or amule or both are running, and than stop and later on start only the ones that were active at the start of the script... (sometimes I use only amule, sometimes ktorrent... so this way the script wouldn't have BOTH p2p progs active all the time and save resources)...


:D

I really appreciate the help guys :)
Windows is extremely fast after a fresh install. If you want to make it stay that way: - don't use it.
-Clem
User avatar
NiksaVel
Level 5
Level 5
 
Posts: 806
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia

Postby NiksaVel on Thu Aug 16, 2007 6:02 pm

one mroe thing... do I need to run this script as a sudo or regular... (since some of the commands are sudo)...=??
Windows is extremely fast after a fresh install. If you want to make it stay that way: - don't use it.
-Clem
User avatar
NiksaVel
Level 5
Level 5
 
Posts: 806
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia

Postby scorp123 on Fri Aug 17, 2007 4:16 am

NiksaVel wrote:one mroe thing... do I need to run this script as a sudo or regular... (since some of the commands are sudo)...=??
Oh damn. Of course. You are right :? The script won't do you any good if not run as root. Hmmm ... How about changing it a little? This version would have to run as 'root' inside your normal user's desktop:
Code: Select all
#!/bin/bash

while [ 1 ]; do

   # Get first two chars of the nslookup
   # If they are ";;", it means no internet connection
   nslookup=`nslookup www.google.com`
   internet=${nslookup:0:2}

   if [[ $internet == ";;" ]]; then
      /etc/init.d/networking stop
      killall amule && killall ktorrent
      /etc/init.d/moblock-nfq restart
      sleep 5m
      /etc/init.d/networking start
      sudo -u yournormaluseraccount amule &
      sudo -u yournormaluseraccount ktorrent &
      sleep 1h
   else
      sleep 1h
   fi
done
So this variation would need to run as 'root' (because we are touching the networking and you won't be there to enter the password ... so this needs to happen auto-magically). So you'd start your desktop, open a terminal, sudo su - into 'root' and then run this script above. It will then use 'sudo' to switch back into your account and launch your P2P apps for you. I think this might work ... Can you test it?
User avatar
scorp123
Level 8
Level 8
 
Posts: 2287
Joined: Sat Dec 02, 2006 4:19 pm
Location: Switzerland

Postby NiksaVel on Fri Aug 17, 2007 6:27 am

will try it later on today..


thanks again!
Windows is extremely fast after a fresh install. If you want to make it stay that way: - don't use it.
-Clem
User avatar
NiksaVel
Level 5
Level 5
 
Posts: 806
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia

Postby NiksaVel on Tue Aug 28, 2007 2:33 am

I finally got around to play with this script...

when I try to start it (I used sudo sh mobloscript.sh) I get the following:

8: Syntax error: Bad substitution


seems to be the problem with the $internet line... but I'm too dumb to figure it out, so .... HELP :D
Windows is extremely fast after a fresh install. If you want to make it stay that way: - don't use it.
-Clem
User avatar
NiksaVel
Level 5
Level 5
 
Posts: 806
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia

Postby Boo on Tue Aug 28, 2007 2:58 am

change

nslookup=`nslookup http://www.google.com`
internet=${nslookup:0:2}

to

test1=`nslookup http://www.google.com`
internet=${test1:0:2}

why?
nslookup:0:2
is trying run the nslookup command with the wrong syntax. :roll:

:D
Image
Now where was i going? Oh yes, crazy!
User avatar
Boo
Level 8
Level 8
 
Posts: 2364
Joined: Mon Mar 26, 2007 7:48 am

Postby NiksaVel on Tue Aug 28, 2007 4:01 am

hell... I did it and it's still the same error
Windows is extremely fast after a fresh install. If you want to make it stay that way: - don't use it.
-Clem
User avatar
NiksaVel
Level 5
Level 5
 
Posts: 806
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia

Postby Boo on Tue Aug 28, 2007 8:49 pm

it works for me so check your syntax of the script.

make sure ${test1:0:2} is NOT ${test1;0;2}

:D
Image
Now where was i going? Oh yes, crazy!
User avatar
Boo
Level 8
Level 8
 
Posts: 2364
Joined: Mon Mar 26, 2007 7:48 am

Postby NiksaVel on Wed Aug 29, 2007 3:32 am

I'm not very knowledgable with writing this stuff, as I've already said... but from what I see in code coloring thingie I have added for gedit, something is wrong with that line as {test1 is pink while the rest is normally colored (black text, red :).. if I leave a space between $ and the { than it colors normally... it even runs with different errors:


wait: 23: Illegal number: 2m
mobloscript.sh: 23: {test1:0:2}: not found
mobloscript.sh: 23: [[: not found
Windows is extremely fast after a fresh install. If you want to make it stay that way: - don't use it.
-Clem
User avatar
NiksaVel
Level 5
Level 5
 
Posts: 806
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia

Postby scorp123 on Wed Aug 29, 2007 1:14 pm

NiksaVel wrote:I'm not very knowledgable with writing this stuff
Why don't you just copy & paste stuff? You can copy & paste the code above ... or vice versa: Copy & paste the content of your editor so we can see what's wrong.
User avatar
scorp123
Level 8
Level 8
 
Posts: 2287
Joined: Sat Dec 02, 2006 4:19 pm
Location: Switzerland

Postby NiksaVel on Wed Aug 29, 2007 1:37 pm

here we go: :D

Code: Select all
#!/bin/bash

while [ 1 ]; do

   # Get first two chars of the nslookup
   # If they are ";;", it means no internet connection
   test1=`nslookup www.google.com`
   sleep 2m
   internet=${test1:0:2}

   if [[ $internet == ";;" ]]; then
      /etc/init.d/networking stop
#      killall amule && killall ktorrent
      /etc/init.d/moblock-nfq restart
      sleep 5m
      /etc/init.d/networking start
#      sudo -u yournormaluseraccount amule &
#      sudo -u yournormaluseraccount ktorrent &
      sleep 1h
   else
      sleep 1h
   fi
done
Windows is extremely fast after a fresh install. If you want to make it stay that way: - don't use it.
-Clem
User avatar
NiksaVel
Level 5
Level 5
 
Posts: 806
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia

Postby Boo on Thu Aug 30, 2007 12:00 am

I would put $internet in quotes and ${test1:0:2}
ie
internet="${nslookup:0:2}"

if [[ "$internet" == ";;" ]]; then

you don't need the sleep 2m

if that still fails add some echo for more output.
eg
test1=`nslookup http://www.google.com`
echo $test1
sleep 2m
internet=${test1:0:2}
echo $internet

and tell us what you get.

one other thing to check

ls -l /bin/bash

:D
Image
Now where was i going? Oh yes, crazy!
User avatar
Boo
Level 8
Level 8
 
Posts: 2364
Joined: Mon Mar 26, 2007 7:48 am

Linux Mint is funded by ads and donations.
 

Return to Other topics

Who is online

Users browsing this forum: No registered users and 5 guests