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 read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
NiksaVel
Level 5
Level 5
Posts: 782
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia
Contact:

Help me write a bash script

Post by NiksaVel »

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...
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Windows is extremely fast after a fresh install. If you want to make it stay that way: - don't use it.
-Clem
scorp123
Level 8
Level 8
Posts: 2272
Joined: Sat Dec 02, 2006 4:19 pm
Location: Switzerland

Re: Help me write a bash script

Post by scorp123 »

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.
facade47

Post by facade47 »

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..
NiksaVel
Level 5
Level 5
Posts: 782
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia
Contact:

Post by NiksaVel »

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
scorp123
Level 8
Level 8
Posts: 2272
Joined: Sat Dec 02, 2006 4:19 pm
Location: Switzerland

Post by scorp123 »

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:
NiksaVel
Level 5
Level 5
Posts: 782
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia
Contact:

Post by NiksaVel »

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
NiksaVel
Level 5
Level 5
Posts: 782
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia
Contact:

Post by NiksaVel »

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
scorp123
Level 8
Level 8
Posts: 2272
Joined: Sat Dec 02, 2006 4:19 pm
Location: Switzerland

Post by scorp123 »

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?
NiksaVel
Level 5
Level 5
Posts: 782
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia
Contact:

Post by NiksaVel »

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
NiksaVel
Level 5
Level 5
Posts: 782
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia
Contact:

Post by NiksaVel »

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
Boo
Level 7
Level 7
Posts: 1633
Joined: Mon Mar 26, 2007 7:48 am

Post by Boo »

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!
NiksaVel
Level 5
Level 5
Posts: 782
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia
Contact:

Post by NiksaVel »

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
Boo
Level 7
Level 7
Posts: 1633
Joined: Mon Mar 26, 2007 7:48 am

Post by Boo »

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!
NiksaVel
Level 5
Level 5
Posts: 782
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia
Contact:

Post by NiksaVel »

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
scorp123
Level 8
Level 8
Posts: 2272
Joined: Sat Dec 02, 2006 4:19 pm
Location: Switzerland

Post by scorp123 »

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.
NiksaVel
Level 5
Level 5
Posts: 782
Joined: Wed Feb 28, 2007 4:06 am
Location: Croatia
Contact:

Post by NiksaVel »

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
Boo
Level 7
Level 7
Posts: 1633
Joined: Mon Mar 26, 2007 7:48 am

Post by Boo »

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!
Locked

Return to “Other topics”