Intermittent problem reading from an input file.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Intermittent problem reading from an input file.
# 1  
Old 07-09-2009
Intermittent problem reading from an input file.

First of all thanks to all for the good post, and the great site. I'm a noob, but I've been able to learna a lot by checking past posts.

I haven't been able to make sense of a problem that I've been working on for a while, hopefully someone can help me out. The script I wrote telnets into several routers via an expect script to check the numbers of CDP and EIGRP neighbors on each switch. The input file includes a list of the routers, the number of neighbors that should be seen and the number of problems since a successful run. Here's a sample of the source file:

Code:
 
#Input file for the department network script. Each router is checked
#for the following IP addresses as Eigrp neighbors and CDP neighbors:
#10.240.*.* , 10.241.*.* , 10.243.*.* , 10.244.*.* , 172.16.*.*
#192.168.*.* , 0.0.0.0
#The format for adding new entries is:
#Router Name, total Neigbors, problem count (0)
#agpcat1 1 0
agccat1 6 0
agpcat 6 0
anncat1 5 0


The first portion of the script ignores any line with # to start then reads the router name into a variable and sends that along with some predefined password stuff to an expect script. The output of the expect script is redirected to a file with the same name as the router. Later in the script I grep through each file looking for lines with IP addreses and count the number of lines to verify that it's the same as what's in the input file.

I run the script via cron every 5 minutes. The whole script works perfectly 99% of the time, but once or twice a day it doens't seem to parse the file normally. It seems to be including at least part of the first lines that are to be commented out and then it also misses part of the routers names. For instance I'll see a text file in the directory named 0.0.txt and ccat1.txt, when the 0.0.0.0 par of the input file should be skipped entirely and the ccat1.txt should be agccat1.txt.

Here's the first part of my script that seems to be causing me trouble. Any help you can give would be greatly appreciated.

Code:
 
#!/bin/ksh
C="/apps/scripts/Test/"
cd $C
b="username"
c="password"
d="enablepassword"
ulimit -nS 1024
 
while read ROUTER EIGRPNEI PROBLEMCOUNT
do
    if [[ "$ROUTER" != *#* ]] ; then
        ${C}report.exp $ROUTER $b $c $d > ${C}${ROUTER}.txt 2>/dev/null &
    fi
done < ${C}sourcefile
wait

# 2  
Old 07-09-2009
One idea is that if you make sure you always have a space after the # for the comment lines then the line:
Code:
 if [[ "$ROUTER" != *#* ]] ; then

could then be:
Code:
 if [[ "$ROUTER" != "#" ]] ; then

instead which might be more reliable?

Last edited by TonyFullerMalv; 07-20-2009 at 07:41 PM..
# 3  
Old 07-09-2009
Thanks Tony...Good idea, I"ll give it a try. Since it's so sporadic it will take me a day or so till i can let you know if it worked.
# 4  
Old 07-09-2009
resolve

#!/bin/ksh
C="/apps/scripts/Test/"
cd $C
b="username"
c="password"
d="enablepassword"
ulimit -nS 1024


mainloop(){
while read ROUTER EIGRPNEI PROBLEMCOUNT
do
${C}report.exp $ROUTER $b $c $d > ${C}${ROUTER}.txt 2>/dev/null &
done < ${C}sourcefile
wait

}


while read line; do
cat<<EOF|grep -v -e "#"|mainloop
$line
EOF
done
# 5  
Old 07-10-2009
sighK,
Thanks for the reply. I'm not sure i understand the changes you are suggesting. Can you explain the reasoning?

Last edited by Wallygooo32; 07-10-2009 at 09:59 AM..
# 6  
Old 07-10-2009
simple

it looks like you will use your script as

cat $file|yourscript.ksh

so I took the main loop of your script, encapsulated it into a function within the script

I then have it read every line, use grep to find anything without # so all input to your original loop will be comment free

grep -v the -v flag means find without match to sanitize the input to your function.

You could always keep your original script - the if statement and use

cat $file|grep -v -e "#"|yourscript

and that may rid some of the confusion inside of the script.



I hope this makes scence.
# 7  
Old 07-10-2009
Thanks for the extra info. It makes more sense now that I know you were assuming i was running it like this:
cat $file|yourscript.ksh

The source file is basically static so the file it reads through is hardcoded into the script - i don't pass anything to the main ksh script.

I made a copy of the script and I'm trying a variation of what you suggested. I changed the read loop so that it greps the file prior to reading the lines. I removed the cat statement since it seem like it might be a UUOC - if there's a good reason to cat the file then pipe to grep let me know. Here's the read loop I'm testing.

Code:
grep -v "#" ${C}sourcefile | while read ROUTER EIGRPNEI PROBLEMCOUNT
do
    ${C}report.exp $ROUTER $b $c $d > ${C}${ROUTER}.txt 2>/dev/null &
done
wait

Thanks again!!

---------- Post updated at 02:38 PM ---------- Previous update was at 02:30 PM ----------

Tony,

i tried your suggestion and still had some problems...thanks for the idea though.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

User input while reading from a file

I am not able to capture the user input in this script(bash).There is prompt for user input.Could some one help me capture user input while reading afile? while read line do echo "$i" path1=$line path2=`echo $line|sed s/new_dir/old_dir/` echo "Do you want to replace?";... (4 Replies)
Discussion started by: parijat guh
4 Replies

2. Shell Programming and Scripting

Reading user input...problem with tab key

Hi all, I have a little problem with my shell script (reading user input, save user input to variable, invisible characters in the log file :() printf "1. What's your file path?" /path/to/my/file read -e FILE I have invisible characters in my log file (e.g. <ESC> or ^G) when I'm... (3 Replies)
Discussion started by: splendid
3 Replies

3. Shell Programming and Scripting

Problem in reading the input value

echo "Enter the Value : " read value sed '1s:\(.\{6\}\)\(.\{4\}\):\1'$value':' flextran$RUN_DATE-completed.txt > temp.txt mv temp.txt flextran$RUN_DATE-completed.txt on the run time after entering the input value it waits for keystroke and the values is not input to the function The output... (4 Replies)
Discussion started by: rammm
4 Replies

4. UNIX for Dummies Questions & Answers

Help in reading the date from the input file name

Hi, I need to read the date from the input file. The format of the input file is as follows: a_b_c_yyyymmdd.txt I need to read the date(yyyymmdd) part from the name of the input file. Would really appreciate if someone can help me in this regard Thanks a lot. (1 Reply)
Discussion started by: Sunny_teotia
1 Replies

5. Shell Programming and Scripting

[SH] Problem reading input in script

Alright, so the goal of my script is to read text from standard input and store it into a file using the ex-editor: so far i've got this, but it doesn't work. #!/bin/s read text ex $1 >> HERE text HERE I don't get any errors either, so i don't know what i'm doing wrong. (7 Replies)
Discussion started by: Bertieboy7
7 Replies

6. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

7. Shell Programming and Scripting

reading input from a file

I am trying to read input for a C program (that expects input from the user) from a file using the shell command: progname < filename but it seems that the program considers the char '<' as the first input, hence causing an "error" in my program. I checked it with another program and it... (2 Replies)
Discussion started by: nadbar
2 Replies

8. Solaris

Intermittent Connection and Samba problem

Hi all , I just installed a Solaris10 on x86 machine , running on vmware . I was able to telnet from my local machine to the solaris ( the one running on the vmware) , but the connection was pretty slow and intermittent . Is there any setting that I should customize to ensure the telnet... (2 Replies)
Discussion started by: osca7578
2 Replies

9. UNIX for Advanced & Expert Users

FTP Problem (Intermittent)

I am having problem with ftp not indicating any error, but my customer is complaining that their response file is not present on their machine. This only happens a couple of times a day. Is there a debugging option I can turn on to the trace the ftp command. I have a return code displayed and it... (2 Replies)
Discussion started by: rob11g
2 Replies

10. Shell Programming and Scripting

Help reading an input file in KSH

First I' d like to say you guys are awesome. :) I have a word document that I cut and paste into Textpad and it removed all the fancy formatting which is fine with me. I WinScp'd it to the box and and called it inputfile.txt. Opened it in vi and don't see any special characters or stuff that... (2 Replies)
Discussion started by: zilla30066
2 Replies
Login or Register to Ask a Question