Operating System - HP-UX
1844557 Members
3079 Online
110233 Solutions
New Discussion

Re: Share your one liner scripts

 
Craig A. Sharp
Super Advisor

Share your one liner scripts

Share your system administration one liners.

Perl, sh, ksh, csh, etc....
22 REPLIES 22
Craig A. Sharp
Super Advisor

Re: Share your one liner scripts

I will get it started. I recently saw this one. Can't remember which posting but thanks to the person who wrote it.

Mirror a set of logical volumes to a single mirror disk:

vgdisplay -v /dev/vgxx | grep "LV Name" | awk '{print $3}'|while read v;do;lvextend -m 1 $v /dev/dsk/cxtxdx;done
G. Vrijhoeven
Honored Contributor

Re: Share your one liner scripts

Kent Ostby
Honored Contributor

Re: Share your one liner scripts

Looking for processes chewing up CPU:

ps -ef | cut -c42-80 | sort -nr | head

Create a list of files not created in May (substitute as needed) to remove from a given directory:

ll | grep -v " May " | awk '{print "rm ",$NF)' > useme

Then execute the file "useme".

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Geoff Wild
Honored Contributor

Re: Share your one liner scripts

Version of sendmail:

echo \$Z | /usr/sbin/sendmail -bt -d


Identify and display stale extends on all system volume groups:

vgdisplay -v | awk '/LV Name/ { print $3 }' | xargs lvdisplay -v | grep -i -e "lv name" -e "lv status" -e stale -e '?'

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Roberto Polli
Trusted Contributor

Re: Share your one liner scripts

Used to timestamp FlexLM log files

cat license.log|perl -ne 's/\(tring-to-purge\)//;if ($_ =~ /TIMESTAMP (.*)/) { $ts=$1;} else { /license_name/ && print "$t $_";} '|less

Peace, R>
Tom Smith_9
Frequent Advisor

Re: Share your one liner scripts

This one will show you the top 10 consumers of memory.

UNIX95= ps -eo vsz,ruser,pid,args |sort -rn | head -10
Mel Burslan
Honored Contributor

Re: Share your one liner scripts

big stick script especially useful in an environment with lot of careless developers :

ps -ef | grep username | grep -v grep | awk {'print $2'} | xargs kill

(optional -9 to kill command on second attempt)
________________________________
UNIX because I majored in cryptology...
Lee Hundley
Valued Contributor

Re: Share your one liner scripts

Lizard poison :)

kill -9 $(ps -aex | grep mozilla | grep -v grep | awk '{print $1}')
It is my firm belief that it is a mistake to hold any firm beliefs
Charlie Rubeor
Frequent Advisor

Re: Share your one liner scripts

UNIX95= ps -eo etime,user,pid,ppid,cpu,stime,tty,time,args | awk '$1 ~ /^[0-9][0-9]:/'

will list all of the processes started in the last 24 hours that are still running. This comes in handy in identifying stuck processes, before the user calls and complains.
Michael D'Aulerio
Regular Advisor

Re: Share your one liner scripts

Here's a challenge for all the gurus out there. I have a .cshrc (which sources other files) that sets up an environment. Does anyone know of a way to generate an equivalent .profile file for sh or ksh that will set up the same environment?
Email: michael.n.daulerio@lmco.com
Francisco J. Soler
Honored Contributor

Re: Share your one liner scripts

Hi Michael, to source files from .profile or ksh you can use the dot command,

in csh source file.sh
in ksh . file.sh (dot space file)

Frank.
Linux?. Yes, of course.
Michael D'Aulerio
Regular Advisor

Re: Share your one liner scripts

Thanks Frank. What I was actually looking for was a way to convert a .cshrc file into an equivalent .profile. Change all the setenv to export, convert the if statements, fix the variable assignments. I want to duplicate the environment set up when csh starts up in scripts that run sh and ksh.
Email: michael.n.daulerio@lmco.com
Michael Tully
Honored Contributor

Re: Share your one liner scripts

Simple script that reads the sulog and reports all unsuccessful attempts to gain access to root.

$ cat /var/adm/sulog | awk '$4 == "-" {print $0}' | grep 'root$'
Anyone for a Mutiny ?
Bill Hassell
Honored Contributor

Re: Share your one liner scripts

Here's a big secret about ps and grep: they don't work very well!!! Instead, use ps to do all the work.

Here are some better solutions:

ps -ef | grep username | grep -v grep | awk {'print $2'} | xargs kill

is much more accurate when written:

ps -f -u username | awk '{print $1}' | xargs kill

The first script could be VERY dangerous because grep doesn't care what it matches. If the username is ash, then the first form kills all copies of bash (every user) as well as the unhasdaemon (not good!) and so on. The second form only returns processes used by a specific (and exact match) username.

And the other example will kill every process owned by mozilla9 as well as the mozilla process. Rather than:

kill -9 $(ps -aex | grep mozilla | grep -v grep | awk '{print $1}')

Use this:

kill $(UNIX95= ps -C mozilla -o pid | grep -v PID)

In this case, grep gets rid of the column title line PID and leaves zero or more process ID numbers. ps looks at the exact process name, so -C sh will not match bash or ksh.


Bill Hassell, sysadmin
Mike Stroyan
Honored Contributor

Re: Share your one liner scripts

Translating from csh to sh is awfully open-ended.
You can run an actual csh to ask it to report the current environment that .cshrc will setup.
This one-liner puts the csh environment into a sh script that it sources into the current sh.

csh -c env | sed "s/^/export /;s/=/='/;s/$/'/" > .csh_env; . .csh_env

Mike Stroyan
Honored Contributor

Re: Share your one liner scripts

Bill,

You don't need to grep away the PID header.
You can use "pid=" to ask ps to not print any header.
(Mozilla is usually seen as mozilla-bin.)

kill $(UNIX95= ps -C mozilla-bin -o pid=)

What do people have against lizards anyway?
Marvin Strong
Honored Contributor

Re: Share your one liner scripts

well let me try to break away from the kill stuff.

# replace PAT1 with PAT2 in file
perl -p -e 's/PAT1/PAT2/' file

# replace PAT1 with PAT2 in file and make original file.bak
perl -pi.bak -e 's/PAT1/PAT2/' file


Marvin Strong
Honored Contributor

Re: Share your one liner scripts

opps first on from above should be

perl -pi -e 's?PAT1/PAT2/ file

without the i it just goes to STDOUT
Rich Wright
Trusted Contributor

Re: Share your one liner scripts

Here is one I use quite a bit to find an unused minor number for a new vg.

ll /dev/*/group | sort -b -k6,6
Sundar_7
Honored Contributor

Re: Share your one liner scripts

To find out the process using a particular file

fuser /var/adm/syslog/syslog.log 2>/dev/null | xargs -n1 ps -fp
Learn What to do ,How to do and more importantly When to do ?
Dani Seely
Valued Contributor

Re: Share your one liner scripts

You asked for SA one-liners, this is a popular one-liner that I created and have set up to run incrementally throughout the day, via cron. Worried about users (or careless SAs) giving full permissions to files/directories that could result in possible file/system compromise:

nice find / -perm -o+w -exec ls -l {} \; -exec chmod o-w {} \; | tee -a /tmp/wwf.out

Note, performing a find from the root (/) level can take awhile and chew up resources ... so, I use nice to share the system processor resources.

Also, dumping the output to a file so that we can go back and determine the ownership and locations of the files and talk with the file owner and provide him/her with a security dissertation.
Together We Stand!
Abdul Rahiman
Esteemed Contributor

Re: Share your one liner scripts

Following one liner will give a sorted list of filesystems which are above a certain threashold level eg: 75%. And you could exclude the static filesystems with exclude options..

Pls. note that this was out of Tru64/AIX, you may need to adjust the field positions to filter the HP-UX's bdf output.

df -k | sort -r -k 5 |egrep -v "exclude1|exclude2|exclude3|..|.." |awk 'split($4,a,"%") && a[1] > 75 {print $7, $4}'
No unix, no fun