Archive

Archive for the ‘SHELL’ Category

resolved – sudo: sorry, you must have a tty to run sudo

April 1st, 2014 5 comments

The error message below sometimes will occur when you run a sudo <command>:

sudo: sorry, you must have a tty to run sudo

To resolve this, you may comment out "Defaults requiretty" in /etc/sudoers(revoked by running visudo). Here is more info about this method.

However, sometimes it's not convenient or even not possible to modify /etc/sudoers, then you can consider the following:

echo -e "<password>\n"|sudo -S <sudo command>

For -S parameter of sudo, you may refer to sudo man page:

-S' The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. The password must be followed by a newline character.

So here -S bypass tty(terminal device) to read the password from the standard input. And by this, we can now pipe password to sudo.

PS:

From comments, you may also try below:

1. Comment out Defaults requiretty in /etc/sudoers

2. Defaults:[username] !requiretty #change [username]

3. You can use ssh -t to force pseudo-tty allocation. e.g. ssh -t user1@hostname1 "sudo df -h"

wget and curl tips

March 14th, 2014 Comments off

Imagine you want to download all files under http://www.example.com/2013/downloads, and not files under http://www.example.com/2013 except for directory 'downloads', then you can do this:

wget -r --level 100 -nd --no-proxy --no-parent --reject "index.htm*" --reject "*gif" 'http://www.example.com/2013/downloads/' #--level 100 is large enough, as I've seen no site has more than 100 levels of sub-directories so far.

wget -p -k --no-proxy --no-check-certificate --post-data 'id=username&passwd=password' <url> -O output.html

wget --no-proxy --no-check-certificate --save-cookies cookies.txt <url>

wget --no-proxy --no-check-certificate --load-cookies cookies.txt <url>

curl -k -u 'username:password' <url>

curl -k -L -d id=username -d passwd=password <url>

curl --data "loginform:id=username&loginform:passwd=password" -k -L <url>

curl -i -u username:password -H X-Oracle-UserId:myname@example.com -H X-Oracle-IdentityDomain:domainname -X GET "https://login.example.com:443/api/v1/users?userLogin"

Here's one curl example to get SSL certs info on LTM:

#!/bin/bash
path="/var/tmp"
path_root="/var/tmp"

agent="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.2)"

curl -v -L -k -A "$agent" -c ${path}/cookie "https://ltm-url/tmui/login.jsp?msgcode=1&"

curl -v -L -k -A "$agent" -b ${path}/cookie -c ${path}/cookie -e "https://ltm-url/tmui/login.jsp?msgcode=1&" -d "username=myusername&passwd=mypassword" "https://ltm-url/tmui/logmein.html?msgcode=1&"

curl -v -L -k -A "$agent" -b ${path}/cookie -c ${path}/cookie -o ${path_root}/certs-env.html "https://ltm-url/tmui/Control/jspmap/tmui/locallb/ssl_certificate/list.jsp?&startListIndex=0&showAll=true"

Now you can have a check of /var/tmp/certs-env.html for SSL certs info of Big IP VIPs.

 

avoid putty ssh connection sever or disconnect

January 17th, 2014 2 comments

After sometime, ssh will disconnect itself. If you want to avoid this, you can try run the following command:

while [ 1 ];do echo hi;sleep 60;done &

This will print message "hi" every 60 seconds on the standard output.

PS:

You can also set some parameters in /etc/ssh/sshd_config, you can refer to http://www.doxer.org/make-ssh-on-linux-not-to-disconnect-after-some-certain-time/

self defined timeout for telnet on Linux

December 26th, 2013 Comments off

telnet's default timeout value is relative high, so you may want to change timeout value to lower value such as 5 seconds. Here's the way that we can fulfill this:

#!/bin/bash

timeout()
{
waitfor=5
command=$*
$command &
commandpid=$!
( sleep $waitfor ; kill -9 $commandpid > /dev/null 2>&1 ) &
watchdog=$!
sleeppid=$PPID
wait $commandpid > /dev/null 2>&1
kill $sleeppid > /dev/null 2>&1
}

timeout telnet slcc29-scan1.us.oracle.com 1521 >> $output

Also, we can use expect and set timeout for expect. When telnet is integrated with expect, we can fulfill timeout for telnet through using expect's timeout value:

#!/usr/bin/expect

set timeout 30

send "<put telnet command here>\r"

remove duplicate images using fdupes and expect in linux

December 13th, 2013 Comments off

I've got several thousands of pictures, but most of them had several exact copies of themselves. So I had to remove duplicate ones by hand firstly.

Later, I thought of that in linux we had md5sum which will give the same string for files with exact same contents. Then I tried to write some program, and that toke me some while.

I searched google and found that in linux, we had fdupes which can do the job very well. fdupes will calculate duplicate files based on file size/md5 value, and will prompt you to reserve one copy or all copies of the duplicates and remove others if you gave -d parameter to it. You can read more about fdupes here http://linux.die.net/man/1/fdupes

As all the pictures were on a windows machine, so I installed cygwin and installed fdupes and expect. Later I wrote a small script to reserve only one copy of the duplicate pictures for me(you will have to enter your option either reserving one copy or all copies by hand if you do not use expect, as there's no option for reserve one copy by the author of fdupes). Here's my program:

$ cat fdupes.expect
#!/usr/bin/expect
set timeout 1000000
spawn /home/andy/fdupes.sh
expect "preserve files" {
send "1\r";exp_continue
}

$ cat /home/andy/fdupes.sh
fdupes.exe -d /cygdrive/d/pictures #yup, my pictures are all on this directory on windows, i.e. d:\pictures

After this, you can just run fdupes.expect, and it will reserve only one copy and remove other duplicates for you.

PS: Here's man page of fdupes https://github.com/adrianlopezroche/fdupes

make tee to copy stdin as well as stderr & prevent ESC output of script

October 30th, 2013 Comments off
  • Make tee to copy stdin as well as stderr

As said by manpage of tee:

read from standard input and write to standard output and files

So if you have error messages in your script, then the error messages will not copied and write to file.

Here's one workaround for this:

./aaa.sh 2>&1 | tee -a log

Or you can use the more complicated one:

command > >(tee stdout.log) 2> >(tee stderr.log >&2)

  • Prevent ESC output of script

script literally captures every type of output that was sent to the screen. If you have colored or bold output, this shows up as esc characters within the output file. These characters can significantly clutter the output and are not usually useful. If you set the TERM environmental variable to dumb (using setenv TERM dumb for csh-based shells and export TERM=dumb for sh-based shells), applications will not output the escape characters. This provides a more readable output.

In addition, the timing information provided by script clutters the output. Although it can be useful to have automatically generated timing information, it may be easier to not use script’s timing, and instead just time the important commands with the time command mentioned in the previous chapter.

PS:

  1. Here's the full version http://stackoverflow.com/questions/692000/how-do-i-write-stderr-to-a-file-while-using-tee-with-a-pipe
  2. Some contents of this article is excerpted from <Optimizing Linux® Performance: A Hands-On Guide to Linux® Performance Tools>.

bash & expect tips

August 19th, 2013 Comments off


/u02/0-sync.sh

#!/usr/bin/expect
set timeout 10800
spawn /u02/sync.sh
expect "*assword*" {send "test\r";exp_continue
expect "*#*" {close}
}

/u02/sync.sh

#! /bin/sh
logpathroot=/u02/logs
synclist=/u02/sync.list

num=1
allnum=`cat $synclist | wc -l`
while [ $num -le $allnum ];
do
line=`awk '{if(NR=='$num') print}' $synclist`
component=`echo $line | awk '{print $2}'`
logtype=`echo $line | awk '{print $3}'`
host=`echo $line | awk '{print $4}'`
source=`echo $line | awk '{print $5}'`
env=`echo $line | awk '{print $1}'`
if [[ "$env" =~ "#" ]]; then
env=`echo $env | sed "s/#//"`
lnenv=`ls /u01/logs | grep -i "$env"`
exist=`ls "/u01/logs/${lnenv}/${component}_${logtype}-$host"`
if [ "$exist" != "" ]; then
rm -f /u01/logs/${lnenv}/${component}_${logtype}-$host
fi
else
lnenv=`ls /u01/logs | grep -i "$env"`
/bin/mkdir -p "${logpathroot}/${env}/${component}/${logtype}-$host"

#rsync -avz --exclude "aime/work/CLOUDTOP/*" /scratch/ /scratch_new/
/usr/bin/rsync -ave ssh --exclude '.zfs' --delete-excluded logs@${host}:${source}/ ${logpathroot}/${env}/${component}/${logtype}-$host
#chmod 755 -R "${logpathroot}/${env}/${component}/"
exist=`ls "/u01/logs/${lnenv}/${component}_${logtype}-$host"`
if [ "$exist" = "" ]; then
# rm -f /u01/logs/${lnenv}/${component}_${logtype}-$host
component1=`echo $component | tr "[:upper:]" "[:lower:]"`
logtype1=`echo $logtype | sed "s/-/_/"`
ln -sf ${logpathroot}/${env}/${component}/${logtype}-$host/ /u01/logs/${lnenv}/${component1}_${host}_${logtype1}_log
fi
fi
num=`expr $num + 1`
done

rm -f /u02/sync_at_*
echo > /u02/sync_at_"`date +%Y-%m-%d` `date +%H:%M` (UTC)"

/u02/sync.list

DC1 apex AdminServer_adr test27vmf4053 /u01/local/config/m_domains/base_domain/servers/AdminServer/adr
DC1 emgc EMGC_ADMINSERVER_adr test27cn05 /u01/local/exalogic/Middleware/gc_inst/user_projects/domains/GCDomain/servers/EMGC_ADMINSERVER/adr
DC1 emgc EMGC_OMS1_adr test27cn05 /u01/local/exalogic/Middleware/gc_inst/user_projects/domains/GCDomain/servers/EMGC_OMS1/adr
DC1 emgc EMGC_OMS2_adr test27cn06 /u01/local/exalogic/Middleware/gc_inst/user_projects/domains/GCDomain/servers/EMGC_OMS2/adr
#DC1 emgc EMGC_OMS3_adr test27cn07 /u01/local/exalogic/Middleware/gc_inst/user_projects/domains/GCDomain/servers/EMGC_OMS3/adr

PS:

  • If you want to match empty space in expect, you can use the ascii code of space which is 040, and expect -re "\040" would work.
  • About special characters in tcl/expect/shell:

Some characters have special meanings in Tcl or shells (e.g., sh, tcsh, etc.). In particular, these characters may provide security holes for shells.
The characters listed here are escaped by preceeding each one with a backslash (\).
The following characters are dissallowed or have special meanings in Tcl and so are escaped: &;`'"|*?~<>^()[]{}$\-
The following characters are dissallowed or have special meanings in command shells and so are escaped: &;`'"|*?~<>^()[]{}$\\n
The only difference between the two tables is the addition of \n to the shell escape table.

  • To execute multiple commands in one run, you can do below:
[root@vm1 ~]# cat conf.andy
sudo su -
echo $UID >/tmp/e
for i in {1..10}
do
touch /tmp/$i.txt
done
ls /tmp/

[root@vm1 ~]# cat /bin/autoexcute.andy
#!/usr/bin/expect --
set timeout 60
set user [lindex $argv 0]
set ip [lindex $argv 1]
set password [lindex $argv 2]
set com [lindex $argv 3]

spawn ssh -t -o "StrictHostKeyChecking no" $user@$ip
expect {
  "assword:" {
    send "$password\r"
    expect -re "(\\\$|\\\$\040)$" {
        send "/usr/bin/sudo su -\r"
        sleep 2
        expect -re "(\->\040|#|#\040)$" {
            send "$com\r"
            sleep 1
            expect -re "(\->\040|#|#\040)$" {close}
        }
    }
  }
}

[root@vm1 ~]# /bin/autoexcute.andy root testvm password1 "`cat conf.andy`"
spawn ssh -t -o StrictHostKeyChecking no root@testvm
root@testvm's password:
Last login: Tue Sep 15 19:06:49 2015 from vm1.us.oracle.com
-bash-4.1$ /usr/bin/sudo su -
[root@testvm-luis ~]# sudo su -
[root@testvm-luis ~]# echo $UID >/tmp/e
[root@testvm-luis ~]# for i in {1..10}
> do
> touch /tmp/$i.txt
> done
[root@testvm-luis ~]# ls /tmp/
1.txt  
10.txt 
2.txt  
3.txt  
4.txt  
5.txt  
6.txt  
7.txt  
8.txt  
9.txt  
  • Here's something about expect_out in tcp/expect:

expect_feedback_01

expect_feedback_02

word boundaries in egrep

June 17th, 2013 Comments off

Here's one example:

[root@doxer_#1]# cat a
mcat me
here cat is
ok concaten

[root@doxer_#1]# egrep '\<cat\>' a
here cat is

[root@doxer_#1]# egrep '\<cat' a
here cat is

And here goes the details:

word-boundaries

GNU Posix Standards Conformance guide – take diffutils for example

January 30th, 2013 Comments off

In a few cases, the GNU utilities' default behavior is incompatible with the POSIX standard. To suppress these incompatibilities, define the POSIXLY_CORRECT environment variable. Unless you are checking for POSIX conformance, you probably do not need to define POSIXLY_CORRECT.

Normally options and operands can appear in any order, and programs act as if all the options appear before any operands. For example, ‘diff lao tzu -C 2’ acts like ‘diff -C 2 lao tzu’, since ‘2’ is an option-argument of -C. However, if the POSIXLY_CORRECT environment variable is set, options must appear before operands, unless otherwise specified for a particular command.

Newer versions of POSIX are occasionally incompatible with older versions. For example, older versions of POSIX allowed the command ‘diff -c -10’ to have the same meaning as ‘diff -C 10’, but POSIX 1003.1-2001 ‘diff’ no longer allows digit-string options like -10.

The GNU utilities normally conform to the version of POSIX that is standard for your system. To cause them to conform to a different version of POSIX, define the _POSIX2_VERSION environment variable to a value of the form yyyymm specifying the year and month the standard was adopted.

Two values are currently supported for _POSIX2_VERSION: ‘199209’ stands for POSIX 1003.2-1992, and ‘200112’ stands for POSIX 1003.1-2001. For example, if you are running older software that assumes an older version of POSIX and uses ‘diff -c -10’, you can work around the compatibility problems by setting ‘_POSIX2_VERSION=199209’ in your environment.

Categories: IT Architecture, Programming, SHELL Tags:

resolved – how to remove whitespaces in expect – tcl language

November 15th, 2012 Comments off

Here's the way to remove whitespaces automatically when using expect/tcl language:

If $line is the string variable that you wanna remove whitespaces, just replace $line with the following in your programs context:

[string trim $line]

That's it, simple, Ehh..

resolved – tcl error list element in quotes followed by “” instead of space while executing tcl

November 13th, 2012 1 comment

Today I met an error on tcl with the following error message:

list element in quotes followed by "\/ovm.info" instead of space
while executing
"llength $line"
(procedure "passchg" line 48)
invoked from within
"passchg $prompt $user $pass $host $spawn_id $SCRIPTFILE $SCRIPTFILENAME $resfile"

After some digging, I found the problem was caused in the following tcl script:

if {[llength $line] == 0 || $first_char == ""} {
continue
}

Analysis:

Here's the info from tcl wiki:

The only "problem" is in developers thinking any string can be treated as a list. There are no bugs in Tcl related to list commands and quoting. The problems are when developers try to use list commands on things that are not lists. Thinking of any string can be a list is misunderstanding Tcl.

Resolution:

To resolve this, we need change [llength $line] == 0 to [llength [split $line]] == 0

if {[llength [split $line]] == 0 || $first_char == ""} {
continue
}

So next time we need  replace $arg with [split $arg] whenever lindex/lrange/llength is used.

PS:

 

Categories: IT Architecture, Programming, SHELL Tags:

Bash Shell Parameter Expansion examples

July 7th, 2012 Comments off

Here are some example/tutorial about bash Shell Parameter Expansion. More is here.

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

ifnull="";
my_god="God exists";
my_null_message="yes, it's null";

echo ${ifnull:-will_print_this};


If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.

echo ${ifnull:=will_assign_this}; 

 

If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

ifnull="";

echo ${ifnull:?"Error - ifnull is null"};

If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

echo ${my_god:+will_print_this};

Expands to up to <length> characters of parameter starting at the character specified by <offset>. If length is omitted, expands to the substring of parameter starting at the character specified by offset. length and offset are arithmetic expressions (see Shell Arithmetic). This is referred to as Substring Expansion. length must evaluate to a number greater than or equal to zero. If offset evaluates to a number less than zero, the value is used as an offset from the end of the value of parameter.

Substring indexing is zero-based unless the positional parameters are used, in which case the indexing starts at 1 by default. If offset is 0, and the positional parameters are used, $@ is prefixed to the list.

echo ${my_god:0:3}; #will print "God"

echo ${my_god:4}; #will print "exists"

If parameter is ‘@’, the result is length positional parameters beginning at offset.

set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h

echo ${@:7} #will print "7 8 9 0 a b c d e f g h"

If parameter is an indexed array name subscripted by ‘@’ or ‘*’, the result is the length members of the array beginning with ${parameter[offset]}. A negative offset is taken relative to one greater than the maximum index of the specified array. Substring expansion applied to an associative array produces undefined results.

Note that a negative offset must be separated from the colon by at least one space to avoid being confused with the ‘:-’ expansion.

array=(0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h)

echo ${array[@]:7} #will print "7 8 9 0 a b c d e f g h"

echo ${array[@]: -7:2} #will print "b c"

 

The length in characters of the expanded value of parameter is substituted. If parameter is ‘*’ or ‘@’, the value substituted is the number of positional parameters. If parameter is an array name subscripted by ‘*’ or ‘@’, the value substituted is the number of elements in the array.

my_god="God exists";

echo "God exists has ${#my_god} characters"; #10

The word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘#’ case) or the longest matching pattern (the ‘##’ case) deleted. If parameter is ‘@’ or ‘*’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

echo ${my_god#God}; #will print "exists"

echo ${my_god##God*}; #will print NULL

The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted. If parameter is ‘@’ or ‘*’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

echo ${my_god%*exists}; #will print "God"

echo ${my_god%%*exists}; #will print NULL

The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with ‘/’, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with ‘#’, it must match at the beginning of the expanded value of parameter. If pattern begins with ‘%’, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is ‘@’ or ‘*’, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.

my_god="God exists, heaven exists"

echo ${my_god//exists/does not exist}; #will print "God does not exist, heaven does not exist"

echo ${my_god/#God exists/God does not exist}; #will print "God does not exist, heaven exists"

echo ${my_god/%exists/does not exist}; #will print "God exists, heaven does not exist"

echo ${my_god//exists/}; #will print "God , heaven"

 

Categories: IT Architecture, Programming, SHELL Tags:

trap bash shell script explanation and example

July 2nd, 2012 Comments off

If you want to give some information on standard output when the user press ctrl+c on the bash script, or you want to print something when the script completes, then you should consider using trap to implement this.

Here's an example which will print something to end user when the user print ctrl+c(SIGINT is equal to number 2):

#!/bin/bash
trap "echo 'you typed ctrl+c'" 2
sleep 5
And if you want print something when the script ends, you can use the following as an example:

#!/bin/bash
trap "echo 'script ends'" 0
sleep 5

 

useful sed single line examples when clearing embedded trojans or embedded links

June 7th, 2012 Comments off

When your site is embedded with some links/trojans by somebody maliciously, the first thing you could think of would mostly like to clear these malicious links/trojans. sed is a useful stream editor based on line, and you would of course think of using sed to do the cleaning job.

Usually, the embedded codes would be several lines of html codes like the following:

<div class="trojans">
<a href="http://www.malicous-site-url.com">malicous site's name</a>
blablabla...
</div>

To clear these html codes, you can use the following sed line:

sed  '/<div class=\"trojans\">/,/<\/div>/d' injected.htm

But usually the injected files are spread across several directories or even your whole website's directory. You can combine using find and sed together to clean these annoying trojans:

find /var/www/html/yoursite.com/ -type f \( -name *.htm -o -name *.html -o -name *.php \) -exec sed  -i.bak' /<div class=\"trojans\">/,/<\/div>/d' {} \;

Please note I use -i.bak to backup file before doing the replacement.(you should also backup your data before cleaning trojans!)

PS:

For more info about sed examples/tutorials, you may refer to the following two resources:

1.http://sed.sourceforge.net/sed1line.txt

2.http://www.grymoire.com/Unix/Sed.html

general bash tips

March 25th, 2012 Comments off

1.select in & :<<EOF
:<<EOF
echo "DATABASE?"
select yn in "Yes" "No"
do
case $yn in
Yes)
read -p "please type the host,user,pwd,db,use space to separate" host user pwd db
mysqldump -h $host -u $user -p$pwd --default-character-set=utf8 $db>/root/$wwwname.sql
;;
No)
echo "no database"
;;
done
EOF

2.for in do done
for i in /etc/profile.d/*.sh; do
if [ -r "$i" ]; then
. $i #execute it
fi
done

3.press enter to continue
echo "Vivek Gite"
read -p "Press [Enter] key to continue..."

4.functions && unset -f #remove function;declare -f list,declare -F <function name only>
#!/bin/sh
usage()
{
echo "usage:`basename $0` start|stop process name"
}
OPT=$1
PROCESSID=$2
if [ $# -ne 2 ]
then
usage
exit 1
fi
case $OPT in
start|Start) echo "Starting..$PROCESSID"
;;
stop|Stop) echo "Stopping..$PROCESSID"
;;
*)usage
;;
esac
5.while do done shift
while [ $# -ne 0 ];#space
do
echo $1
shift
done

6.read && $IFS && readarray[mapfile] && source[.]
#!/bin/bash
# Reading lines in /etc/fstab.
File=/etc/fstab
{
read line1
read line2
} < $File
echo "First line in $File is:"
echo "$line1"
echo
echo "Second line in $File is:"
echo "$line2"

read -s -t 10 -p "hello" domain_name #timeout 10s;-s not showing input, like passwd
whois $domain_name

pwd="gitevivek:x:1002:1002::/home/gitevivek:/bin/sh"
old="$IFS"
IFS=:
read -r login password uid gid info home shell <<< "$pwd" #-r,Remove any current binding for keyseq, <<<;<< will return and ask for input,-r, do not escape \
printf "Your login name is %s, uid %d, gid %d, home dir set to %s with %s as login shell\n" $login $uid $gid $home $shell
IFS="$old"

7.declare && array

#bash array

shares=`df -Ph | egrep -v "/boot$|/dev/shm$|^slc|Filesystem" | awk '{print $NF}'`
for myshare in ${shares[@]};do
echo $myshare
done

declare -i x=10 #-a,-A,-f,-r,-x; refer to Bash Builtin Commands > declare
declare -i y=10
declare -i z=0
z=$(( x + y ))
echo "$x + $y = $z"

declare -a hello=("hello world" world2)
echo ${hello[1]} #world2
echo ${hello[@]} #world world2;${name[*]}
echo ${#hello[1]} #length,6
echo ${#hello[*]} #2

declare -A hellos
hellos=([a]=hello1 [b]=hello2 [c]=hello3)
echo ${hellos[b]} #hello2

8.break && continue
#!/bin/bash
# set an infinite while loop
while :
do
read -p "Enter number ( -9999 to exit ) : " n

# break while loop if input is -9999
[ $n -eq -9999 ] && { echo "Bye!"; break; }

isEvenNo=$(( $n % 2 )) # get modules
[ $isEvenNo -eq 0 ] && echo "$n is an even number." || echo "$n is an odd number."

done

for i in something
do
while true
do
cmd1
cmd2
[ condition ] && break 2 #The above break 2 will breaks you out of two enclosing for and while loop.
done
done
9.$RANDOM
#!/bin/bash

# Genarate a number (random number) between 1 and 10
r=$(( $RANDOM%10+0 )) #$RANDOM,0~32767

# Quotes author name
author="\t --Bhagavad Gita."

# Store cookies or quotes in an array
array=( "Neither in this world nor elsewhere is there any happiness in store for him who always doubts."
"Hell has three gates: lust, anger, and greed."
"Sever the ignorant doubt in your heart with the sword of self-knowledge. Observe your discipline. Arise."
"Delusion arises from anger. The mind is bewildered by delusion. Reasoning is destroyed when the mind is bewildered. One falls down when reasoning is destroyed."
"One gradually attains tranquillity of mind by keeping the mind fully absorbed in the Self by means of a well-trained intellect, and thinking of nothing else."
"The power of God is with you at all times; through the activities of mind, senses, breathing, and emotions; and is constantly doing all the work using you as a mere instrument."
"One who has control over the mind is tranquil in heat and cold, in pleasure and pain, and in honor and dishonor; and is ever steadfast with the Supreme Self"
"The wise sees knowledge and action as one; they see truly."
"The mind acts like an enemy for those who do not control it."
"Perform your obligatory duty, because action is indeed better than inaction." )

# Display a random message
echo
echo ${array[$r]}
echo -e "$author"

11。Redirecting Standard Output and Standard Error
&>word #same as >word 2>&1
12.Here Documents && Here Strings
http://en.wikipedia.org/wiki/Here_document

13.sh debugging
sh -n ./if.sh #for checking syntax
sh -x ./if.sh #bash debugging

14.expr && let && (( ))
expr 5 + 3 #8
let z=5+3 #$z=8
let z+=3
let a=5**2 #25
z=$(( 5 + 3 ))
z=$(( z + 4 )) #12
LOOP=10;expr $LOOP '-' 10
echo $?
a=6;expr '(' $a '&' 4 ')' + 5 #11,| &
b=beijing;expr $b = beijing #1
expr 5 '<' 10 #1
LOOP=0;LOOP=`expr $LOOP +1 `
expr length 'hell' #4
expr index 'hello,world' 'o' #5
expr substr 'hello,world' 2 5 #start,length
expr 123458akd : '[0-9]*' #6, return number of matched character
expr accounts.doc : '\(.*\).doc' #accounts

16.:
while :
do
operation-1
operation-2
...
operation-n
done
# Same as:
# while true
# do
# ...
# done

17.() {} []
echo \"{These,words,are,quoted}\" #"These" "words" "are" "quoted"

a=123
( a=321; ) #cannot read variables created in the child process
echo "a = $a" # a = 123

Array=(element1 element2 element3) #array initialization.
echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z

echo $[5+3] #8

20.wait
echo "Updating 'locate' database..."
echo "This may take a while."
updatedb /usr & # Must be run as root.
wait
# Don't run the rest of the script until 'updatedb' finished.
# You want the the database updated before looking up the file name.
locate $1

22.time bash if.sh
#!/bin/bash #parallel

for ((i=0;i<5;i++));do
{
sleep 3;echo 1>>aa && echo "done!"
} &
done
wait
cat aa|wc -l
rm aa

23. Comparison Operators is here

Categories: IT Architecture, Programming, SHELL Tags:

autossh sshldap

March 25th, 2012 Comments off

1. create /root/autossh.sh

#!/usr/bin/expect --
#!/bin/sh
#
# autossh.sh
# SimpleSSHProxy
#
# Created by ivan on 10-9-10.
# http://twitter.com/ivan_wl
#
# This is another wheel of autossh built using expect.
# If you have to use ssh password authorizing,
# use this instead of autossh.
#
# WARNING: This script is NOT SAFE for your password,
# DON'T USE THIS IF YOU HAVE SECURITY CONCERNS!
#
# Usage: autossh.sh [foo] [foo] [bindingIP:port] [foo] [username] [remoteHost] [password]
# get arguments
set username [lrange $argv 0 0]
set remoteHost [lrange $argv 1 1]
set password [lrange $argv 2 2]

while (1) {
set connectedFlag 0;
spawn /usr/bin/ssh $username@$remoteHost;
match_max 100000;
set timeout 60;
expect {
"?sh: Error*"
{ puts "CONNECTION_ERROR"; exit; }
"*yes/no*"
{ send "yes\r"; exp_continue; }
"*?assword:*" {
send "$password\r"; set timeout 4;
expect "*?assword:*" { send "$password\r"; set timeout 4; }
set connectedFlag 1;
}
# if no password
"*~*"
{ send "echo hello\r"; set connectedFlag 1; }
}
if { $connectedFlag == 0 } {
close;
puts "SSH server unavailable, retrying...";
continue;
}

while (1) {
set conAliveFlag 0;
interact {
# time interval for checking connection
timeout 600 {
set timeout 30;
send "echo hello\r";
expect "*hello*" { set conAliveFlag 1; }
if { $conAliveFlag == 1 } {
# connection is alive
continue;
} else { break; }
}
}
}

close;
puts "SSH connection failed, restarting...";
}

2. create autoldap.sh:

#!/bin/bash
/root/autossh.sh <replace with your SSH username>$1 <replace with your SSH password> 2>/dev/null

3. run autoldap.sh <SSH hostname>

general expect tips

March 23rd, 2012 Comments off

#!/bin/bash
PATH=$PATH:/opt/csw/bin/
export PATH
CMD="uname -a && finishthecoreadm"
#....ksh: finishthecoreadm: not found,......,......
USERNAME="root"
PASSWORD="test"
for i in `cat /home/liandy/servers_list`
do
echo ''>/root/.ssh/known_hosts
expect <<EOF
spawn ssh -l${USERNAME} -p22 $i $CMD
set timeout 20
expect "*yes*"
send "yes\r"
expect "assword:"
send "${PASSWORD}\r"
expect eof
EOF
done

================
spawn ssh 172.2.0.79 ifconfig
expect {
“*yes*” { send “yes\r” }
“password:” { send “yytt22\r” }
}
expect eof

#interact
spawn ssh 172.2.0.79
expect {
“*yes*” { send “yes\r” }
“password:” { send “yytt22\r” }
}
interact
expect “hi\n”
send “youyouyou”

expect “hi”
send “you typed $expect_out(buffer)”
send “but I only expected $expect_out(0,string)”

#spawn_id
spawn bc
set bc_id $spawn_id

spawn /bin/sh
set sh_id $spawn_id

set spawn_id $bc_id
send “scale=50\r”

#send -i & expect -i
set spawn_id $ftp_id
send “get $file1\r”;expect “220*ftp> ”
send -i $write_id “success get file” #another spawn_id
send “get $file2\r”;expect “220*ftp>” #return to current spawn_id

expect {
-i $ftp_id
“220*ftp> ” action1
“550*ftp ” action2
}

same as:

expect {
-i $ftp “220*ftp> ” action
“550*ftp ” action2 #return current spawn_id
}

#proc
proc login(id) { #spawn as parameter
set spawn $id
expect “login:”
send “$name\r”

expect “password:”
send “$password\r”

expect “$prompt”
}

#!/bin/bash
expect -c ”
set send_human {.1 .3 1 .05 2}

send -h \”I’m hungry. Let’s do lunch.\”