Showing posts with label screen. Show all posts
Showing posts with label screen. Show all posts

Saturday, May 9, 2009

A Nice Linux ScreenSaver

Hey there,

Well, now that the sickness is abating, I'm back to working on the weekend ;)

Hope you enjoy this cool little screensaver script I found over at BloggEd

I included the basic code below, but you should check out the site for a good write-up and instruction on how to use and modify it for your system's kernel.

Enjoy :)



#!/bin/sh

file=phosphor-kernel-src.pl # script to create
linux_src_dir=/usr/src/linux # location of kernel src

cat << EOF > "$file"
#!/usr/bin/perl
srand;
rand(\$.) < 1 && (\$line = \$_) while <DATA>;
print \$line;
__DATA__
EOF
find -L "$linux_src_dir" -name \*.c >> $file
chmod 555 $file




, Mike




Discover the Free Ebook that shows you how to make 100% commissions on ClickBank!



Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Friday, October 31, 2008

11 Simple Ways To Recover Your Screen on Linux and Unix

Hey there,

If you're one of the many readers who submit a comment now and again (and even if you aren't ;), today's post is an homage to your helpful and insightful suggestions and tips. Some of you reading this right now may have emailed me with something that deserves to be posted as a comment to another post, and I can say, with great confidence, that it eventually will. I personally respond to all emails, so sometimes balancing that with work, family and more work can cause me to get a little behind. I'm still going to keep doing things this way, of course, not because I'm lazy or narrow-minded (okay, that's up for debate ;), but because I believe in actually responding to anyone who takes the time to send me a comment. And, just to clarify, this response-ethic of mine does not apply in any way shape or form to folks who write me one sentence vulgarities. If you want (or need) to hurl an insult my way, I strongly encourage it, but I probably won't write you back. Like with every other cycle, no matter who started it, someone has to end it :)

And, now, just to keep you on your toes, I'll include a hyperlink back to the original article on 9 simple ways to recover your screen in the same sentence that I let you know that this post is virtually the same material, only bigger, better and with extra sauce ;) Unless you want to read the rambling introduction (which is different in the original post), don't bother visiting the original page. All of the useful stuff from that post is included in today's version.

Once, again, a super-sized Thanks to everyone who wrote in with suggestions for this post and any other post. I truly appreciate your taking the time to write in and am almost always impressed, after a reading, with how much I don't know ;)

And, here we go again. Attribution for reader suggestions will accompany those points; otherwise, this post - from here on out - will be almost exactly the same as the last one.

Have a great weekend!

1. See if you can clear the screen the obvious way

host # clear

2. Or maybe resetting it might help

host # reset

3. stty is also usually available on most Linux and Unix distributions, and it has a sanity check built in. Worth a shot:

host # stty sane

4. tput is also fairly common. You can use it to clear your screen, or reset it, almost like the first two options did (just from a different angle):

host # tput clear

5. and...

host # tput reset

6. Or you can use it to re-initialize the screen (gross oversimplification, but you just want your screen back, right? ;)

host # tput init

7. Finally, you can try to get it to sort out the garbage on your screen by resetting the bold and underline definitions (this makes no sense to me at all, but it works sometimes;)

host # tput sgr 0 0

8. If your shell built-in echo can do this, it's fairly easy to take this route:

host # echo -e \033c

9. And, if it doesn't (or you just can't tell because your characters are still all goofed), you can type the control characters directly:

host # echo ^[c <-- Which, if you don't want to check out the link above, translates to typing: echo and then ctl-v followed by hitting the escape key and then typing the regular letter c

10. Suggested by Matthew Lenz! Here's a very simple way to get back your screen. Why does it work? I have no idea, but I tested it and it does on every machine I could try it out on :

host # cat /bin/cat

11. Suggested by James Ranks! One of these control key combinations will almost always work for you and remove the garbage from your screen (see point 9 for a link back on how to really represent control characters in Linux or Unix) :

host # ctrl-v followed by ctl-n

or

host # ctrl-v followed by ctl-o

Hopefully this little cheat sheet will come in handy at some time. You'll most likely be typing "in the dark."

Cheers,

, Mike



zcat made this comment regarding the post. It illustrates a good point: If things get too hairy, you have to know when to just bail and fix your terminal the old-fashioned way :)

I've catted plenty of binary files in the past and never needed anything
beyond 'reset' to recover from it. Perhaps I just got lucky.

But if I ever did need more than a reset, I think I would probably just
log out and back in at that point; the advantage of using a short
username like 'zcat' (and on the hosts where it's my full name I use
key-based authentication so I never have to type it)







Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Friday, August 22, 2008

9 Simple Ways To Recover Your Screen on Linux and Unix

Hey there,

Wow, it's been a long work week. Figuratively, of course. The official version is that it was a standard 5 days ;) Tonight's post is a bit on the lighter side (both the material covered and the overall tone), as I haven't had much time to write this week. It's like my wife says; I should take some of the 1500 word rants I put down and split them up every once in a while. She's a smart lady. One of these days I'll start listening to her and take it easy on my keyboard ;)

Hopefully, today's title didn't give anyone the wrong impression. If you've gone and thrown your monitor off the balcony in a fit of rage, this post can't help you, except to offer the following advice: Go to the store and buy a buy a new one (they're much cheaper now than when you may have bought yours, which will help offset the counseling, therapy and court-ordered community service expenses ;) Then make a solemn resolution. Promise yourself that, the next time you work all night on a complex program that, even to you, barely makes any sense by the time you've accidentally deleted random chunks of it before remembering to write the buffer to disk, you will take 5 seconds, walk away, huff in a deep deep breath, count to 10 and then smash it with a brick, instead. You'll be as happy as you were last time, but you probably won't get arrested ;)

If you're a pragmatist, take a moment to reflect on the fact that (although this is a very interesting phenomena that I experience from time to time), even though you desperately want to smash your monitor into bits, it has absolutely nothing to do with the problem. For instance, I often whack my monitor on the side when Windows takes 45 minutes to boot up. I should be smacking the OS install disks, or the computer itself. If I was in full possession of my faculties (or, at least, my sense of reason) I would be kicking my own arse for expecting anything more ;)

Alas, enough pontificating. To get your weekend started off right, here are 9 ways you can, hopefully, get your terminal output back to normal if you ever cat a binary executable or do anything else that makes it all turn "Chinese" on you. Enjoy and have a safe weekend :)

Start out with the classics (NOTE: All examples will be given as command line mock-ups which end in a carriage return or enter-key press - I'm assuming you'll be typing blind)

1. See if you can clear the screen the obvious way

host # clear

2. Or maybe resetting it might help

host # reset

3. stty is also usually available on most Linux and Unix distributions, and it has a sanity check built in. Worth a shot:

host # stty sane

4. tput is also fairly common. You can use it to clear your screen, or reset it, almost like the first two options did (just from a different angle):

host # tput clear

5. and...

host # tput reset

6. Or you can use it to re-initialize the screen (gross oversimplification, but you just want your screen back, right? ;)

host # tput init

7. Finally, you can try to get it to sort out the garbage on your screen by resetting the bold and underline definitions (this makes no sense to me at all, but it works sometimes;)

host # tput sgr 0 0

8. If your shell built-in echo can do this, it's fairly easy to take this route:

host # echo -e \033c

9. And, if it doesn't (or you just can't tell because your characters are still all goofed), you can type the control characters directly:

host # echo ^[c <-- Which, if you don't want to check out the link above, translates to typing: echo and then ctl-v followed by hitting the escape key and then typing the regular letter c

Hopefully this little cheat sheet will come in handy at some time. You'll most likely be typing "in the dark." If none of these options work, I usually give in and just disconnect and re-login (It's amazing how much work goes into being lazy ;)

Cheers,

, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Friday, July 11, 2008

Using Screen, Script, Mkfifo And Redirection To Watch Or Log User Sessions

Hey there,

Today we're going to continue, in a sense, where we left off on yesterday's post regarding using mkfifo for user monitoring. In this post, we're going to look at that a bit more specifically, and also touch on some other ways you can do the same thing (if not better). Everything we'll be going over today should be either freeware or come with your OS (or both :)

Again, please check our older posts on executing new file descriptors and shell redirection if you need that part of the process fleshed-out more. We're going to take it as a given that you basically get how that works (This is the biggest hazard of writing a tech blog for everyone. Sometimes it's hard to explain the basics and not insult the experienced user and/or explain a more advanced concept while not seeming to talk down to the new user. Whatever I write here; no offense meant to any reader. Except a few of you. You know who you are ;)

Now, we'll get to the meat and go after logging and/or monitoring user sessions. You have various options, depending on what you want to do. Personally, I think it's cool to be able to watch someone's process in real-time, but sometimes (especially if you want to make sure you're always keeping what you're seeing) its better to dump output to files and just cat or tail those in almost-real-time. If being a few usec's behind the user you're monitoring has the power to ruin your entire day, please stop reading now. This will end badly no matter what ;)

1. Using mkfifo and enhanced profile security: This can be fun, and it has the potential for being miserable, depending on the OS you're running. For instance, Solaris' standard "script" command only has one flag (-a, to append to a previous script file) and is notorious for rarely flushing the buffer (so you'll get 50 pages of user messages at once and then nothing for 20 minutes) and, often times, will clip the information sent through a FIFO if the session ends before it flushes for the first time, leaving you with incomplete output!

The first thing you'll want to do is modify your users' .profile (or appropriate shell settings file) to be secured as if the account was su-only. If your user can modify the file after you've made your changes, you can bet they'll remove the script line as soon as possible. If you want to meet your users half-way, insist on a 750 root:root owned .profile and have the last line source in another file that they can use to make their own customizations, as deemed appropriate or possible. Here's what I'd put in somebody's .profile to create a FIFO and start script running when they login and pipe to that FIFO (Note that, although script normally makes it so you have to exit twice to log out - because it opens up in a subshell - using it in the .profile makes it so that when the user exits once, the connection is terminated!) :

tempvar=$$
user="`logname`.$tempvar"
myfifo=/tmp/$user
/usr/bin/rm -rf $myfifo
/usr/bin/bash -isc "/usr/bin/script $myfifo"


Then, as soon as the user logs in, you can have an automated process begin reading from the named pipe (/tmp/username.112327, for instance) and either begin writing it to the screen or dumping it to a file, or both. If you want to read the output to the screen you're on, just do:

host # cat /tmp/username.112327

or

host # tail -f /tmp/username.112327

to dump off the output into a log, just do:

host # cat /tmp/username.112327 >/tmp/log

to do both, just do something like:

host # cat /tmp/username.112327 2>&1|tee /var/logdir/log

For my money, like I said, you're better off just dumping to log. You won't have to worry about catching logins with another process and you can always read the log later (or watch it in real time). If you decide that's the best option, change that section of your users' .profile to read like this instead:

tempvar=$$
user="`logname`.$tempvar"
myfifo=/tmp/$user
/usr/bin/rm -rf $myfifo
/usr/bin/bash -isc "/usr/bin/script $myfifo"
cat $myfifo >/var/logdir/$user.log 2>&1 &


2. Using screen and enhanced config file security: Gnu Screen is another alternative for root (or users of similar privilege) to keep an eye on users. The rules for securing screens configuration files, are the same as those we went over regarding su-only accounts, just applied to different files. The theme is the same: Make the files usable, but not modifiable, by the user.

Screen is a bit easier to set up for ethical-hijacking. All you need to do is modify the users' "screenrc" file, make it so they can't modify it and add the lines:

multiuser on
acladd root


and, of course, set up their profiles to launch screen at login. With these conditions in place, all you need to do, as root, to watch a user session is to type the following at the command line:

host # screen -x user

If you're not sure who's on, or one user has multiple screens open:

host # screen -x

with no specific argument should let you know :) You may then, as with script, decide that you're better off dumping to a logfile and not constantly monitoring (or using resources to monitor). With screen, you don't need to jump through any gigantic hoops (making FIFO's etc, unless you want to ;) If your original entry in the users' .profile was:

screen

all you would have to do to change it to log the output to a file, as well as the screen, would be to change that to:

screen -L PickANameForALog <-- or just a number if you want to accept the default screenfile.NUMBER output

3. Wrapping up - Big differences between "script" implementations:

I just thought I'd end on this note, since it's crazy, and I can't tell you for sure what to expect from "script" between distro's. The implementation of this command is wildly different. For instance, as I mentioned above, Solaris' "script" only has a "-a" option to append to an existing script output file. BSD's "script" command (and most Linux distro's, it seems) have a lot of other useful options that you can use to make your life (and doing this sort of thing) easier. Here are the additional flags that I know of:

-c COMMAND <-- this will run a command, which would make it so you could run bash from script directly rather than having to re-invoke bash and call script from there.

-f &;lt-- This output will flush the buffer after every read/write. So, unlike the Solaris "script," you can actually get your results back in real-time. Very helpful if you need to watch a user's activity.

-q <-- Be "extremely" verbose... Just kidding ;) This tells script to not announce itself starting and stopping. This might prevent certain users from fighting you over this from day-one!

-t <-- This tells script to output timing data. It's run on an existing scriptfile and spits its output to STDERR. So, if you wanted to capture it in a file, something like this would do:

host # script -t 2>myNewTimeLog MyOriginalOutput

and then you can use that file to read your original "script" output back at relatively the same speed at which it was captured, using the "scriptreplay" command, like this:

host # scriptreplay MyNewTimeLog MyOriginalOutput

If you've got an hour's worth of output and don't want to go through the hassle of removing all the control characters, this is a great help. If you just cat the original scriptfile, it'll run through itself in under a minute and may cause seizures ;)

Now you should be all set to start monitoring users' activities and output. If your company wants to purchase a commercial product instead, you can always put that on your resume!

Hope this has been a helpful, if not enjoyable, diversion :)

Cheers,

, Mike

Tuesday, April 1, 2008

Script For A Simple Menu Using Tput And ANSI Color

Simple Menu With An Option Selected

Click above to see a larger example of what happens when you dare to select option 1 ;)

Hey there,

Today I thought we'd look at something that almost all admins have to do at some point or another; create a simple shell menu script for users who aren't Unix or Linux savvy, but need to make use of the systems directly, nonetheless.

To spice it up (a little. There's only so much you can do ;) I've used colors within the standard shell menu framework, sticking with the ECMA compliant ANSI color codes. The reason I choose to stand by these color codes (since they, admittedly, lack the range of colors available in different flavors of Linux and Unix) is that they're virtually guaranteed to work on "all" flavors of Linux and Unix. Or maybe I'm just a people pleaser who hasn't realized that he can't win 'em all yet ;)

You'll notice also that, rather than just throw the menu together, using "case" inside a "select" loop, I went out of my way to complicate things by using tput to manage the cursor on the screen. In this case, it actually adds some functionality to the menu that would, otherwise, be impossible to implement. I shied away from unnecessarily using "getopts" when our "case" statement can handle these simple menu arguments just as efficiently :).

You can run this menu very simply, from the command line, like so:

host # ./SimpleMenu.sh

And there are only a few things to really take note of today. In a future post, I'll devote more time to utilizing your terminal screen with tput. It can be used to do a lot more useful, and entertaining, things than what we're having it do today.

Basically, in quick sequential order, we're using tput to:

1. Make the cursor invisible : tput civis
2. Position the cursor at various places on the terminal: tput cup x y <--- With x and y being single numeric coordinates designating the row and column, respectively.
3. Save the current cursor position: tput sc
4. Get back to (recover) the previously saved cursor position: tput rc
5. Make everything go back to the way it was, just in case our screen gets screwed up: tput reset

You can also, very simply, modify this shell menu script by just changing the menu options and adding your own routines to the case loop. And, if you can find something less offensive to your sensibilities, please do change the color scheme ;)

Enjoy :)


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/bash
#
# SimpleMenu.sh - Only SomeWhat Useful ;)
#
# 2008 - Mike Golvach - eggi@comcast.net
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

trap 'tput reset;exit 1' 1 2 3 9 15

tput civis

while :
do
echo -e "\033[44;37;22m"
clear
echo -e "\033[41;37"

echo -ne "\033[46;30m"
tput cup 8 25 ; echo -n "The Linux and Unix Menagerie"
tput cup 9 25 ; echo -n " Shell Account Services "
tput cup 10 25 ; echo -n " W E L C O M E "
echo -e "\033[40;32m"

tput cup 12 25 ; echo -n " 1. See Who's Logged In "
tput cup 13 25 ; echo -n " 2. Reboot The Server "
tput cup 14 25 ; echo -n " 3. Gain Root Access "
tput cup 15 25 ; echo -n " 4. Call Your Mother "
tput cup 16 25 ; echo -n " 5. Quit "

echo -ne "\033[41;30m"
tput cup 18 28 ; echo -n " Pick Your Poison: "
tput cup 18 48
tput sc

read y
tput cup 20 30

case "$y" in
1|S|s)
output=`w|awk '{print $1}' |xargs -ibing grep bing /etc/passwd|awk -F":" '{print $5}'`
starting_line=21
tput cup $starting_line 28;
echo -ne "\033[46;30mCurrently Logged In Users:"
echo -ne "\033[0m"
echo "$output"|while read x
do
let starting_line=$starting_line+1
tput cup $starting_line 28;
echo -n $x
done
tput rc
echo -n "Any Key To Menu"
read x
;;
2|R|r)
tput cup $starting_line 28;
echo -n "Option Not Active Yet"
tput rc
echo -n "Any Key To Menu"
read x
;;
3|G|g)
tput cup $starting_line 28;
echo -n "Option Not Active Yet"
tput rc
echo -n "Any Key To Menu"
read x
;;
4|C|c)
tput cup $starting_line 28;
echo -n "Option Not Active Yet"
tput rc
echo -n "Any Key To Menu"
read x
;;
5|Q|q)
tput reset
clear
exit
;;
*)
tput cup 20 28
echo "$y - Unknown Option"
tput rc
echo -n "Any Key To Menu"
read x
;;
esac
done

, Mike




Tuesday, December 11, 2007

What To Do When nohup Hangs Up Anyway - Down and Dirty

nohup is one of those commands pretty much everyone who uses Unix knows about. And for good reason. If you don't have access to a system console, or some other direct connection to a machine at work, it makes sense that you'd want to avail yourself of any utility that makes it possible for you to begin running a command through your terminal software, shut down your PC and go home. Even people who've never thought of this before will find this command almost immediately after typing something like "./sqlplus @extremelyLongDatabaseQuery" 5 minutes before quitting time ;)

nohup is the natural end-point of most searches since it's been around so long that it's actually a built-in in certain shells.

nohup stands for, literally, "no hangup." This, in layman's terms (and terms that make more grammatical sense), means that any command, run by nohup, will ignore the hangup signal (or SIGHUP) which is issued when a user disconnects from his or her pseudo-terminal (pty). Anyway; that's what it's supposed to do. In reality, I'd give it about a 50% reliability rating. One of the complaints I hear the most on the job is that "such and such" a command was run with nohup, the user disconnected and the process never completed. It, in plain fact, stopped running as soon as the user logged out; which sent the hangup signal. Yes, nohup is specifically designed to ignore that (nohup /sqlplus@extremelyLongDatabaseQuery really works in theory ;)

Alternatives to this command exist, of course. On my ratings scale, from worst to best, you've got (to my knowledge):

1. nohup <-- Included as an alternative only to hammer-home how much heartache this command causes me on a regular basis.
2. disown -h JOBID <-- This is available in bash, and exists as a way to block SIGHUP on an existing job. Your JOBID can be found, while still in your shell, by typing: jobs (If your job is listed as number 2, you can then run "disown -h %2" - In fact, you don't really need to include the "-h" flag. The outcome is pretty much the same, since disown'ing the job removes it from the job table)
3. setsid <-- Not always available. I haven't used it frequently enough to know exactly what distro's it comes standard with, but I've run it enough to know that it does run commands in a separate session (not attached to your pty) which will keep your command running after you log out.
4. GNU Screen <--- This is "almost" the best because you can run a process in one screen, disattach it, log out, and that same screen can either be accessed by you at a later point, or by any other user with access, from a different pty at any point in time before its conclusion.
5. The Down and Dirty Method <--- Explained below.

The Down and Dirty Method isn't an industry term; just what I like to call doing what most of the above mentioned programs are built to do in a way that, in my experience, is more consistently effective. In order to ensure that my programs keep running after I log off (if I happen to not have access to a console), I do the following: background the command I want to run and then execute that backgrounded command in a subshell. This probably sounds more complicated than it actually is. For example; to background a command, instead of typing:

./command

you would type

./command &

The ampersand character runs the job for you in the background. You can verify that it's still running by typing jobs at the command line.

To run a command in a subshell (in sh, ksh and bash, for example), you just need to surround it with parentheses, like so:

(./command)

You won't be able to verify that this job is running by typing jobs, but (assuming it has a long enough run-time or you're an incredibly fast typist) you can see it in the output of "ps -ef." You should note that, when you execute a command like this, the ps output will show your user id as the process owner.

Now, to make use of the Down and Dirty Method, you just need to type the following amalgam of the above concepts:

(./command &)

And that's it. You've backgrounded the command, by following it with an ampersand, and executed it in a subshell by surrounding it with parentheses. The key reason this process almost always works is the way in which the shell processes your request. First, it invokes the subshell in which to execute your command. Once in the subshell, it executes the command and processes the ampersand, which instructs it to background the process. Since the process you're executing is being run in a subshell, when the subshell backgrounds it, the process disconnects itself from your pty. You can also verify this in "ps -ef" output; noting that the user 1 (init) is listed as the processes owner. It's not going anywhere until it's finished. Unless you have the privilege to kill the process.

Hope this helps you out in some form or fashion. Even if we agree to disagree on what works best, it's always good to have alternatives :)

, Mike