Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need help in tuning/adding debug in script

Posted on 2014-08-18
41
Medium Priority
?
370 Views
Last Modified: 2014-08-21
Hi ,

I have attached one script in which i want to add some functions like below:

add_client()
{
      echo >> $DHCPD
      echo "host $hostname {#$hostname" >> $DHCPD
      echo "#client network interface in backup vlan : $cni#$hostname" >> $DHCPD
      echo "#networker server : $ns,$nsip#$hostname" >> $DHCPD
      echo "#storage node : $snhost,$snip#$hostname" >> $DHCPD
      echo "#platform : $platform#$hostname" >> $DHCPD
      echo "hardware ethernet $mac;#$hostname" >> $DHCPD
      echo "fixed-address $ip;#$hostname" >> $DHCPD
      echo "server-name \"$snip\";#$hostname" >> $DHCPD
      echo "next-server $snip;#$hostname" >> $DHCPD
}

list_client()
{
LIST_CLIENTS=`grep "\#" $DHCPD |grep "^host " |awk '{print $2}'`
for i in ${LIST_CLIENTS}
do
echo $i
done |more
echo
}


remove_client()
{
list_client
echo "Your choice ?"
read choice

sed "/$choice/d" $DHCPD > $DHCPD.tmp && mv $DHCPD.tmp $DHCPD
echo
echo "Client $choice successfully removed"

}

Currently above function is working but if by mistake you give bad choice the also it deletes all entries.

What i want is to avoid any situation - i am looking something like - it deletes only what i have added in dhcpd.conf file. so that accedently nothing wrong can happen.

Please help?

Also is there any way in CENTOS 6.4 so that i can only reboot tftp service only and not whole xinetd daemon ?

Thanks!
0
Comment
Question by:apunkabollywood
  • 23
  • 18
41 Comments
 
LVL 81

Expert Comment

by:arnold
ID: 40267566
There is no attachment.

It is unclear what you are asking here.

Deleting entries, all you can do to avoid making a mistake, is to prompt the user again.
i.e. relist the options and then the selection and prompt the user to confirm that this entry is the one that needs to be deleted.

Passing a choice directly/blindly is the issue.
One thing you can do is in the listing phase, enumerate the list while maintaining a reference file.
1 Ip1
2 Ip2
3 ip3
etc.
then prompt the user which entry should be deleted. only accept a number.
Then when the selection is returned, you can consult the reference file to know which selection needs deleting, and then delete the file.
The simple way to keep track of the reference file is to use the scripts PID ($$) as part of the reference filename.
0
 

Author Comment

by:apunkabollywood
ID: 40267627
Hi Arnold - Thanks - let me breif you

Look the output of script:

### Cristie Management Script ###
#################################

1 - Start Cristie services

2 - Stop Cristie services

3 - Show Cristie status

4 - Show Cristie configuration for a specific client

5 - Add a Cristie client

6 - Modify a Cristie client

7 - Remove a Cristie client

8 - Exit

Your choice ?
7

sdds
sdsd

Your choice ?


above sdds and sdsd are two dhcp entries we made in dhcpd.conf file

below is the dhcpd.conf file output

[root@localhost ~]# cat /etc/dhcp/dhcpd.conf




host sdds {#sdds
#Cristie
#client network interface in backup vlan : 32#sdds
#networker server : d,32#sdds
#storage node : 32,ed#sdds
#platform : 32#sdds
hardware ethernet dw;#sdds
fixed-address sdsd;#sdds
server-name "ed";#sdds
next-server ed;#sdds
}#sdds

host sdsd {#sdsd
#Cristie
#client network interface in backup vlan : de#sdsd
#networker server : ds,e3#sdsd
#storage node : ds,23#sdsd
#platform : we#sdsd
hardware ethernet ds;#sdsd
fixed-address 32;#sdsd
server-name "23";#sdsd
next-server 23;#sdsd
}#sdsd



Now i agreed with you that numbering could resolve the issue:

like

1) sdds
2) sdsd

but i am not able to allocate numbering to this list - please help me in this:

this i was trying previously to avoid wrong input:

remove_client()
{
TCLIENT=0
list_client
echo "Your choice ?"
read choice

TCLIENT="grep "\#" $DHCPD |grep "^host " |awk '{print $2}' | grep $choice &> /dev/null"

if [ $TCLIENT == 0 ]; then
        sed "/$choice/d" $DHCPD > $DHCPD.tmp && mv $DHCPD.tmp $DHCPD
else
        echo "Wrong Choice - No Entry with this name"
fi
}

but above one giving errors

please help me to fix or give the right code of your suggestion for giving numbering to LIST_CLIENT?

Thanks
0
 
LVL 81

Expert Comment

by:arnold
ID: 40267726
As part of your list_client function you would introduce the enumeration where the output from the awk is going to the file which is then cat filename.$$
Prompt for a choice.

the change to the enumeration requires the addition of another variable count that starts at 1 or 0 depending on your preference that gets incremented after the
Echo "$count $i"
echo "$count $i" >> filename.$$



To avoid wrong output, you would place a conditional with a grep that will look at whether a choice provided has an explicit match.
I.e. If ( egrep "^#client network interface in backup vlan: de:$choice$" >/dev/null) ; then
                    Continue with the remove
      Else echo wrong selection
       Fi
Or using the same command line used in list_client to feed into a grep with the choice, though the same reason an errand selection deletes all.

Checking to make sure the choice is not an empty string or its length is of substantial size

Test ! -z "$choice"
 Which shell are you using sh, bash, etc.?
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

Author Comment

by:apunkabollywood
ID: 40267787
Thank you arnold - i am using bash.

Is it possible to give continous steps as i am able to understand your nice description but unable to place it properly ...

for remove_client() function ?
0
 
LVL 81

Expert Comment

by:arnold
ID: 40268387
Which method? Creating an enumerated file?
Instead of an enumerated file, you can create an array of the possible selections.

List_client will return an array.


Will provide greater example later on.
0
 

Author Comment

by:apunkabollywood
ID: 40268421
Thank you arnold - Whatever you think will work great - i just need person can only able to delete the listed DHCP entry and exact entry.
0
 

Author Comment

by:apunkabollywood
ID: 40268617
I have tried this also but didnt work:

remove_client()
{
list_client > /tmp/tclient
tval=/tmp/tclient
#tval=cat /tmp/tclient
cat /tmp/tclient
echo "Your choice ?"
read choice

for i in ${tval};do
if [ $tval == $choice ]; then
sed "/$choice/d" $DHCPD > $DHCPD.tmp && mv $DHCPD.tmp $DHCPD
echo "Cristie DHCP Entry has been removed"
exit 0
else
echo "Wrong Choice - No Entry with this name"
fi
done
press
}


Just giving wrong choice again and again ...and nothing deleting
0
 
LVL 81

Expert Comment

by:arnold
ID: 40270166
Ok Try the following replacing the current List_client

list_client()
{
LIST_CLIENTS=`grep "\#" $DHCPD |grep "^host " |awk '{print $2}'`
count=0 # you can start the count from 0 if you prefer note the test if 0 means there 
#are no clients. no need to output anything to screen as there are no choices to be 
# made
for i in ${LIST_CLIENTS}
let count+=1;
do
echo "$count $i"
DHCP_CLIENTS[$COUNT]="$i"
let count+=1;
done 
echo
}

Open in new window


remove_client()
{
TCLIENT=0
list_client
if test ${#DHCP_CLIENTS[@}} -eq "0"; then {
    echo "There are no clients to remove. Exiting"
    exit;
}
echo "q) quit l) list Clients"
while (true) do
echo -n "What is your Choice?  "
read choice
case "$choice" in

q)    parameter="exit"
       break
;;
l) for list in ${!DHCP_CLIENTS[@]} 
          do
           echo "$list) ${DHCP_CLIENTS[$list]}"
           done
    echo "q) quit l) List_clients)
;;
esac
}

if test "$parameter" == "exit" ; then
      exit
fi

if test "$choice" -ge "1" -a "$choice" -le "${#DHCP_CLIENTS[@]}"; then
TCLIENT="grep "\#" $DHCPD |grep "^host " |awk '{print $2}' | grep ${DHCP_CLIENTS[$choice]} &> /dev/null"

if [ $TCLIENT == 0 ]; then
        sed "/$choice/d" $DHCPD > $DHCPD.tmp && mv $DHCPD.tmp $DHCPD
else
        echo "Wrong Choice - No Entry with this name"
fi
break;
fi
done
}

Open in new window


After recosidering your, you can use your LIST_CLIENTS list variable as well instead of using the DHCP_CLIENTS I add.
you can use the for index in "${!LIST_CLIENTS[@]}" do
to have the index starting at 0 through ${#LIST_CLIENTS[@]} length of list.

Use a copy of your script to test with these examples.
The user will have a list from 0- number of elements in the LIST_CLIENTS list.
you can use a conditional i.e. if $index modulo 4 is 0 generate a new line while when you are outputing the options, list echo -n index) value

the output could look like
0) option1   1) option2  2) option3 3) option4
4)option 5
q) quit   l) list client

What is your Choice?
0
 

Author Comment

by:apunkabollywood
ID: 40270250
Thank you - but it says " syntax error: 'let' unexpected"
0
 
LVL 81

Expert Comment

by:arnold
ID: 40270295
Move let one line down, below do.
Switch line 8 and 9 in list_client function.

The idea is not to let individuals directly type a selection to the delete portion which is what was causing the issue, I.e. A person wants to abort, and it appeared that you do not provide the user with an option to do nothing.

There are different ways to implement DHCP lease cleanup without user intervention.
0
 

Author Comment

by:apunkabollywood
ID: 40270890
Thank you - i am getting you ...

just a one more small syntax - i thought i change but then i thought better to ask you ?

./new.sh[219]: missing ]

 215 remove_client()
    216 {
    217 TCLIENT=0
    218 list_client
    219 if test ${#DHCP_CLIENTS[@}} -eq "0"; then {
    220     echo "There are no clients to remove. Exiting"
    221     exit;
    222 }
    223 echo "q) quit l) list Clients"
    224 while (true) do
    225 echo -n "What is your Choice?  "
0
 

Author Comment

by:apunkabollywood
ID: 40270932
I  have fixed above but still some issues - i am attaching my full script
test.ch.txt
0
 
LVL 81

Expert Comment

by:arnold
ID: 40271500
Line     219 if test ${#DHCP_CLIENTS[@}} -eq "0"; then {

The closing array member should be ] while you have it as }
Below is the corrected version.

    219 if test ${#DHCP_CLIENTS[@]} -eq "0"; then {

I'll take a look at the entire script.
0
 
LVL 81

Expert Comment

by:arnold
ID: 40271507
It list_client,  I seem to have kept count+=1 in two places, effectively incrementing it twice.  Remove the one near the end of the for loop.
0
 
LVL 81

Expert Comment

by:arnold
ID: 40271523
Kept forgetting to deal with you xinetd question, issuing a HUP signal to xinetd is enough for it to reread the configuration.

Not clear what or why you need to cycle tftp.
Is your issue that it gets too many connections that need to be flushed?
0
 

Author Comment

by:apunkabollywood
ID: 40271680
Actually this script is to add/remove dhcp client as per LINUX/Solaris oS and to start few services if required.

I have removed the count=+1 at end of for loop and

list_client()
{
LIST_CLIENTS=`grep "\#" $DHCPD |grep "^host " |awk '{print $2}'`
count=0
for i in ${LIST_CLIENTS}
do
let count+=1;
echo "$count $i"
DHCP_CLIENTS[$COUNT]="$i"
done
echo
}

but stil giving issues in line 218/219 syntax error. before this script was running fine ..just issue was in wrong deletion of dhcp entry.
0
 
LVL 81

Expert Comment

by:arnold
ID: 40271740
You said you were using bash, looking at the entire script you are using, korn shell.

Did you make the correction in remove_client line 219 from an earlier response?

Let me check the entire script.


I might strip out my suggestion, and just implement the suggestions with what existed before.
0
 
LVL 81

Expert Comment

by:arnold
ID: 40271783
Here is a the corrected script (no syntax errors)
Since I do not have a setup, can not test the functionality.

 Most issues were within the remove_client

TFTP, NFS do not need to be restarted when contents are added, removed, or modified
for nfs changes to exports file only require the use of exportfs -a to deal with the changes.

reloading DHCPD is only needed when adding/removing clients.
script.ksh.txt
0
 

Author Comment

by:apunkabollywood
ID: 40271798
Thank you so much Arnold for your advice and help ...

stil some issues while removing client. Script is running but when going for removing client it gives below error:

#################################
### Cristie Management Script ###
#################################

1 - Start Cristie services

2 - Stop Cristie services

3 - Show Cristie status

4 - Show Cristie configuration for a specific client

5 - Add a Cristie client

6 - Modify a Cristie client

7 - Remove a Cristie client

8 - Exit

Your choice ?
7

1 123
./newtest.sh[336]: COUNT: parameter not set
./newtest.sh[336]: DHCP_CLIENTS[@]: parameter not set


In fact i have 3 entries in dhcp but its giving only 1.

here is my dhcpd.conf:

host 123 {#123
#client network interface in backup vlan : df#123
#networker server : fd,df#123
#storage node : gfgggf,gfg#123
#platform : fd#123
hardware ethernet df;#123
fixed-address fsd;#123
server-name "gfg";#123
next-server gfg;#123
}#123

host 456 {#456
#client network interface in backup vlan : fd#456
#networker server : df,df#456
#storage node : f,gf#456
#platform : df#456
hardware ethernet dffd;#456
fixed-address fd;#456
server-name "gf";#456
next-server gf;#456
}#456

host 789 {#789
#client network interface in backup vlan : dr#789
#networker server : f,fs#789
#storage node : et,srggr#789
#platform : sfg#789
hardware ethernet sf;#789
fixed-address dfgtf;#789
server-name "srggr";#789
next-server srggr;#789
}#789
0
 

Author Comment

by:apunkabollywood
ID: 40271821
Okay got some sucess

I have change below line in list_client()
DHCP_CLIENTS[$COUNT]="$i"
to
DHCP_CLIENTS[$count]="$i"

and

in remove()
i have declare
DHCP_CLIENTS=0

But now below issue occurs.



#################################
### Cristie Management Script ###
#################################

1 - Start Cristie services

2 - Stop Cristie services

3 - Show Cristie status

4 - Show Cristie configuration for a specific client

5 - Add a Cristie client

6 - Modify a Cristie client

7 - Remove a Cristie client

8 - Exit

Your choice ?
7

1 123
2 456
3 789
4 toto
5 ds

q) quit l) list Clients
What is your Choice?  5
./newtest.sh[337]: parameter: parameter not set

#################################
### Cristie Management Script ###
#################################

1 - Start Cristie services

2 - Stop Cristie services

3 - Show Cristie status

4 - Show Cristie configuration for a specific client

5 - Add a Cristie client

6 - Modify a Cristie client

7 - Remove a Cristie client

8 - Exit

Your choice ?
7

1 123
2 456
3 789
4 toto
5 ds

q) quit l) list Clients
What is your Choice?  l
./newtest.sh[337]: ${!DHCP_CLIENTS[@]}: bad substitution
0
 
LVL 81

Expert Comment

by:arnold
ID: 40271997
I have the script dealing with removing entries worked out with the exception of handling where there are no more client entries left.


Had to rearrange the logic within remove_client function.
I'll post the script later on today.
0
 

Author Comment

by:apunkabollywood
ID: 40272274
Thanks Arnold - waiting for it - I need it to deploy tonight.
0
 

Author Comment

by:apunkabollywood
ID: 40272430
Hi Arnold - really thanks for your efforts but it would be great if you give some help in adding one more functionality in this script :

like adding  below lines to dhcpd.conf  only when if the below subnet / subnet IP dosent exist already:

subnet 192.168.1.0 netmask 255.255.255.0 {
   range 192.168.1.10 192.168.1.100;
}

Thank you :(
0
 
LVL 81

Expert Comment

by:arnold
ID: 40272912
Here is the script that meets...

Personally, when there is text munipulation I would often use perl for scripting.

In a similar case to yours, I would consider going with a database backend data from which can be used to recreate dhcpd.conf if dhcpd with a database backend was not possible.

Same approach can be used to see if a subnet is defined. list the existing subnets, though you have to take account when you have subdivided subnets.
......
script2.ksh.txt
0
 

Author Comment

by:apunkabollywood
ID: 40273104
Agreed but i dont have time for this - i could go for perl but i have today only to finalize this :(

i have run this script - have a look:

 ./experts.sh
cp: cannot stat `/home/arnoldk/dhcpd.conf': No such file or directory
#################################
### Cristie Management Script ###
#################################

1 - Start Cristie services

2 - Stop Cristie services

3 - Show Cristie status

4 - Show Cristie configuration for a specific client

5 - Add a Cristie client

6 - Modify a Cristie client

7 - Remove a Cristie client

8 - Exit

Your choice ?
7

grep: /home/arnoldk/dhcpd.conf: No such file or directory
The following Clients exist

./experts.sh: line 196: DHCP_CLIENTS[@]: unbound variable


Now i did some changes to my orginal and replaced it with your list_client and remove_client and below is the output:
 ./newtest.sh
#################################
### Cristie Management Script ###
#################################

1 - Start Cristie services

2 - Stop Cristie services

3 - Show Cristie status

4 - Show Cristie configuration for a specific client

5 - Add a Cristie client

6 - Modify a Cristie client

7 - Remove a Cristie client

8 - Exit

Your choice ?
7

The following Clients exist
1 123
2 abcd56
3 789
4 toto
5 ds
6 OBS

./newtest.sh: line 196: test: too many arguments
q) quit l) list Clients
What is your Choice?  1
1 is made: 6
0

./newtest.sh: line 222: test: : integer expression expected
Wrong Choice - No Entry with this name
The Following Clients exist
1 ) 123
2 ) abcd56
3 ) 789
4 ) toto
5 ) ds
6 ) OBS

q ) quit l ) List_clients)
What is your Choice?


Please advice
For you - you can run this script - it has no dependecies on any service or platform.
0
 
LVL 81

Expert Comment

by:arnold
ID: 40273282
I run, the portion I am trying to to fix.
It worked for me on a centos box.

Is line 222 deals with the test "$choice" -ge '0'
Replace '0' with "1"

Use ksh -v new test.sh
Post the line with the error, if any.
0
 
LVL 81

Expert Comment

by:arnold
ID: 40274274
Here is one more attempt.

Post your current script if you are not using the one I am posting.

you can use diff -BruN script3.ksh.txt newtest.sh
to see the differences.
script3.ksh.txt
0
 

Author Comment

by:apunkabollywood
ID: 40274430
Your current script error below:

7 - Remove a Cristie client

8 - Exit

Your choice ?
7

The following Clients exist
1 123
2 abcd56
3 789
4 toto
5 ds
6 OBS

experts2.sh[351]: ${!DHCP_CLIENTS[@]}": bad substitution

#################################
### Cristie Management Script ###
#################################

1 - Start Cristie services

2 - Stop Cristie services

3 - Show Cristie status

4 - Show Cristie configuration for a specific client

5 - Add a Cristie client

6 - Modify a Cristie client

7 - Remove a Cristie client

8 - Exit

Your choice ?



and here is the difrence in my past script and your current script

[root@localhost tmp]# diff -BruN experts2.sh newtest.sh
--- experts2.sh 2014-08-20 20:03:59.839285208 +0200
+++ newtest.sh  2014-08-20 16:15:44.279298848 +0200
@@ -1,4 +1,4 @@
-#!/bin/ksh
+#!/bin/bash
 set -u
 clear

@@ -21,7 +21,7 @@
                 exit 255
                 ;;
 esac
-#DHCPD=/home/arnoldk/dhcpd.conf
+
 cp $DHCPD $DHCPD.`date +%d%d%Y_%H%M`

 cristie_start()
@@ -214,7 +214,7 @@
       return
 fi
 echo "$choice is made: ${#DHCP_CLIENTS[@]}"
-if test "$choice" -ge "1" -a   "$choice" -le ${#DHCP_CLIENTS[@]} ; then
+if test "$choice" -ge '0' -a   "$choice" -le ${#DHCP_CLIENTS[@]} ; then
        echo `grep "\#" $DHCPD | grep "^host " >/dev/null; echo $?`
        TCLIENT=$(grep "\#" $DHCPD |grep "^host " |awk '{print $2}' | grep "${DHCP_CLIENTS[$choice]}" > /dev/null)
        echo "$TCLIENT"
0
 

Author Comment

by:apunkabollywood
ID: 40274449
I have to remove DHCPD=/home/arnoldk/dhcpd.conf this line to run your script sucessfully after that this error comes bad subsitution ....and i have tried to run with both ksh and bash but same.
0
 

Author Comment

by:apunkabollywood
ID: 40274570
Hi Arnold when i changed /bin/ksh to /bin/bash the it runs further .....but again error ...this time diffrent:

2 - Stop Cristie services

3 - Show Cristie status

4 - Show Cristie configuration for a specific client

5 - Add a Cristie client

6 - Modify a Cristie client

7 - Remove a Cristie client

8 - Exit

Your choice ?
7

The following Clients exist
1 123
2 abcd56
3 789
4 toto
5 ds
6 OBS

./experts2.sh: line 198: test: too many arguments
q) quit l) list Clients
What is your Choice?  1
1 is made: 6
0

./experts2.sh: line 224: test: : integer expression expected
Wrong Choice - No Entry with this name
The Following Clients exist
1 ) 123
2 ) abcd56
3 ) 789
4 ) toto
5 ) ds
6 ) OBS

q ) quit l ) List_clients)
What is your Choice?
0
 
LVL 81

Expert Comment

by:arnold
ID: 40275106
What is your default shell on the system?
When you run ./newtest.sh your default shell might be running,
Try
Ksh ./newtest.sh
I.e. Your deafult shell is csh, tcsh, zsh and while the top line has #!/bin/bash it still might be executed by the local shell.
Certain command have different context between the shells.

I ran the script with ksh script3.sh and for the remove portion functionality of your script and it worked without a problem.


Look at the -gt '0' change it back to -gt "1"
0
 

Author Comment

by:apunkabollywood
ID: 40275376
i have tried ksh too ....but bad luck exact same substitution error experts3.sh[350]: ${!DHCP_CLIENTS[@]}": bad substitution


I have tried ksh -v experts3.sh but same error.

can you try with bash at your enviroment?
0
 

Author Comment

by:apunkabollywood
ID: 40275381
can we add some simple trick in if statement while removing ....mean to say we are getting list with number 1,2,3 now we are looking for much trustable loop ...but i need it now asap ...so is there any way ?
0
 
LVL 81

Expert Comment

by:arnold
ID: 40275504
We are running on the same platform centos.

I need to have the same DHCPd.conf that you are using.  The sample you provided might not run into issues that yours does because of it.
0
 

Author Comment

by:apunkabollywood
ID: 40275517
cat /etc/dhcp/dhcpd.conf

host 123 {#123
#client network interface in backup vlan : df#123
#networker server : fd,df#123
#storage node : gfgggf,gfg#123
#platform : fd#123
hardware ethernet df;#123
fixed-address fsd;#123
server-name "gfg";#123
next-server gfg;#123
}#123

host abcd56 {#abcd56
#client network interface in backup vlan : fd#abcd56
#networker server : df,df#abcd56
#storage node : f,gf#abcd56
#platform : df#abcd56
hardware ethernet dffd;#abcd56
fixed-address fd;#abcd56
server-name "gf";#abcd56
next-server gf;#abcd56
}#abcd56

host 789 {#789
#client network interface in backup vlan : dr#789
#networker server : f,fs#789
#storage node : et,srggr#789
#platform : sfg#789
hardware ethernet sf;#789
fixed-address dfgtf;#789
server-name "srggr";#789
next-server srggr;#789
}#789

host ds {#ds
#client network interface in backup vlan : 3abcd#ds
#networker server : re,sd#ds
#storage node : r3abcdf,es#ds
#platform : dsfdfd#ds
hardware ethernet abcd3;#ds
fixed-address we;#ds
server-name "es";#ds
next-server es;#ds
}#ds
0
 

Author Comment

by:apunkabollywood
ID: 40275528
I have tried on centos 6.2

here is the output

- just i have removed arnold named dhcp and ksh to bash


#################################
### Cristie Management Script ###
#################################

1 - Start Cristie services

2 - Stop Cristie services

3 - Show Cristie status

4 - Show Cristie configuration for a specific client

5 - Add a Cristie client

6 - Modify a Cristie client

7 - Remove a Cristie client

8 - Exit

Your choice ?
7

The following Clients exist
1 abc
2 123

./cristie.sh: line 196: test: too many arguments
q) quit l) list Clients
What is your Choice?  1
1 is made: 2
0

./cristie.sh: line 222: test: : integer expression expected
Wrong Choice - No Entry with this name
The Following Clients exist
1 ) abc
2 ) 123

q ) quit l ) List_clients)
What is your Choice?


DHCP File :

 cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample
#   see 'man 5 dhcpd.conf'
#

host abc {#abc
#client network interface in backup vlan : dssd#abc
#networker server : 23,ewd#abc
#storage node : de,23#abc
#platform : dwe#abc
hardware ethernet 32;#abc
fixed-address 3223;#abc
server-name "23";#abc
next-server 23;#abc
}#abc

host 123 {#123
#client network interface in backup vlan : 32#123
#networker server : d,34#123
#storage node : 34,fd#123
#platform : 3244#123
hardware ethernet ef;#123
fixed-address ew;#123
server-name "fd";#123
next-server fd;#123
}#123
0
 
LVL 81

Accepted Solution

by:
arnold earned 2000 total points
ID: 40275538
Ok, here it is.
The issue with the bash, there was unexpected results coming from using the test command, on line 223 and 197 made some changes dealing with the testing.

It will generate a list with only the q ) quit and l ) list options without clients when non are present.

I think There was some discussion on EE on a similar matter (bash and some built-in commands not behaving as expected.
script4.sh.txt
0
 

Author Comment

by:apunkabollywood
ID: 40276325
Thank you so much Arnold - finally it works ....really appreciated  your expertise .

Now i just need to add subnet adding function to script ..but will open another case for that if required .

Once again Thank you so much.
0
 

Author Closing Comment

by:apunkabollywood
ID: 40276329
Very good !!
0
 
LVL 81

Expert Comment

by:arnold
ID: 40276511
If I might suggest, look into using a database mysql, progresql, etc. as the backend.
i.e. it could be simpler to look through the database and delete (mark a record as deleted, expire, etc.) a record while using a using a simple script that will query the database, format and output the data to a conf file when a direct dhcpd with mysql backend is not possible.

Oh, on the bash and test side, to avoid unexpected results, using the /usr/bin/test instead of test in any bash script should eliminate those unexpected .........

After some thought, a suggestion dealing with subnets, build an array where the subnet, is the index.
subnet 192.168.0.0 mask 255.255.255.0
ListOfSubnets=`grep '^subnet' | awk ' { print $2 } '`
for i in ($ListOfSubnets)
do
subnet_list[$i]='subnet'
done

Then when entering a subnet you can test whether it already exists using the ${subnet[$entry]}

While you maintain multiple copies of the configuration files, it could be time consuming to locate the erroneous change if it is not immediately noticed.

glad I could help.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question