Archive

Posts Tagged ‘bash’

Looping over a range in bash

April 7th, 2009

I don’t know why I always forget how to do ranges, but I do. I guess it has something to do with the fact that I don’t expect it to be like C at all, and I don’t need to use it often enough to remember. I always assume it to be some oddball syntax like x in range 2..6 or something.

Do this:

for ((x=2;x<=6;x++)); do echo $x; done

And the output will be:

2
3
4
5
6

Almost random bash, for, loop, range, scripting, shell, syntax

Command line shortcut #977 - shell auto-completion

February 18th, 2009

I was trying emerge app-emulation/vmware-server and kept getting crazy GPL errors, so I needed to reboot to try a few different kernels i’ve compiled to see if one of them has the right options vmware is begging for. Every time I open a new shell, I need to export my proxy settings (I know I can set it in my profile, but I didn’t really have the system completely configured and I wasn’t planning on keeping it behind this proxy for long so I didn’t want to make it perm.) … I’ll skip the tab completion because if you haven’t run into that at least by accident, there’s something wrong with you. You can even do that from XP’s fake dos, although it is pretty useless they way they implemented it.

Instead of typing the export string out each time and the emerge line too, I just recalled them from the history knowing that the last set of commands that started with those characters were the commands I wanted.

jen2# !exp
export http_proxy=”http://10.0.0.99:8002
jen2# !em
emerge -av vmware-server

If there were several commands like that recently, or I wasnt’ sure which was the last one, I could look at the history and figure out which number to run, and run that instead:

jen2# !315
export http_proxy=”http://10.0.0.99:8002
jen2# !318
emerge -av vmware-server

If you didn’t know about these handy shortcuts, now you know, and knowing is have the battle. So when is that movie coming out anyway!!?

ooo! Shiny... auto-completion, bash, command line, emerge, export, handy, history, proxy, shortcut

shc compiler to compile bash shell scripts into binary

January 8th, 2009

shc is the only tool i’ve found that will compile scripts so idle hands won’t tamper with your bash shell scripts. Yes, I know I could just use permissions to keep people from reading them and it’s easy to reverse engineer the binary code, but I look at it like locking your car door; it’s a deterrent. It’s not hard to break into a car, you don’t even need to use the door. Thieves can just spider your window glass to get in, but door locks will keep the average joe out! Someone just tampering for a minute or two trying to tweak something probably won’t want to spend the time to effort breaking in.

make
make install
install -c /usr/local/bin -s shc /usr/local/bin
install -c /usr/local/man/man1 -s shc.1 /usr/local/man/man1

to compile a shell script into a binary executable:

shc -v -r -T -f filename.sh

Now you can run it like any normal executable…  If this doesn’t work for you, try an obfuscator.  Obfuscating your code might just be all you need, and it will still be the same script, just the spacing and comments will be different, to the human eye at least.

Try obfsh shell script obfuscator - inject deceiving comments and mess with blank lines, spacing and tabs.  Or just roll your own.  Rename all your variables to rediculous things or words that look like code, or reversed meanings.  Change the formatting, etc.  Get creative…

http://www.comp.eonworks.com/index.html

Linux, programming bash, binary, compile, executable, install, make, obfuscator, shc, shell scripts

Remove lines in your php files that reveal your WordPress version

December 6th, 2008

Get rid of this line in all your WordPress php files

<meta name=”generator” content=”WordPress <?php bloginfo(’version’); ?>” /><!– leave this for stats please –>

When wordpress comes out with a new version, part of the update is usually fixing an exploitable bug.  The bug may allow a hacker to access your wp-admin directory or delete your files, or worse.  If the bug is only in version xyz, and that’s the version you’re running, you don’t want anyone googling that version to run into your pages and exploit your code.

You can use the “leave this for stats please” to find and replace that line in all your php files at once.  If you don’t have shell access, well I guess you’ll have to use whatever interface your site provider overlords have thrust upon you.

[admica@host]$  sudo for x in `grep ” leave this for stats please” /path/to/myblog -R | awk ‘{print $1}’ | grep -o -e “.*php”`; do sed -e ’s/.*leave this for stats please.*//g’ $x > $x.temp; mv $x.temp $x; done

This will find all the files that contain that line and delete it from each one.

Linux bash, php, security, wordpress

Add your public SSH keys to a remote host’s authorized_keys in a single command

November 14th, 2008

[user@localhost ~]$  cat ~/.ssh/id_rsa.pub ~/.ssh/id_dsa.pub | ssh user@remotehost ’sh -c “cat - >> ~/.ssh/authorized_keys”‘

You’ll be prompted for the password just this one last time.  This is perfect for running a script that runs several remote commands through ssh.  Here’s a script that checks for your keys and adds them if they’re not there.  You’ll get prompted for a password twice if the keys didn’t already exist, and then no more.

#!/bin/sh
MY_NAME=`hostname`
MY_IPADDR=`hostname -i`

CHECK_KEYS=`ssh user@remotehost “touch ~/.ssh/authorized_keys > /dev/null 2> /dev/null; \
chmod 700 ~/.ssh/authorized_keys; grep -e $MY_NAME ~/.ssh/authorized_keys”`

LENGTH=`expr $CHECK_KEYS” : ‘.*’`
if [ $LENGTH -lt 3 ]; then
# cat the keys
else
# they already exist
fi

Another way around the password prompting issue from running a bunch of ssh commands is to branch the script and have one branch check your hostname to make sure you’re not the remote host and then start running all your commands.  When you get to the stuff you want to do remotely, echo the script across your ssh tunnel and execute it.  Now in the script, go into the 2nd branch that only runs if the hostname check matches the remote host, and it will skip down to this part on the remote run.  This gets around having a 2nd script with all your remote commands in it.  It might not be elegant, but it works!

#!/bin/sh
if [ `hostname` != $1 ]; then
# you ran this script with the remote host as the 1st argument, so it’s not going to be equal, and it will run these commands
# do a bunch of local stuff here
cat $0 | ssh user@remotehost /bin/bash `hostname`

else

# i’m here because i’ve been called on the remote host
REMOTEHOST=$2
# now i can run commands as if they were local.  executing `hostname` now would now return the remotehost name.  So any variables you want to carry over to the remote host, such as where I was called from, just add them as additional arguments when you cat the script and grab them from $2, $3, … etc. when you enter this else clause!
fi

Linux authorized_keys, bash, ssh

Installing OpenSSL, OpenSSH, and RSYNC on Solaris 2.6 (SunOS)

November 7th, 2008

Yes, I know this is ancient stuff, but I have no choice but to mess with it right now.  Old ultrasparc garbage, weeee!  So here goes the installation of some ‘modern day’ packages so I can work with this old box.  (It hasn’t been touched since 2002, ouch)

First you’ll need to download the following packages from ftp.sunfreeware.com, gunzip them, then install them with pkgadd:

# pkgadd -d libgcc-3.4.6-sol26-sparc-local.gz

# pkgadd -d egd-0.8-sol26-sparc-local.gz

# pkgadd -d popt-1.7-sol26-sparc-local.gz

# pkgadd -d zlib-1.2.3-sol26-sparc-local.gz

# pkgadd -d prngd-0.9.25-sol26-sparc-local.gz

# pkgadd -d openssl-0.9.8i-sol26-sparc-local.gz

# pkgadd -d openssh-5.1p1-sol26-sparc-local.gz

# pkgadd -d rsync-3.0.4-sol26-sparc-local.gz

Create some new directories:

/var/spool/prngd/

/var/run/

Create a startup script for the random number generator in /etc/init.d

#!/bin/sh
# 10/04/2008
# Purpose: start, stop, status script for prngd
case “$1″ in
’start’)
/usr/local/sbin/prngd /var/spool/prngd/pool /var/run/egd-pool
;;
’stop’)
/usr/bin/kill `ps -ef | /usr/bin/grep prngd | /usr/bin/grep local | /usr/bin/awk ‘{print $2}’`
;;
’status’)
if [ "`ps -ef | /usr/bin/grep prngd | /usr/bin/grep local`" ]; then
echo prngd is running…
else
echo prngd is stopped.
fi
;;
*)
echo “Usage: $0 { start | stop | status }”
exit 1
;;
esac
exit 0

Create a startup script for sshd in /etc/init.d

#! /bin/sh
#
# start/stop the secure shell daemon
case “$1″ in
’start’)
# Start the ssh daemon
if [ -f /usr/local/sbin/sshd ]; then
echo “starting SSHD daemon”
/usr/local/sbin/sshd &
fi
;;
’stop’)
# Stop the ssh deamon
PID=`/usr/bin/ps -e -u 0 | /usr/bin/fgrep sshd | /usr/bin/awk ‘{print $1}’`
if [ ! -z "$PID" ] ; then
/usr/bin/kill ${PID} >/dev/null 2>&1
fi
;;
*)
echo “usage: /etc/init.d/sshd {start|stop}”
;;
esac

Don’t forget to link them both in /etc/rc2.d so they’ll start automatically.  I used 50 and 99 to try to make sure that prngd starts before sshd fires up.

# cd /etc/rc2.d

# ln -s ../init.d/prngd S50prngd

# ln -s ../init.d/sshd S99sshd

Create ssh public key pairs.  Don’t change these output names, the daemon expects them to be named like this and if you change them, you’ll see an error like no key found, ssh v1 not starting.  But who really cares, right?

# /usr/local/bin/ssh-keygen -d -f /usr/local/etc/ssh_host_dsa_key -N “”

# /usr/local/bin/ssh-keygen -b 1024 -f /usr/local/etc/ssh_host_rsa_key -t rsa -N “”

# /usr/local/bin/ssh-keygen -t rsa1 -f /usr/local/etc/ssh_host_key -N “”

Start the daemons and you should be good to go.  If you’re getting PRNGD not seeded errors, go take care of some other stuff, it will eventually stop as long as you installed prngd properly and started it up.  Generating the keys will probably take forever if you’re on an old Ultra 1 like me, give them a minute or two.  Entropy will take forever+1.  You can fill the seed files with garbage data if you want to speed it up.  If you’re still getting PRNGD errors an hour later, you could try the kernel patch to add /dev/random /dev/urandom support directly to the kernel.  (Sun patch 112438-03) I chose not to because I didn’t want to risk something going terribly wrong with this machine.  It’s unique in my environment and been shoved in a corner and forgotten about for a long time until now!

I also installed bash and top.  Bash was a no brainer!  I hate old ksh shells with broken backspaces, arrow keys, and lack of a command history.  They were both installed with pkgadd -d, no additional script writing or directory creating necessary.  If you have library issues after installed, run ldd on the binaries and do a google search to find what libraries packages you need.

Uncategorized ancient, bash, egd, gcc, popt, prngd, rsync, solaris, ssh, ssl, sun, sunfreeware, sunos, top, zlib


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser
Mobilytics