Author Topic: Shell scripting  (Read 7316 times)

0 Members and 1 Guest are viewing this topic.

Offline Krazed

  • Moderator
  • Hero Member
  • *****
  • Posts: 1822
    • View Profile
Shell scripting
« on: August 29, 2007, 10:09:20 am »
I wrote this back in May of '05, and I've recently got back into linux/unix, and figured I'd get back into shell scripting.


Quote
I've recently, (today) gotten into shell scripting. I've written four scripts today, I'll post them here and let you guys discuss them.

We'll start with helloworld.sh.

Code:

Code: [Select]
#!/bin/bash

echo "Hello world bash script!"
echo "Writen by krazed of x86!"
echo "The first of many. :P"

#this script is pointless, shush.
#what directory are we in!?

pwd

#what's in this directory!?

ls

#and, what's the uptime on this boxs?

uptime

#how about the processes being ran?

ps -aux

#Okay, that's all for today, 'folks.
#Written by krazed of x86.
#E-Mail: krazedx86@gmail.com
#AIM: krazedx86


This is called cmdlisting.sh. It takes the contents of the command directorys and lists them into four different .txt files.

Code:

Code: [Select]
#!/bin/bash

echo "Command listing bash script."
echo "Purpose: Copy your command lists to three different text files."

# First, we start off with /usr/bin, the default commands.

ls /usr/bin > defaultcommands.txt
echo "Contents of /usr/bin written to defaultcommands.txt"
echo " "

# Okay, now we have copied the contents of /usr/bin into the file defaultcommands.txt, Next!
# Now, time for /bin, the system commands.

ls /bin > systemcommands.txt
echo "Contents of /bin written to systemcommands.txt"
echo " "

# Okay, contents of /bin are now in systemcommands.txt
# Let's move on to /usr/X11/bin, the X windows programs.

ls /usr/X11/bin > xwincommands.txt
echo "Contents of /usr/X11/bin written to xwincommands.txt"
echo " "

# Alright, last but not least, let's list your installed programs. /usr/local/bin

ls /usr/local/bin > installedprograms.txt
echo "Contents of /usr/local/bin written to installedprograms.txt"
echo " "
echo "Script executed, no errors, not bad for my second script ever, eh?"

# Okay, that's the script for yah.
# It's not too bad for my second script ever.
# This script written by krazed of the x86 crew.
# We're on IRC at irc.tehnetwork.org:6667 #x86.
# E-Mail: krazedx86@gmail.com AIM: krazedx86


And, this is the third one. This one is actually pretty nifty, it's me experimenting with ANSI color codes, and scripting in color.

Code:

Code: [Select]
#!/bin/bash

# A small shell script written so I could fuck around
# and get the hang of coding with colors in shell scripts.

# A full list of all the ANSI color codes.
#1m     -     Change text to hicolour (bold) mode
#4m     -        "    "   "  Underline (doesn't seem to work)
#5m     -        "    "   "  BLINK!!
#8m     -        "    "   "  Hidden (same colour as bg)
#30m    -        "    "   "  Black
#31m    -        "    "   "  Red
#32m    -        "    "   "  Green
#33m    -        "    "   "  Yellow
#34m    -        "    "   "  Blue
#35m    -        "    "   "  Magenta
#36m    -        "    "   "  Cyan
#37m    -        "    "   "  White
#40m    -     Change Background to Black
#41m    -        "       "      "  Red
#42m    -        "       "      "  Green
#43m    -        "       "      "  Yellow
#44m    -        "       "      "  Blue
#45m    -        "       "      "  Magenta
#46m    -        "       "      "  Cyan
#47m    -        "       "      "  White
#49m    -        "       "      "  Default background
#7m     -     Change to Black text on a White bg
#0m     -     Turn off all attributes.

echo -e "\x1B[34;49m This is blue text on default background."
echo -e "\x1B[32;49m This is green text on default background."
echo -e "\x1B[37;42m This is white text on green background."

# This script written by krazed of the x86 crew.
# We're on IRC at irc.tehnetwork.org:6667 #x86.
# E-Mail: krazedx86@gmail.com AIM: krazedx86


I re-wrote cmdlisting.sh so I could practice getting color codes down. So here it is, the new and improved, cmdlistingv2.

Code:

Code: [Select]
#!/bin/bash

echo -e "\x1B[34;49m" "Command listing bash script, version2. Rewritten 05/22/05."
echo -e "\x1B[34;49m" "Purpose: Copy your command lists to four different text files."
echo " "

# First, we start off with /usr/bin, the default commands.

ls /usr/bin > defaultcommands.txt
echo -e "\x1B[31;49m" "Contents of /usr/bin written to defaultcommands.txt"
echo " "

# Okay, now we have copied the contents of /usr/bin into the file defaultcommands.txt, Next!
# Now, time for /bin, the system commands.

ls /bin > systemcommands.txt
echo -e "\x1B[31;49m" "Contents of /bin written to systemcommands.txt"
echo " "

# Okay, contents of /bin are now in systemcommands.txt
# Let's move on to /usr/X11/bin, the X windows programs.

ls /usr/X11/bin > xwincommands.txt
echo -e "\x1B[31;49m" "Contents of /usr/X11/bin written to xwincommands.txt"
echo " "

# Alright, last but not least, let's list your installed programs. /usr/local/bin

ls /usr/local/bin > installedprograms.txt
echo -e "\x1B[31;49m" "Contents of /usr/local/bin written to installedprograms.txt"
echo " "
echo -e "\x1B[34;49m" "Script executed."
echo -e "\x1B[0m"

# Okay, that's the script for yah.
# This script written by krazed of the x86 crew.
# Script rewritten so I could fuck with color codes.
# We're on IRC at irc.tehnetwork.org:6667 #x86.
# E-Mail: krazedx86@gmail.com
It is good to be good, but it is better to be lucky.

Offline Krazed

  • Moderator
  • Hero Member
  • *****
  • Posts: 1822
    • View Profile
Re: Shell scripting
« Reply #1 on: August 29, 2007, 10:11:29 am »
This was a post tmp made, (tmp: come back and give me new challenges), which I plan to retry and finish within the week. Don't give me hints or tell me how.  :P

Quote
Challenge:

Using bash, make it so that when you boot into X Windows with your default user (krazed or whichever login you use) it always starts up a terminal (of your choice) that is running with UID 0 (root). This is assuming that your default user is not UID 0.

I used to have it so that whenever I booted X, xfterm4 popped up already tailing my logfiles (which required privs), so I've had to do this before. Let's see how similar our methodology is.

Let me know if you need any clarification/hints.
It is good to be good, but it is better to be lucky.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Shell scripting
« Reply #2 on: August 29, 2007, 10:24:52 am »
For the command listing one, there's a bit of a problem. Linux doesn't necessarily use /bin /usr/bin etc. for its binaries, that's simply a convention. To ensure you got everything, I recommend the following (plus, it's a good way to learn about looooping :) ) (note: untested):

Code: [Select]
#!/bin/bash

echo "" > commands.txt
for i in `echo $PATH | sed 's/:/ /g'`; do
 echo "Commands in $i:" >> commands.txt
 ls -l $i >> commands.txt
 echo "" >> commands.txt
done

Oh, and by the way, don't ever call x86 a "crew" :P

Offline Krazed

  • Moderator
  • Hero Member
  • *****
  • Posts: 1822
    • View Profile
Re: Shell scripting
« Reply #3 on: August 29, 2007, 10:31:15 am »
Haha, those days were the best.
It is good to be good, but it is better to be lucky.

Offline Newby

  • Moderator
  • Hero Member
  • *****
  • Posts: 10877
  • Thrash!
    • View Profile
Re: Shell scripting
« Reply #4 on: August 29, 2007, 02:27:05 pm »
I remember those. Heh.
- Newby
http://www.x86labs.org

Quote
[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

I'd bet that you're currently bloated like a water ballon on a hot summer's day.

That analogy doesn't even make sense.  Why would a water balloon be especially bloated on a hot summer's day? For your sake, I hope there wasn't too much logic testing on your LSAT. 

Offline mynameistmp

  • Moderator
  • Full Member
  • *****
  • Posts: 111
  • Hi! I'm new here!
    • View Profile
Re: Shell scripting
« Reply #5 on: September 01, 2007, 01:37:04 am »
This was a post tmp made, (tmp: come back and give me new challenges), which I plan to retry and finish within the week. Don't give me hints or tell me how.  :P

Quote
Challenge:

Using bash, make it so that when you boot into X Windows with your default user (krazed or whichever login you use) it always starts up a terminal (of your choice) that is running with UID 0 (root). This is assuming that your default user is not UID 0.

I used to have it so that whenever I booted X, xfterm4 popped up already tailing my logfiles (which required privs), so I've had to do this before. Let's see how similar our methodology is.

Let me know if you need any clarification/hints.

I'm curious to see what you come up with here. Nice work thus far. I'll spend some time this weekend trying to recall some cool tricks for you =)

Offline LordVader

  • Full Member
  • ***
  • Posts: 113
  • Knowledge is power.
    • View Profile
    • James Moss on the web!
Re: Shell scripting
« Reply #6 on: September 22, 2007, 07:49:06 am »
Adding users, this is ment for a linux system with a chroot environment but could be altered for other uses.
Code: [Select]
#!/bin/bash
# Script to add a user to Linux system
if [ $(id -u) -eq 0 ]; then
read -p 'Enter username : ' username
read -s -p 'Enter password : ' password
egrep ^À^Ü^$username^À^Ý /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
    echo ^À^Ü$username exists!^À^Ý
    exit 1
else
    pass=$(openssl passwd $password)
    useradd -s /bin/bash -m -d /home/chroot/./home/$username -p $pass -c $username -g users $username
    [ $? -eq 0 ] && echo ^À^ÜUser has been added to system!^À^Ý | grep /etc/passwd -e "^$username" >> /home/chroot/etc/passwd | mkdir -p /home/chroot/home/$username/public_html || echo ^À^ÜFailed to add a user!^À^Ý
    exit 1
fi
else
echo ^À^ÜOnly root may add a user to the system^À^Ý
exit 2
fi
Allows you to add a username, and password and create home dir + public_html dir automatically.
The "public_html" folder creation which is for apaches user directories to provide the new user with a web folder as well as chrooted ssh|sftp|sftp access.

Any suggestions to improve and clean this up are appreciated.

Update:
I am working on a full blown chroot automation script you can find it here
It is made to help automate and work with the openssh-server chroot tutorial for Debian etch located here from www.howtoforge.com.
As of sept. 23rd it is not finished but the parts that are finished work and everything is documented, check the readme.txt file for information, it's current status and a changlog if you are interested or curious.

As always feedback and suggestions is appreciated.
« Last Edit: September 23, 2007, 04:48:25 pm by LordVader »

Offline LordVader

  • Full Member
  • ***
  • Posts: 113
  • Knowledge is power.
    • View Profile
    • James Moss on the web!
Re: Shell scripting
« Reply #7 on: September 22, 2007, 08:04:26 am »
Also I was playing with a chroot attempting to add different tools to it, and ran into some snags with these:
man
java

Basically there are some dependancies to those that my create_chroot_env script fails to find and add, has anyone else tried to and been successfull @ adding those to a chroot environment?

You can see a similar example of the create_chroot_env script I am using in the middle of this article:
http://www.howtoforge.com/chroot_ssh_sftp_debian_etch

I am basically using that with some slight edits and adding more apps to the list.
It uses ldd to attempt to sort out dependancies and copy them into the chroot.
But sadly it fails to grab all the dependancies for java and some other things and im not sure why.
Possibly may need to do a: #strings <appname> | grep *.so or something to get a more complete depenancy listing?
Anyone with idea's to get a more full list of dependancies I would much appreciate the help.

Update:
For reference I have Sun Java SE 6 JDK/JRE installed, and I am not suffering from the problem listed here:
http://java.sun.com/javase/6/webnotes/#linux
getconf _NPROCESSORS_CONF returns 1 (one processor) in my chroot which is correct for that pc.
Also it appears all the key files are located in: /usr/lib/jvm
But even if you link or copy them all into the chroot java still have some issues finding everything.
java -jar responds if you do this where as java -version fails it could not find libjava.so.
« Last Edit: September 22, 2007, 10:44:16 am by LordVader »

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: Shell scripting
« Reply #8 on: September 22, 2007, 12:47:53 pm »
I've never had any issues using Java on my chrooted vhost for clanbnu.ws - I use the hosting service to run my Java bot.

Sometimes /proc doesn't mount properly, and that cripples what the vhost can do.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Offline LordVader

  • Full Member
  • ***
  • Posts: 113
  • Knowledge is power.
    • View Profile
    • James Moss on the web!
Re: Shell scripting
« Reply #9 on: September 22, 2007, 01:17:10 pm »
Java is running fine on the machine, im having problems getting the dependancies sorted out to copy into the chroot environment.
Same for man also, im fairly sure it may be a problem with the script im using either not properly finding all the dependencies or not adding them all(still playing/testing).

Outside the chroot all is fine, getting the libs and binary's in place so they can find one another inside the chroot is where the problem seems to be.
Smaller apps like wget/nano/vi etc all are working fine but some things seem to have deeper folder tree's and those seem to be the ones that are not working correctly.

Update:
I see one problem the script to create my chroot uses ldd on binary files to sort out dependancies, /usr/bin/java is a perl script so it doesn't return *.so files as results to copy in dependancies..  Using the perl script as a reference tho may give enuff info on what i need to track down :D
Quote from: the java perl script
# The real Java runtime:
my $javaRuntime = '/usr/bin/gij-4.1';

# The debian JNI module directory:
my $debianJNIDir = '/usr/lib/jni';
It's amazing what you find when you actually dig and look.
I'll post back a mini-how-to or something once I sort it all out.
« Last Edit: September 23, 2007, 11:46:45 am by LordVader »

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: Shell scripting
« Reply #10 on: September 25, 2007, 07:41:52 pm »
Are you the computer's administrator?

You could mount -o bind your lib folders to the chroot.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Offline LordVader

  • Full Member
  • ***
  • Posts: 113
  • Knowledge is power.
    • View Profile
    • James Moss on the web!
Re: Shell scripting
« Reply #11 on: September 25, 2007, 11:59:54 pm »
Yes is an old pc of mine, atm running debian linux and freebsd..
I'm doing this in debian atm, for a chrooted ssh environment trying to provide a isolated means to allow ssh logins and provide java apps like javaop2 or bnubot etc.
« Last Edit: September 26, 2007, 12:05:10 am by LordVader »

Offline pxc

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Shell scripting
« Reply #12 on: March 04, 2008, 01:59:10 am »
This was a post tmp made, (tmp: come back and give me new challenges), which I plan to retry and finish within the week. Don't give me hints or tell me how.  :P

Quote
Challenge:

Using bash, make it so that when you boot into X Windows with your default user (krazed or whichever login you use) it always starts up a terminal (of your choice) that is running with UID 0 (root). This is assuming that your default user is not UID 0.

I used to have it so that whenever I booted X, xfterm4 popped up already tailing my logfiles (which required privs), so I've had to do this before. Let's see how similar our methodology is.

Let me know if you need any clarification/hints.

Am I on the right track if I'm playing with xinitrc, some script I make up, and setuid?