Linux Blog

Linux History Command

Filed under: General Linux,Linux Software — TheLinuxBlog.com at 5:00 am on Wednesday, November 17, 2010

linux find command

History is great. How does the saying go?

“Those who forget about history are doomed to repeat it?”

If that’s the saying I think it is more fitting to say that for those who forget the Linux History Command are doomed to repeat typing. A lot. Seriously, the history command can help you remember the exact Linux find command with the intricate search options you typed a while ago. It could help you open up your x2x or x2vnc sessions after a reboot. Who knows what you’ll use it for. All this comes at a little cost, you’ll have to know how to use it.

(Read on …)

Asterisk AGI IP Address lookup

Filed under: General Linux — TheLinuxBlog.com at 12:01 am on Sunday, September 19, 2010

While not exactly a shell script, I figured it would be worthy to post something rather than nothing and technically it is a script none-the-less.
I was experimenting with Asterisk AGI scripts and needed a project. I decided that an IP address lookup would be a good one. Sometimes my dynamic IP changes but my dynamic DNS doesn’t update. With this script, I should be able to dial into my Asterisk machine and get it to tell me the IP address. At least, that’s the plan.

(Read on …)

Making ISO’s with dd

Filed under: General Linux — TheLinuxBlog.com at 4:59 pm on Monday, July 26, 2010

Creating an ISO Image under Linux from the command line is a really easy process. Fire up your favorite terminal and type the following:

dd if=/dev/cdrom of=<iso image name>

The if is for input file and the of for output file. There are lots of options for dd so check out the man page.

To mount the newly created image (as root) you can mount it as a normal device with the -o loop option:

mount -o loop <iso image name> /mnt/<dest dir>

Remove lines that are in another file

Filed under: Shell Script Sundays — TheLinuxBlog.com at 6:52 pm on Sunday, February 7, 2010

Remove lines from a file that exist in another fileI had an issue this week where I needed to remove lines from one file if they existed in another file. Looking back it was frustrating as such a task should be simple.

I tried all sorts of things. Differencing the two files and using grep to grab the lines I wanted. Whatever I tried just did not produce the expected results. Thanks to a buddy I found the solution which ended up being to sort the two files before using diff.

Example:
Assuming two files exist, File_1 and File_2. File_1 containing lines with a, b, c and. File_2 containing b and d. If we want to remove b and d from File_1 because they exist in File_2 you could use something like the this:

owen@linuxblog:~$ cat File_1.txt
a
b
c
d
owen@linuxblog:~$ cat File_2.txt
b
d

owen@linuxblog:~$ diff File_1.txt File_2.txt | grep \< | cut -d \  -f 2
a
c

That’s all fine and dandy until File_2.txt contains the same lines in a different order. Running the same command produces different results. See Below:

owen@linuxblog:~$ cat File_2.txt
d
b

owen@linuxblog:~$ diff File_1.txt File_2.txt | grep \< | cut -d \  -f 2
a
b
c

The solution as noted above is to use sort before hand and then difference them:

owen@linuxblog:~$ sort File_1.txt >> File_1-sorted; sort File_2.txt >> File_2-sorted;
owen@linuxblog:~$ diff File_1-sorted File_2-sorted | grep \< | cut -d \  -f 2
a
c

Obviously the example has been simplified, when dealing with thousands of lines the sort could take a while. With that said I’m sure there are more efficient ways to achieve the same results. I wouldn’t doubt there being a command better suited to do this. Have at it in the comments.

Tether iPhone through the cable, on Linux

Filed under: General Linux — TheLinuxBlog.com at 10:02 am on Thursday, August 20, 2009

So you got an iPhone huh? perhaps you’ve got an SSH client and maybe you jailbroke your iPhone and have done some iPhone wifi tethering (That’s not the best way, and not free), but have you been able to use SSH through the cable? Me neither until a little while ago.

The benefits of connecting by the cable are slightly obvious to anyone who wants to do it:

BATTERY!
With your phone plugged in, you’ll get better battery life, or perhaps just prevent the battery from getting discharged any further.

No Wifi Setup
This was really the biggest problem for me, having to reconfigure wifi on my laptop then getting the laptop and phone to talk. Some times wireless connections mysteriously dropped or just acted plain funky. Any one who’s ever typed in a WEP key on the iPhone also knows what a pain it is, so not having to use Wifi is a plus.

Privacy
Since the cable is used, there should be no wireless network for others to fool around with.

Data Transfer Rates
I have not yet confirmed this, mostly because I’m lazy, partly because I don’t have the time to produce any stats but, 480Mbps with USB2.0 Vs. a theoretical 54Mbps on 802.11G

Enough already, how do I do it?
Thanks to Jing Su there is a LGPL’d piece of software called itunnel that can be found here. Installing itunnel will vary by distribution. It’s fairly straight forward on Fedora, provided you have the library libiphone installed you should be able to download, untar, make, and run.

Once you’ve installed itunnel, you run it by using:

sudo itunnel <port>

If you don’t specify a port it listens on port 3023.

Connect to localhost using your normal SSH Socks proxy method -D <port> and your username, default being mobile with your new port.

 ssh -D <yourport> -p 3023 mobile@localhost

Type “Yes” to accept the fingerprint (you may need to eventually change your fingerprint when you upgrade your phone) and then type in your password which should NOT be “alpine”, because you did change it right?

Once you’re SSH’d in, you can set up your browser to use the socks proxy as you would with Wifi tether version on the port you specified with -D.

There you have it, a way to Tether your iPhone through the cable on Linux! Rumor, (well not really a rumor, since I’ve used itunnel.exe) has it that this also works with a Windows box, just try to find a non-virus infected version of the iTunnel suite.

Happy Hacking!

Thunderbird localmail Spool

Filed under: Linux Software — TheLinuxBlog.com at 1:13 pm on Friday, July 3, 2009

Thunderbird

I was in a bit of a bind the other day when I learned that an IMAP server I was using was going to dissapear. I wanted to backup all of my mail, but had too many messages hosted on the IMAP server to copy from one to another, so I decided I’d download them all locally first and then deal with them later. I used fetchmail to download all of my messages from the IMAP main folder to my local spool, and copied over all of the sub folders because I was in a rush and needed to copy them quickly.

Once they were in my local mail spool, I wanted to get them into Thunderbird, but learned that the option I had once used to read my localmail had gone. There used to be an option for it in the GUI, but somewhere along the line it got removed. After a bit of Googling, I found: http://lists.freebsd.org/pipermail/freebsd-questions/2003-December/027652.html

This worked for me, and should work for any one that wants to use Thunderbird to read local mail.

Basically, you add a new mail account in Thunderbird as usual, then close it. Get into your local profile directory by using

cd .thunderbird/[tab]

then

vi prefs.js

find your new mail server, with the bogus name and change the hostname to localhost, change your name to <yourusername>@localhost, the server type to movemail, and change the userName to your username. It should look something like the following:

user_pref(“mail.server.server4.hostname”, “localhost”);
user_pref(“mail.server.server4.name”, “owen@localhost”);
user_pref(“mail.server.server4.type”, “movemail”);
user_pref(“mail.server.server4.userName”, “owen”);

Once thats done, you can restart Thunderbird and fetch your mail as usual. From there you can do as you wish with your messages.

Excellent! My question really is why was the GUI option removed from Thunderbird? Whatever the answer this method still works, so if you need to, use it while you still can!

Coppermine Photo gallery Upload Script

Filed under: Shell Script Sundays — TheLinuxBlog.com at 9:15 pm on Sunday, May 31, 2009

This week I bring you a script that I helped Kaleb (who has written posts here before) write. Well, I got him started with it, using curl and he rolled with it and finished it up. Here is the script:

#!/bin/bash
# Script to Upload to http://kporter.homelinux.com/~kaleb/cpg14x
# Written by Kaleb Porter May 23 2009
# with help of www.thelinuxblog.com
# email: porterboy55@gmail.com
# if you wish to use this code for something else please give me credit

IMAGE=”$1″
URL=”http://kporter.homelinux.com/~kaleb/cpg14x/upload.php”
DA=`date ‘+%d%b%y-%N’`
# If the user does not specify a file or url
if [ -z "$1" ]; then
echo “You did not give a file to upload”
echo “Takeing a screenshot in 3 Seconds…”
sleep 3
scrot $DA.png
IMAGE=$DA.png
fi
FI=`echo “$IMAGE” | grep ‘^[a-z]*://’`
AL=2
FIUP=`curl -s -F control=phase_1 -F blaa=continue -F file_upload_array[]=@$IMAGE $URL | grep unique_ID | awk -F\” ‘{print $6}’`
URLUP=`curl -s -F control=phase_1 -F blaa=continue -F URI_array[]=$IMAGE $URL | grep unique_ID | awk -F\” ‘{print $6}’`

#Get the title for the image from the user and change all the spaces to “%20″
echo “Enter a title for the image”
read TITLE1
TITLE=`echo $TITLE1 | sed ‘s/ /\%20/g’`

#Get the Description for the image from the user and change the spaces to “%20″
echo “Enter a discription”
read DES1
DES=`echo $DES1 | sed ‘s/ /\%20/g’`

#Get the keywords for the image from the user and change the spaces to “%20″
echo “Enter keywords (separated by spaces)”
read KEY1
KEY=`echo $KEY1 | sed ‘s/ /\%20/g’`

if [ -z "$FI" ]; then

UNIQUE_ID=$FIUP
#echo “Choose the NUMBER value for the album you want”
#curl -s -F control=phase_2 -F unique_ID=$UNIQUE_ID $URL | awk ‘/name=”album”/{disp=1} {if (disp==1) print} /<\/select>/{disp=0}’ | grep ‘value=”[0-9]“‘ | sed ‘s/<option//’ | sed ‘s/<\/option>//’ | sed ‘s/>//’
#read AL
curl -o /dev/null -d “control=phase_2&unique_ID=$UNIQUE_ID&album=$AL&title=$TITLE&caption=$DES&keywords=$KEY&blaa=continue” $URL
exit 0
else

# If the image is from a URL
UNIQUE_ID=$URLUP
#echo “Choose the NUMBER value for the album you want”
#curl -s -F control=phase_2 -F unique_ID=$UNIQUE_ID $URL | awk ‘/name=”album”/{disp=1} {if (disp==1) print} /<\/select>/{disp=0}’ | grep ‘value=”[0-9]“‘ | sed ‘s/<option//’ | sed ‘s/<\/option>//’ | sed ‘s/>//’
#read AL
curl -o /dev/null -d “control=phase_2&unique_ID=$UNIQUE_ID&album=$AL&title=$TITLE&caption=$DES&keywords=$KEY&blaa=continue” $URL
exit 0
fi

If there are any questions you can pretty much read the Shell Script Sundays column and figure out everything you need to know. Now that the upload script works, and tries to take a screenshot with scrot, the next step is a check to see if scrot exists, if it doesn’t a check for import, if not an error message.

It really does amaze me at the capabilities of the shell. Especially how mashable it is and how you can combine it with pretty much anything, this script is a great example of combining the power of the shell with the intrawebs. Well, I hoped you learned something, and as always if you have any questions, you know where the comment box is.

- Owen.

What groups am I in?

Filed under: Quick Linux Tutorials — TheLinuxBlog.com at 10:45 am on Tuesday, April 21, 2009

@nongeekboy on Twitter tweeted? about a blog post: Simple Script To List Groups in passwd File. I read it and have done something similar before so I figured I’d blog it here so I won’t have to write it again. Anyway, along with the point of this post, since this is suppose to be quick.

A question that is often asked is “What groups am I in?”

The easiest way to find out is to type the command: groups
This will give you a list of the groups you are in separated by a space. There are some other fancy ways of getting the groups but they rely on the `id` command. Running groups with no user name, its the same as running id -Gn.

Here are some other variations that you can try if you need to script the output:

groups
id
id -G
id -g
id -n
id -nG <user>
and the obvious:
id –help
info id

APC Access Temperature Query and Conversion. (2 of 2)

Filed under: Shell Script Sundays — TheLinuxBlog.com at 6:55 pm on Sunday, March 29, 2009

This second part of the script APC Access Temperature Query Script and its been a long time coming. Basically this script is the part that runs as a cron and will e-mail me if the temperature goes over a certain threshold. Once it returns to normal it e-mails me again. It has the option to send a text message to me via my SMS gateway, but it is commented out.

#!/bin/bash

temp=$(/home/linux/bin/temp f)
threshold=76

if [ "`echo \"$temp > $threshold\" | bc`" == 1 ]; then
echo $(date +%s) $temp >> /home/linux/thermal-over.log
echo “High Temp”;

if [ "$(cat temp.txt)" == "norm" ]; then
echo “Sending E-Mail, High Temp”;
echo “Current Temperature Is: $(/home/linux/bin/temp f)” | mail -s “Thermal Overload” owen@linuxblog                #echo “Current Temp Is: $(/home/linux/bin/temp f)” | mail -s “Thermal Overload” mynumber@cingularme.com
echo “high” > temp.txt
fi
elif [ "`echo \"$temp < $threshold\" | bc`" == 1 ]; then
echo “Low Temp”;

if [ "$(cat temp.txt)" == "high" ]; then
echo “Temp Resumed, Sending E-Mail”;
echo $(date +%s) Resumed at: $temp | mail -s “Thermal Normal” owen@linuxblog
echo “norm” > temp.txt
fi

fi

echo $(date +%s) $temp >> /home/linux/thermal.log

When I first wrote the script, I did not do any temperature checking. I found out that I needed to when I came back one morning with a bunch of emails that I needed to delete. Its pretty simple to figure out, temp.txt holds a value that is either norm or high. It gets switched when the temperature changes, this will in turn stop it from e-mailing me repeatedly. Once the temperature drops it flips it back. It will still e-mail if your temperature fluctuates between 75 and 77 which can be annoying, but you can adjust the threshold with the variable and set it to what you need. Thankfully our chiller has been fixed and I no longer have to worry about the temperature, but it still runs on a cron just in case.

Adjust sudo timeout

Filed under: Quick Linux Tutorials — TheLinuxBlog.com at 1:05 pm on Thursday, March 5, 2009

SudoI think its pretty evident that I love sudo right? Well, what I do not like about sudo is the timeout. I understand while its there but five minutes is not exactly what I’d call an overly generous time, especially when I’m parked here at my desk for hours upon end. This tutorial shows you the line you’ll need adjust the sudo timeout:

First as root you’ll want to get into the sudo file and edit it. I’m sure you know how to do this since you’ve probably already visudo’ed your way into using sudo and are now trying to adjust the timeout. For those just reading for the sake of it, you’ll do the command: visudo

Right, now you’re there, you’ll either be in nano, pico or vi depending on your distribution. Search for the Defaults section, and put

Defaults:<your username> timestamp_timeout=<your timeout>

Replace your username with yours. Change your timeout to the number of minutes, or -1 for unlimited per session. Save and quit, then exit. Try it again, then try it again after the sudo timeout you set has changed. If it works, great news if not double check your sudoers file for another Default property that may be acting up.

Use VNC through SSH

Filed under: Quick Linux Tutorials — TheLinuxBlog.com at 11:33 am on Thursday, November 20, 2008

Here is another quick tutorial;

Some times its nice to tunnel through SSH. Perhaps you have SSH running but the firewall does not allow anything but SSH in. You can tunnel VNC (or any other service) through SSH by doing the following:

On the machine local to you establish an SSH connection to the remote machine with “Local (-L)”  port forwarding. This may seem confusing and often confuses me, where <-p PORT> is optional

 ssh -L 5901:localhost:5900 username@HOST <-p PORT>

Once I have the connection established I can now use vncviewer to connect to my local host with the port specified

vncviewer  localhost:5901

Thats all there is to it, have fun!

Next Page »