LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Unidentified error in bash script that notepad ++ is not catching (https://www.linuxquestions.org/questions/linux-newbie-8/unidentified-error-in-bash-script-that-notepad-is-not-catching-888238/)

tfarnsworth74 06-24-2011 10:12 PM

Unidentified error in bash script that notepad ++ is not catching
 
Hello,


I've been debugging the following script all evening and am now stuck...this script is supposed to ssh to a router, pull interface conf, 'cut' 3 pieces of info, and then print the results of 2 as well as the 3rd (after running a ping cmd on the linux box):



SCRIPT_CODE

#! /bin/bash

lb1="0"
lb2="3";
device=core1.sjo1
while `env test "$lb1" -le "$lb2"`
do
`ssh orcadmin@$device "sh run int lo $lb1" ; "exit" > conf_info1`
echo "`cat conf_info1`"
`cat conf_info1 | grep Loopback > lb_int`
`cat conf_info1 | grep address > lb_addr`
`cat conf_info1 | grep description > lb_desc`
interface=`cut -f2 lb_int -d " "`
address=`cut -f3 lb_addr -d " "`
description=`cut --characters=14- lb_desc`
`echo $interface $address`
pinginfo=`ping "$address"`; printf "\n $pinginfo"
echo "$device $interface is reachable, as the following shows, $pinginfo"
lb1=`expr $lb1 + 1`
done



SCRIPT RUNNING:

[linux]$ sh dev*
orcadmin@core1.sjo1's password:
Connection to core1.sjo1 closed by remote host.
: command not found

ping: unknown host

core1.sjo1 is reachable, as the following shows,



--Any assistance/add'l_info would be greatly appreciated

lithos 06-25-2011 03:41 AM

Hi

I don't think that this line
Code:

`ssh orcadmin@$device "sh run int lo $lb1" ; "exit" > conf_info1`
lets the script to stay connected, in fact I think that you should implement a "password" for the user "orcadmin" to log in as this line states it requires password:
Quote:

orcadmin@core1.sjo1's password:
after that the script will run
Quote:

`cat conf_info1 | grep Loopback > lb_int`
`cat conf_info1 | grep address > lb_addr`
`cat conf_info1 | grep description > lb_desc`
...

tfarnsworth74 07-09-2011 11:19 AM

I figured out that there is no way to specify a password via ssh...apparently the linux/unix ssh application expects the password to arrive via a TTY session (user input)...thus, the best way I could find, to resolve my issue, was to run an expect script within the shell script...thanks much for your assistance

grail 07-09-2011 11:58 AM

You do know that the construct `` is designed to allow you to return information, generally to a variable.
So your prolific use of it is not doing anything. In fact I am surprised that the script runs at all.
As an example:
Code:

#!/bin/bash

tada=blah

`echo $tada`

When run:
Code:

$ ./test.sh
./test.sh: line 5: blah: command not found


chrism01 07-10-2011 07:03 PM

To automate ssh, use auth keys instead of putting passwd in a script.

Also, as mentioned, using backquotes is not reqd when running a cmd normally.
You do need it to collect the results of a cmd into a var, so
Code:

# backquotes not reqd
cat somefile
echo $var1
var2=$var1

# reqd
var1=`cat somefile`



All times are GMT -5. The time now is 05:45 PM.