Posts tagged bash
Auto Start your WiFi on Raspberry Pi
7So I just got my new Hawking HWUN3 USB wireless N adapter that I purchased for my Raspberry Pi, and wanted it to connect on boot being I only access it via SSH and to plug it in to a wired adapter in order to start the wireless connection so that I can unplug it just didn’t make sense. So here’s how I got it installed and starting on boot.
This network adapter is perfect, I didn’t need to compile a thing, it was recognized right out of the box. LOVE IT!
I ran ifconfig and it was right there, happy and waiting.
[codesyntax lang="bash" lines="no" title="ifconfig output"]
wlan0 Link encap:Ethernet HWaddr 00:0e:3b:1e:b8:4d
inet addr:192.168.0.51 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:464 errors:0 dropped:0 overruns:0 frame:0
TX packets:267 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:68999 (67.3 KiB) TX bytes:40100 (39.1 KiB)
[/codesyntax]
Then you’ll need to do a little bash scripting, you can create a script in “/etc/init.d/” and then run “update-rc.d scriptname defaults” to install it, however I decided to instead just add it to the end of “/etc/rc.local” as this will run after everything else has already loaded after a reboot. Of course you can have init start your script at the end of the boot cycle (or anywhere in between) but the rc.local just takes less planning.
By default the “/etc/rc.local” file does nothing, but I left the existing code in and just commented it out. You can do what you like of course. Here is the contents of my rc.local file
[codesyntax lang="bash" lines="normal" title="rc.local"]
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will “exit 0″ on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
## Print the IP address
#_IP=$(hostname -I) || true
#if [ "$_IP" ]; then
# printf “My IP address is %s\n” “$_IP”
#fi
echo “Starting WiFi…”
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf
sleep .5s
dhclient wlan0
echo “WiFi should be started”
exit 0
[/codesyntax]
The code doesn’t actually do anything until line 18. On line 19 you’ll see that we’re starting the wpa_supplicant in the background (-B) for interface wlan0 (-i wlan0) using the configuration file found at “/etc/wpa_supplicant.conf” (-c)
We will need to create that file. Also I should note, wpasupplicant was already installed on my image, however if you need it installed, just issue the following command.
[codesyntax lang="bash" lines="no" title="install wpa supplicant"]
|
1 |
sudo apt-get install wpasupplicant |
[/codesyntax]
now let’s create the ever important wpa_supplicant.conf file. This will hold your ssid name and pre-shared key (password) of course replace YOURSSID and YOURPASSWORD with your own info respectively.
[codesyntax lang="bash" lines="fancy" title="create wpa_supplicant.conf file"]
|
1 |
sudo nano /etc/wpa_supplicant.conf |
[/codesyntax]
and the contents of that file should look something like this:
[codesyntax lang="bash" lines="fancy" title="wpa_supplicant.conf"]
|
1 2 3 4 |
network={ ssid="YOURSSID" psk="YOURPASSWORD" } |
[/codesyntax]
That’s it! You should be connected! Go ahead and reboot, and then run iwconfig and you should be connected to your designated access point, all without using the GUI!
Here’s a pic of my pi, with the usb stick, and some ram heat sinks I picked up at Frys for like $10 ( and I still have 6 more lying around now.. guess I’ll need more pi!)
And here’s a screen dump showing it recognized by my pi.
[codesyntax lang="text" lines="fancy" title="pi screendump"]
Using username “pi”.
pi@192.168.0.51′s password:
Linux raspberrypi 3.1.9+ #168 PREEMPT Sat Jul 14 18:56:31 BST 2012 armv6l
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Type ‘startx’ to launch a graphical session
Last login: Wed Jul 25 19:45:17 2012 from 192.168.0.119
pi@raspberrypi ~ $ iwconfig
lo no wireless extensions.
eth0 no wireless extensions.
wlan0 IEEE 802.11bgn ESSID:”editedformysaftey”
Mode:Managed Frequency:2.452 GHz Access Point: BC:C5:C3:6C
Bit Rate=65 Mb/s Tx-Power=20 dBm
Retry long limit:7 RTS thr:off Fragment thr:off
Power Management:on
Link Quality=70/70 Signal level=-37 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:47 Invalid misc:3 Missed beacon:0
pi@raspberrypi ~ $ ifconfig
eth0 Link encap:Ethernet HWaddr b8:27:16:60
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1104 (1.0 KiB) TX bytes:1104 (1.0 KiB)
wlan0 Link encap:Ethernet HWaddr 00:3b:1e:b8
inet addr:192.168.0.51 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:464 errors:0 dropped:0 overruns:0 frame:0
TX packets:267 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:68999 (67.3 KiB) TX bytes:40100 (39.1 KiB)
pi@raspberrypi ~ $ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 0e66:0013 Hawking Technologies HWUN3 Hi-Gain Wireless-N Adapter [Ralink RT3070]
pi@raspberrypi ~ $
[/codesyntax]
One thing to note, your network led’s wont be working, because your not using your nic…. You’ll want to watch the led on the actual usb stick to see if there’s activity. If it’s up and online, the light will be steady green, and only flash with transmissions. If it’s constantly off and only blinks every few seconds, something’s wrong and you’ll need to do some troubleshooting.
Hope this helps someone, and if not, at least I can forget about how I did it until my next pi comes in a few more days!
rsync with ReadyNAS and debian
0So this post is more for me and my reproducing it when my hard drive eventually fails, on how to get the rsync setup and working with my Netgear readyNAS.
First make sure you have rsync installed, which of course you do.
Then test the commands to make sure they are working.
[codesyntax lang="bash" doclinks="0"]
|
1 |
rsync -nav --progress /var/www/ 192.168.0.221::backup/rsync/www/ |
[/codesyntax]
and
[codesyntax lang="bash" doclinks="0"]
|
1 |
rsync -av /var/backup/ 192.168.0.221::backup/rsync/ |
[/codesyntax]
Respectfully. Remember this is just a test, the final commands will not have the “n,v, or –progress” commands in them instead replacing it with the “q” (or quiet) option to limit the amount of chatter going to /dev/nul (you’ll see later). Also the -a option stands for archive (archive mode; equals -rlptgoD (no -H,-A,-X)
Now let’s create the bash script to be called by a cron job as root.
Being I’m using a switch and only going over my local network I’m not bothering with the overhead of doing this over ssh, because really, if you already have access to my network, your in too deep for me to stop you any more then I’ve tried. Also there are a ton of guides on using rsync over ssh, less so for just using good ol rsync by itself.
Obviously you don’t want to have these running over eachother so first do the database’s and then the htdocs directories. as I find I have much more volume in the htdocs folder then the databases.
The way bash works is you start the *.sh file with the comment #!/bin/bash and then just type in the command you want it to execute on the next line.
I’m making two files, one for each command shown above. The finished product should look something like this. My Database backups live in /var/backup/ and are being moved over to the netgear’s “backup” share in the “rsync” folder. I’ve named this file db2rsync.sh
[codesyntax lang="bash"]
|
1 2 |
#!/bin/bash rsync -aq /var/backup/ 192.168.0.221::backup/rsync/ |
[/codesyntax]
And similarly I’ve named this next file as www2rsync.sh
[codesyntax lang="bash"]
|
1 2 |
#!/bin/bash rsync -aq /var/www/ 192.168.0.221::backup/rsync/www/ |
[/codesyntax]
Make these two files executable and then move them into the /root folder (or somewhere owned by root)
[codesyntax lang="bash"]
|
1 2 |
sudo chmod +x db2rsync.sh www2rsync.sh sudo cp *.sh /root/ |
[/codesyntax]
and let’s move on to getting the crontab entries sorted.
As ROOT! (sudo su) type in
[codesyntax lang="bash"]
|
1 |
crontab -e |
[/codesyntax]
and you’ll have a list (or not) of items in your crontab entries. The format is explained millions of places around the web so I won’t go into that. I’ll just say I want these to run around 4 in the morning, daily.
Create 2 new entries (one per line) and one for each script we are going to be calling. (a total of only 2 lines)
[codesyntax lang="bash"]
|
1 2 |
0 4 * * * /root/db2rsync.sh > /dev/null 30 4 * * * /root/www2rsync.sh > /dev/null |
[/codesyntax]
In this example the file db2rsync.sh will be called every day at exactly 4am and will only alert me of errors, and the www2rsync.sh file will run at 4:30am, also only alerting me if there is an error.
That’s it! Save your crontab file and it should reload making those changes happen as you schedualled them! If you see
[codesyntax lang="bash"]
|
1 |
crontab: installing new crontab |
[/codesyntax]
then your in good shape!
Also I’d recommend running the first rsync transfer using the –progress command manually to make sure everything transfers fine and your rsync settings are working.
Hope this helps someone other then just me but even still, it’s handy to have around!
Surfraw Makes google faster! (Linux)
3Here’s another quick shortcut for you commandline lovers out there!
First you need to install surfraw. (all of these commands are of course entered in your favorite terminal, be it terminator, guake or the like)
|
1 |
sudo apt-get install surfraw |
now we need to create an alias for it in our .bashrc file
|
1 |
gedit .bashrc |
under your alias section (towards the bottom) enter in the following
|
1 |
alias google='surfraw google -results=100 $1' |
now you can simply reload your terminal window (to enable the new settings) and enter in “google whatever you’re looking for” and it will bring up your default browser with the google results already on display! Sweet!
This also works with several other sites such as google, altavista, babelfish, dejanews, freshmeat, research index, slashdot and many others. Enjoy!
Spellcheck using the commandline
3If your anything like myself, I find I use google as a spellchecker more often then anything else, however having to wait for firefox to load when I’m trying to type in irc and don’t know the spelling of a word takes up alot of my time and slows the conversation down as well.
A great post from Stormdragon has shown me a great and easy way to do this using a program called hunspell.
Following most of his steps you can do this as well on any debian / Ubuntu / Linux Mint pc.
First install hunspell and it’s english library.
|
1 |
sudo apt-get install hunspell hunspell-en-us |
then cd over to your ~/bin/ directory
|
1 |
cd ~/bin/ |
create and edit a file named “spellcheck”
|
1 2 |
sudo touch spellcheck sudo gedit spellcheck |
Enter in the following script
|
1 2 3 |
#!/bin/bash echo “$@” | hunspell exit 0 |
then save and close gedit.
Now we need to make the file executable
|
1 |
sudo chmod 755 spellcheck |
Then just close and reopen your terminal of choice (I prefer terminator myself) and try out your new spellchecker!
|
1 |
spellcheck whatever you cant spell |
Correctly spelled words will just respond with an “*” if a word is misspelled it will offer you suggestions! Just that easy! Yea BASH!
tweet from bash a better alternative!
4TSo yesterday I spent a good while looking for options that didn’t require me to memorize a huge long string just to post to twitter, and I thought I had found one until I realized that nope, that only works if your in the same bash session and won’t still be there after a reboot or even just a new terminal window.
So trial and error and I have the solution!
in a terminal window go to /usr/local/bin
[codesyntax lang="bash" lines="no"]
|
1 |
cd /usr/local/bin |
[/codesyntax]
then make a file called twitter
[codesyntax lang="bash" lines="no"]
touch twitter
[/codesyntax]
then open it
[codesyntax lang="bash" lines="no"]
nano twitter
[/codesyntax]
and add this command replacing username and password with YOUR twitter account info
echo $@|tr ' ' '+'” “http://twitter.com/statuses/update.json”

