1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
|
<!--startcut ==========================================================-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>More 2 Cent Tips & Tricks Issue 16</title>
</head>
<BODY BGCOLOR="#EEE1CC" TEXT="#000000" LINK="#0000FF" VLINK="#0020F0"
ALINK="#FF0000" >
<!--endcut ============================================================-->
<H4>"Linux Gazette...<I>making Linux just a little more fun!</I>
"</H4>
<P> <hr> <P>
<!-- QUICK TIPS SECTION ================================================== -->
<center>
<H1><A NAME="tips"><IMG ALIGN=MIDDLE ALT="" SRC="../gx/twocent.gif">
More 2¢ Tips!</A></H1> <BR>
Send Linux Tips and Tricks to <A HREF="mailto:gazette@ssc.com">
gazette@ssc.com
</A></center>
<p><hr><p>
<H3>Contents:</H3>
<ul>
<li><a HREF="./lg_tips16.html#ftp">How to ftp Back Home</a>
<li><a HREF="./lg_tips16.html#root">Checking if You're Boot</a>
<li><a HREF="./lg_tips16.html#xv">XV vs Xli</a>
<li><a HREF="./lg_tips16.html#bash">Bash Shell Scripting</a>
<ul>
<li><a HREF="./lg_tips16.html#scr1">Bash Shell Script 1</a>
<li><a HREF="./lg_tips16.html#scr2">Bash Shell Script 2</a>
<li><a HREF="./lg_tips16.html#scr3">Bash Shell Script 3</a>
</ul>
</ul>
<P> <hr> <P>
<!--================================================================-->
<a name="ftp"></a>
<H3><IMG ALIGN=BOTTOM ALT="" SRC="../gx/lil2cent.gif">
How to ftp Back Home
</H3>
<p>
Date:Sat Mar 30 14:23:24 (PST)<BR>
From:Phil Hughes<a href="mailto:phil@ssc.com">phil@ssc.com</a><P>
<p>
Many businesses place a firewall between the Internet and the inside
systems.
This is good protection and it just makes good sense.
One common firewalling technique is to serverly restrict access through
the firewall from the outside but allow a user on the inside to do most
anything through the firewall to the outside.
<p>
When I am at home, I routinely need to move files between home and work.
But, because of the firewall, I can ftp from work to home but not the
other way around.
What this means is that I need to establish an interactive connection
(using ssh) from home to work and then initiate the ftp from work to home.
<p>
So far, so good.
But, what I call "home" consists of various locations, all connected with
a dial-up connection through one of four ISPs.
All four ISPs use dynamic IP addresses meaning that each time I connect I
have a different IP address for my home system.
Even though the ISP knows what the current IP address for my system, the
name server at work doesn't.
<p>
The solution is to enter the IP address of my home system into the ftp
command at work.
First, I need to find out what the IP address is.
To do that, I execute the ifconfig command on my home system:
<pre>
$ /sbin/ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Bcast:127.255.255.255 Mask:255.0.0.0
UP BROADCAST LOOPBACK RUNNING MTU:2000 Metric:1
RX packets:19 errors:0 dropped:0 overruns:0
TX packets:19 errors:0 dropped:0 overruns:0
eth0 Link encap:10Mbps Ethernet HWaddr 02:60:8C:8F:A2:08
inet addr:198.186.207.131 Bcast:198.186.207.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:969719 errors:0 dropped:0 overruns:0
TX packets:971132 errors:0 dropped:0 overruns:0
Interrupt:9 Base address:0x280 Memory:d8000-da000
ppp0 Link encap:Point-Point Protocol
inet addr:206.125.79.118 P-t-P:204.157.220.30 Mask:255.255.255.0
UP POINTOPOINT RUNNING MTU:296 Metric:1
RX packets:5434 errors:0 dropped:0 overruns:0
TX packets:5545 errors:0 dropped:0 overruns:0
$
</pre>
The inet addr for the ppp0 interface (206.125.79.118) is the number I
need.
Now, on my system at work I enter:
<pre>
$ ftp 206.125.79.118
</pre>
ftp then prompts for a login and password.
I enter my standard login and password for my home system and ftp is up
and running.
<P> <hr> <P>
<!--================================================================-->
<a name="root"></a>
<H3><IMG ALIGN=BOTTOM ALT="" SRC="../gx/lil2cent.gif">
Checking if You're Root
</H3>
<P>
Date: Sun Mar 23 23:20:51 1997 (PST)<BR>
From: Kevin Lyda <a href="mailto:kevin@faxint.com">kevin@faxint.com</a> <BR>
<P>
<p>In the march gazette raul miller suggested that the most portable way to
test if you're root is [ -w / ]. that won't work if you're root file
system is read only. [ -w /var ] might be a better method.
<p>
kevin
<P> <hr> <P>
<!--================================================================-->
<a name="xv"></a>
<H3><IMG ALIGN=BOTTOM ALT="" SRC="../gx/lil2cent.gif">
XV vs Xli
</H3>
<P>
Date:Wed Mar 5 16:32:49 1997(PST)<BR>
From:Michael Hammel, <a
href="mailto:mjhammel@emass.com">mjhammel@emass.com</a><BR>
<p>I wasn't aware of Xli (rather, I haven't looked at it), however
your statement that xv can only tile image on the background. xv
allows qutie a bit of command line control. I use the following to
put up a background image at work (non-tiled, takes up the whole
background):
<pre><p>xv -root -max -quit /export/home/mjhammel/lib/images/emass3.tga
</pre>
<p>The initial image is 601x339, with a root display of 1152x900. Since
the original image is 24bpp the enlargement is very accurate in
details.
<p>
Michael J. Hammel
<P> <hr> <P>
<!--================================================================-->
<a name="bash"></a>
<H3><IMG ALIGN=BOTTOM ALT="" SRC="../gx/lil2cent.gif">
Bash Shell Scripting
</H3>
<P>
Date:Thu Mar 20 12:22;34 1997(PST)<BR>
From:Paul Sephton <a href="mailto:paul@inet.co.ca"> paul@inet.co.ca</a><BR>
<p>I have been enjoying the fruits of the Linux Gazette for a number of
years now. Recently, I had one of my users accidentally type rm *>bak,
and immediately noticed something was amiss by the incoherent screams
eminating from her office.
<p>In an attempt to ensure this would not have the same disasterous effect
again, and to protect my eardrums in future, I spent a couple of days
excersising my bash shell scripting skills, and came up with what I
believe to be a decent mechanism for maintaining versioned backups.
<p>My attitude with regard to the normal cludges like aliasing rm and so
on, is that it will not protect you against other programs which unlink
files. (To date I have yet to write a C program that shells rm in order
to unlink a file :)
<p>Whilst writing the set of three scripts, it dawned on me that although
some more complex tools do exist which perform the same sort of
function, the Linux community might be interested in what I did.
<p>Although it's not much more than a creative excersise in the use of the
'find' command, and suffers from the usual limitation of being
restricted to the one file system, I include the three scripts for your
perusal and possible inclusion in the gazette at your discretion.
<p>Don't hesitate to contact me if you need more information.
<p>Kind regards, and many thanks for the gazette.<BR>
Paul Sephton
<P> <hr> <P>
<!--================================================================-->
<a name="scr1"></a>
<H3><IMG ALIGN=BOTTOM ALT="" SRC="../gx/lil2cent.gif">
Bash Shell Script 1
</H3>
<pre>#!/bin/sh
if [ -z "$SAFEDEL" ];then
SAFEDEL=/u/safedel
fi
NDAYS=5 #Erase files after 2 days
MAXVER=6 # Start Overwriting versins at this count
BINDIR=$SAFEDEL/bin # Binaries directory
DATADIR=$SAFEDEL/data # Where links are to go
LOGFILE=$BINDIR/safedel.log # Output messages go here
ERRLOG=$BINDIR/safedel.err # Error output messages go here
DIRLIST=$BINDIR/safedel.dirs # List of directories found here
LOCKFILE=$BINDIR/safedel.lock # Lockfile to prevent re-entry
# Process the file $1 by creating a symbolic link in the data directory
# and an entry for the file in the index.
process-file()
{
SRC=`dirname $1`
FNAME=`basename $1`
VERSION=0
if [ ! -d $DATADIR$SRC ]; then
mkdir -p $DATADIR$SRC
# OWNER=`find -name $SRC -printf "%u"`
# chown $OWNER $FNAME:$VERSION
fi
cd $DATADIR$SRC
while [ -f $FNAME:$VERSION ]; do
VERSION=$[ $VERSION + 1 ]
done
if ! ln $1 $FNAME:$VERSION 2>> $LOGFILE; then
echo "Could not link file $FNAME:$VERSION" >> $LOGFILE
return
fi
echo -e "Linked $FNAME:$VERSION \t \tin $SRC" >> $LOGFILE
return
}
# Erase a file
erase-file()
{
echo "Unlinking $1 $2" >> $LOGFILE
rm -f $1
FN=`echo $1 | cut -f 1 -d ':'`
if ! { echo "$ERASED" | grep "$FN" - } ; then
ERASED="$ERASED $FN"
fi
return
}
# We want the version numbers to follow on each other, so that the next
# file we create gets a bigger version number. This makes sure they follow.
reorganise()
{
if [ -z $1 ]; then
return
fi
FN=$1
FILE_LIST=`ls $FN:* | sort -n -t: -k2`
if [ "$FILE_LIST" = ":*" ]; then
echo "All [$FN:*] files erased" >> $LOGFILE
return
fi
echo -e "File list to be moved is:\n$FILE_LIST" >>$LOGFILE
VERSION=0
for FNAME in $FILE_LIST; do
if [ "$FNAME" != "$FN:$VERSION" ]; then
echo "Moving $FNAME $FN:$VERSION" >>$LOGFILE
mv $FNAME "$FN:$VERSION"
VERSION=$[ $VERSION + 1 ]
fi
done
}
# The main shell script starts here...
cd $BINDIR
if [ -f $LOCKFILE ]; then
exit 0
fi
touch $LOCKFILE
date >> $LOGFILE
cat $DIRLIST |
(
while read SRC ; do
if [ `echo $SRC | cut -b 1` != "#" ]; then
echo "Finding files in $SRC" >> $LOGFILE
echo "Point 1 ($SRC)"
for FNAME in `find $SRC -type f -xdev -links 1 -print`; do
process-file $FNAME
done
fi
done
ERASED=""
echo "Point 2"
for FNAME in `find $DATADIR -type f -links 1 -ctime $NDAYS -print`; do
erase-file $FNAME "(older than $NDAYS days)"
done
echo "Point 3"
for FNAME in `find $DATADIR -type f -name "*:$MAXVER" -print`; do
FN=`echo $FNAME | cut -f 1 -d ':'`
erase-file $FN:0 "Too many versions (VERSION > $MAXVER)"
done
echo "Point 4"
for FNAME in "$ERASED"; do
reorganise $FNAME
done
) 2> $ERRLOG > /dev/null
rm -f $LOCKFILE
</PRE>
<P> <hr> <P>
<!--================================================================-->
<a name="scr2"></a>
<H3><IMG ALIGN=BOTTOM ALT="" SRC="../gx/lil2cent.gif">
Bash Shell Script 2
</H3>
<PRE>
#!/bin/sh
CURRDIR=`pwd`/
if [ -z $SAFEDEL ]; then
SAFEDEL=/u/safedel
fi
DATADIR=$SAFEDEL/data
BINDIR=$SAFEDEL/bin
cd $DATADIR$CURRDIR
if [ -z "$1" ]; then
echo
echo "Restores files unintentionally deleted"
echo
echo "Useage <salvage <filename>[:version] [dest]> from within the directory"
echo " in which the file was deleted."
echo
echo "The following is a list of your backed up files and their versions:"
echo " Salvageable Files:"
find . -xdev -type f -maxdepth 1 -links 1 -printf "%P\n" | column
echo " Files Currently in Use:"
find . -xdev -type f -maxdepth 1 -not -links 1 -printf "%P\n" | column
else
FN=`echo "$1:end" | cut -f 1 -d ':'`
VER=`echo "$1:end" | cut -f 2 -d ':'`
EXIST=`find $CURRDIR -name "$FN"`
# echo "[$EXIST]"
if [ -n "$EXIST" ]; then
echo "Incorrect file specification: File(s) are not deleted. ($FN)"
exit 0
fi
if [ "$VER" = "end" -o "$VER" = "*" ]; then
VER=""
fi
FILE_LIST=`find . -name "$FN:*" -printf "%f "`
FLIST=""
# echo "FILE_LIST is $FILE_LIST"
for FNAME in $FILE_LIST; do
FN=`echo "$FNAME:end" | cut -f 1 -d ':'`
FOUND=0
# echo "Looking for [$FN] in [$FLIST]"
for F in $FLIST; do
if [ "$F" = "$FN" ]; then
FOUND=1
fi
done
if [ "$FOUND" = "0" ]; then
FLIST="$FLIST $FN"
fi
done
# echo "FLIST is $FLIST"
for FNAME in $FLIST; do
if [ -z "$VER" ]; then
VERSION=0
NEXTVER=1
while [ -f $FNAME:$NEXTVER ]; do
VERSION=$NEXTVER
NEXTVER=$[ $NEXTVER + 1 ]
done
else
VERSION=$VER
fi
if [ ! -f $FNAME:$VERSION ]; then
echo "File $FNAME:$VERSION not found"
exit 0
fi
if [ -z "$2" ]; then
DEST=$CURRDIR$FNAME
else
DEST=$CURRDIR$2
fi
if ln $FNAME:$VERSION $DEST 2> /dev/null; then
echo "File $FNAME:$VERSION successfully recovered"
else
echo "Cannot link $FNAME:$VERSION to $DEST"
fi
done
fi
</PRE>
<P> <hr> <P>
<!--================================================================-->
<a name="scr3"></a>
<H3><IMG ALIGN=BOTTOM ALT="" SRC="../gx/lil2cent.gif">
Bash Shell Script 3
</H3>
<PRE>
#!/bin/sh
if [ -z $SAFEDEL ]; then
SAFEDEL=/u/safedel
fi
BINDIR=$SAFEDEL/bin # Binaries directory
DATADIR=$SAFEDEL/data # Where links are to go
# Erase a file
reorganise()
{
if [ -z $1 ]; then
return
fi
FN=`echo "$1:end" | cut -f 1 -d ':'`
FILE_LIST=`ls $FN:* | sort -n -t: -k2`
if [ "$FILE_LIST" = ":*" ]; then
echo "All [$FN:*] files erased"
return
fi
# echo -e "File list to be moved is:\n$FILE_LIST"
VERSION=0
for FNAME in $FILE_LIST; do
if [ "$FNAME" != "$FN:$VERSION" ]; then
echo "Moving $FNAME $FN:$VERSION"
mv $FNAME "$FN:$VERSION"
VERSION=$[ $VERSION + 1 ]
fi
done
}
# The main shell script starts here...
CURRDIR=`pwd`/
echo "Safedel: Purging extra versions in $CURRDIR"
cd $BINDIR
find $DATADIR$CURRDIR -type f -maxdepth 1 -links 1 -exec rm {} \;
for FNAME in `find $DATADIR$CURRDIR -type f -maxdepth 1 -print`; do
reorganise $FNAME
done
</pre>
<P> <hr> <P>
<!--================================================================-->
<center>Published in Linux Gazette Issue 16, April 1997</center>
<P> <HR> <P>
<!-- ============================================================== -->
<A HREF="./lg_toc16.html"><IMG SRC="../gx/indexnew.gif" ALT="[ TABLE OF
CONTENTS ]"></A>
<A HREF="../index.html"><IMG SRC="../gx/homenew.gif" ALT="[ FRONT
PAGE ]"></A>
<A HREF="lg_mail16.html"><IMG SRC="../gx/back2.gif" ALT=" Back "></A>
<A HREF="lg_bytes16.html"><IMG SRC="../gx/fwd.gif" ALT=" Next "></A>
<P><HR><P>
<h5>This page maintained by the Assistant Editor of <I>Linux Gazette</I>,
<A HREF="mailto: gazette@ssc.com">gazette@ssc.com</A><BR>
Copyright © 1997 Specialized Systems Consultants, Inc. </H5>
<P>
<!--startcut ==========================================================-->
</BODY>
</HTML>
<!--endcut ============================================================-->
|