Help me solve this scripting problem please

Hello,

I would really appreciate some help into approaching this problem:

  • i have a random txt file with x lines and y rows following this pattern:

ex:

ip1 | user1 | command
ip2 | user2 | command
ip3 | user3 | command
  • i need to telnet/ssh into these ip's, login with corresponding user, and give corresponding command.

I was thinking at a while loop, assigning for 1st line first word to $ip variable, 2nd word to $user var, 3rd word to $command variable, then execute required action and pass to next line.

Can you guys tell me if it's an easier approach than this, and how i can assign those variables based on a delimiter ?

Thanks!

did you try anything ?

well not yet, because :

  1. i can't figure out yet how to parse every line depending on loop number (line 1 - loop 1, etc)
  2. didn't yet find how to break a line based on specific delimiter

1) parse every line --- read about the while loop
2) delimeter ---- read about the cut command or awk command.

just search in this forum and you will get lot and lot of answers for your problem.

let us know, still you need a help.

we are ready to help and not ready to write script for you :slight_smile:

As a start you could have a look into a while -loop combined with the read command. For the field separator most shells have the variable IFS you could alter.

I'm using this piece of code to export in current.txt a text line corresponding to the sequence in the loop:

awk "NR==$sequence{print;exit}" $file > /var/www/html/current.txt

Problem is that if i run it from browser doesn't seem to work (txt file does not modify, stays the same, showing first line); in shell code works as expected.
I've tried double-quoting, single-quoting the variable without succes.

Any ideas what am i doing wrong, or another option to do this?

while read lines
do

ip=`echo $lines | awk -F"|" '{print $1}'  `
user=`echo $lines | awk -F"|" '{print $2}'  `
cmd=`echo $lines |awk -F"|" '{print $3}'  `

       echo "$ip $user $cmd"

done < configfile.txt

i am writing some script for u , follow the syntax and it will work .change your script accordingly

 
su_string=" su(unix)"
if [ $OS = REDHAT ]
then
                   grep  $unix /var/log/messages > su.txt