Operating System - HP-UX
1831515 Members
3107 Online
110025 Solutions
New Discussion

I need help making this work... getnodeinfo: syntax err

 
SOLVED
Go to solution
Rob Johnson_3
Regular Advisor

I need help making this work... getnodeinfo: syntax err

Again, here's the deal. I have a file with a bunch of IP addresses. I took everything out except for three to show you what I'm trying to do. This file contains addresses of a bunch of routers and switches. Most have TACACs configured but some don't. I'm trying to identify the ones that aren't configured. If the device has TACACs, when telneting, it will prompt for Username: - If it doesn't have TACACs, it will prompt for Password: -.

Here's the error message when I try to execute the dolist script.

$ ./dolist
/export/home/rjohnson/expectstuff/getnodeinfo: syntax error at line 26: `}' unexpected
/export/home/rjohnson/expectstuff/getnodeinfo: syntax error at line 26: `}' unexpected
/export/home/rjohnson/expectstuff/getnodeinfo: syntax error at line 26: `}' unexpected
$


The scripts are shown below.
$ pwd
/export/home/rjohnson/expectstuff
$ ls
dolist getnodeinfo list script.original
$ more dolist
#!/bin/sh
for device in `cat /export/home/rjohnson/expectstuff/list`
do
/export/home/rjohnson/expectstuff/getnodeinfo $device
done
$ more list
10.21.21.13
10.21.21.11
10.21.253.11
$ more getnodeinfo
set username "ciscoworks"
set password "thepasswordforciscoworks"
while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "Username:"
send "$username\r"
expect "Password"
send "$password\r"
send "\r\r"
"Welcome" {
incr pass0
set n 5
set p 0
close
}
"from" {
send "id\r"
expect {
$username {
incr pass0
set n 5
close
}
}
}
"login: $" {
set pass1 0
puts stdout "Expect return: $expect_out(0,string)"
puts stdout "Failed login"
send "\x1d\x0d"
expect "telnet"
send "close\r"
send "quit\r"
close
}
}
incr n
}
$
11 REPLIES 11
Thierry Poels_1
Honored Contributor

Re: I need help making this work... getnodeinfo: syntax err

hi,

at least the getnodeinfo seems to be missing an expect directive. Either use "expect getnodedinfo" or put "#!/yourpath/expect" as first line of the getnodeinfo script.

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Rob Johnson_3
Regular Advisor

Re: I need help making this work... getnodeinfo: syntax err

I added the #!/pathtoexpect/expect to the getnodeinfo file. Now I have a whole new set of errors!!!

$ head -2 getnodeinfo
#!/opt/sfw/bin/expect
set username "ciscoworks"
$ ./dolist
can't read "n": no such variable
while executing
"while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "Username:"
send "$username\r"
expect "Password"
send "$password\r"
send "..."
(file "/export/home/rjohnson/expectstuff/getnodeinfo" line 4)
can't read "n": no such variable
while executing
"while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "Username:"
send "$username\r"
expect "Password"
send "$password\r"
send "..."
(file "/export/home/rjohnson/expectstuff/getnodeinfo" line 4)
can't read "n": no such variable
while executing
"while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "Username:"
send "$username\r"
expect "Password"
send "$password\r"
send "..."
(file "/export/home/rjohnson/expectstuff/getnodeinfo" line 4)
$
c_51
Trusted Contributor

Re: I need help making this work... getnodeinfo: syntax err

now you'll have to define n before you use it

set n 0
while { $n < 5 } {
Sergejs Svitnevs
Honored Contributor

Re: I need help making this work... getnodeinfo: syntax err

Also, you have to add to the top of getnodeinfo file after string #!/path_to_expect/expect:

set HOSTNAME [lindex $argv 0]

Regards,
Sergejs
Rob Johnson_3
Regular Advisor

Re: I need help making this work... getnodeinfo: syntax err

Can you be more specific on the the HOSTNAME setting. If you haven't already noticed, I'm terrible when it comes to scripting.

I did set the value for n and that error message is taken care of.
Rob Johnson_3
Regular Advisor

Re: I need help making this work... getnodeinfo: syntax err

My bad. I didn't think it would be exactly how you typed it
set HOSTNAME [lindex $argv 0]

Here's what I'm getting now.

$ ./dolist
spawn /bin/sh
telnet 10.21.21.13
$ Trying 10.21.21.13...
Connected to 10.21.21.13.
Escape character is '^]'.

***********************************************************
Unauthorized use of DC Government computing systems may
involve not only transgression of DC Government policy,
but also a violation of state and federal laws.

Unauthorized use is a crime and may involve criminal and
civil penalties. Violators will be prosecuted to the full
extent of the law.

Here's the getnodeinfo script
*********************************************

#!/opt/sfw/bin/expect
set username "ciscoworks"
set password "just4us"
set HOSTNAME [lindex $argv 0]
set n 0
while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "Username:"
send "$username\r"
expect "Password"
send "$password\r"
send "\r\r"
"Welcome" {
incr pass0
set n 5
set p 0
close
}
"from" {
send "id\r"
expect {
$username {
incr pass0
set n 5
close
}
}
"login: $" {
set pass1 0
puts stdout "Expect return: $expect_out(0,string)"
puts stdout "Failed login"
send "\x1d\x0d"
expect "telnet"
send "close\r"
send "quit\r"
close
}
}
incr n
}


Keyboard inputs are monitored in the event of a real or
perceived security incident. Logon constitutes agreement
to monitoring of activities.
************************************************************

User Access Verification

Username: ciscoworks
Password: invalid command name "Welcome"
while executing
""Welcome" {
incr pass0
set n 5
set p 0
close
}"
("while" body line 10)
invoked from within
"while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "Username:"
send "$username\r"
expect "Password"
send "$password\r"
send "..."
(file "/export/home/rjohnson/expectstuff/getnodeinfo" line 6)
spawn /bin/sh
telnet 10.21.21.11
$ Trying 10.21.21.11...
Connected to 10.21.21.11.
Escape character is '^]'.

***********************************************************
Unauthorized use of DC Government computing systems may
involve not only transgression of DC Government policy,
but also a violation of state and federal laws.

Unauthorized use is a crime and may involve criminal and
civil penalties. Violators will be prosecuted to the full
extent of the law.

Keyboard inputs are monitored in the event of a real or
perceived security incident. Logon constitutes agreement
to monitoring of activities.
************************************************************

User Access Verification

Username: ciscoworks
Password: invalid command name "Welcome"
while executing
""Welcome" {
incr pass0
set n 5
set p 0
close
}"
("while" body line 10)
invoked from within
"while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "Username:"
send "$username\r"
expect "Password"
send "$password\r"
send "..."
(file "/export/home/rjohnson/expectstuff/getnodeinfo" line 6)
spawn /bin/sh
telnet 10.21.253.11
$ Trying 10.21.253.11...
Connected to 10.21.253.11.
Escape character is '^]'.

***********************************************************
Unauthorized use of DC Government computing systems may
involve not only transgression of DC Government policy,
but also a violation of state and federal laws.

Unauthorized use is a crime and may involve criminal and
civil penalties. Violators will be prosecuted to the full
extent of the law.

Keyboard inputs are monitored in the event of a real or
perceived security incident. Logon constitutes agreement
to monitoring of activities.
************************************************************

User Access Verification

Username: ciscoworks
Password: invalid command name "Welcome"
while executing
""Welcome" {
incr pass0
set n 5
set p 0
close
}"
("while" body line 10)
invoked from within
"while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "Username:"
send "$username\r"
expect "Password"
send "$password\r"
send "..."
(file "/export/home/rjohnson/expectstuff/getnodeinfo" line 6)
$
c_51
Trusted Contributor

Re: I need help making this work... getnodeinfo: syntax err

just like for n, you'll need to set HOSTNAME before you use it.

"while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"

you'll need to do this before the while loop.

if you do as has been suggested:

set HOSTNAME [lindex $argv 0]

this will set HOSTNAME to the first value passed on the command line, i.e. whatever $device has been set to (10.21.21.13, etc.)
Rob Johnson_3
Regular Advisor

Re: I need help making this work... getnodeinfo: syntax err

I'm sorry, my last response wasn't put in very well.

Here's what I'm getting when I run the dolist script.

********************************************
$ ./dolist
spawn /bin/sh
telnet 10.21.21.13
$ Trying 10.21.21.13...
Connected to 10.21.21.13.
Escape character is '^]'.

***********************************************************
Unauthorized use of DC Government computing systems may
involve not only transgression of DC Government policy,
but also a violation of state and federal laws.

Unauthorized use is a crime and may involve criminal and
civil penalties. Violators will be prosecuted to the full
extent of the law.

Keyboard inputs are monitored in the event of a real or
perceived security incident. Logon constitutes agreement
to monitoring of activities.
************************************************************

User Access Verification

Username: ciscoworks
Password: invalid command name "Welcome"
while executing
""Welcome" {
incr pass0
set n 5
set p 0
close
}"
("while" body line 10)
invoked from within
"while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "Username:"
send "$username\r"
expect "Password"
send "$password\r"
send "..."
(file "/export/home/rjohnson/expectstuff/getnodeinfo" line 6)
spawn /bin/sh
$ telnet 10.21.21.11
Trying 10.21.21.11...
Connected to 10.21.21.11.
Escape character is '^]'.

***********************************************************
Unauthorized use of DC Government computing systems may
involve not only transgression of DC Government policy,
but also a violation of state and federal laws.

Unauthorized use is a crime and may involve criminal and
civil penalties. Violators will be prosecuted to the full
extent of the law.

Keyboard inputs are monitored in the event of a real or
perceived security incident. Logon constitutes agreement
to monitoring of activities.
************************************************************

User Access Verification

Username: ciscoworks
Password: invalid command name "Welcome"
while executing
""Welcome" {
incr pass0
set n 5
set p 0
close
}"
("while" body line 10)
invoked from within
"while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "Username:"
send "$username\r"
expect "Password"
send "$password\r"
send "..."
(file "/export/home/rjohnson/expectstuff/getnodeinfo" line 6)
spawn /bin/sh
$ telnet 10.21.253.11
Trying 10.21.253.11...
Connected to 10.21.253.11.
Escape character is '^]'.

***********************************************************
Unauthorized use of DC Government computing systems may
involve not only transgression of DC Government policy,
but also a violation of state and federal laws.

Unauthorized use is a crime and may involve criminal and
civil penalties. Violators will be prosecuted to the full
extent of the law.

Keyboard inputs are monitored in the event of a real or
perceived security incident. Logon constitutes agreement
to monitoring of activities.
************************************************************

User Access Verification

Username: ciscoworks
Password: invalid command name "Welcome"
while executing
""Welcome" {
incr pass0
set n 5
set p 0
close
}"
("while" body line 10)
invoked from within
"while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "Username:"
send "$username\r"
expect "Password"
send "$password\r"
send "..."
(file "/export/home/rjohnson/expectstuff/getnodeinfo" line 6)
$
Here's the getnodeinfo script

*********************************************
$ more getnodeinfo
#!/opt/sfw/bin/expect
set username "ciscoworks"
set password "just4us"
set HOSTNAME [lindex $argv 0]
set n 0
while { $n < 5 } {
spawn "/bin/sh"
send "telnet $HOSTNAME\r"
sleep 1
expect "Username:"
send "$username\r"
expect "Password"
send "$password\r"
send "\r\r"
"Welcome" {
incr pass0
set n 5
set p 0
close
}
"from" {
send "id\r"
expect {
$username {
incr pass0
set n 5
close
}
}
"login: $" {
set pass1 0
puts stdout "Expect return: $expect_out(0,string)"
puts stdout "Failed login"
send "\x1d\x0d"
expect "telnet"
send "close\r"
send "quit\r"
close
}
}
incr n
}
$


c_51
Trusted Contributor
Solution

Re: I need help making this work... getnodeinfo: syntax err

instead of: expect "Password"

use: expect "Password*"

then, after send "\r\r"

expect "Welcome" {

and then make sure your braces are matching up, {}'s
Rob Johnson_3
Regular Advisor

Re: I need help making this work... getnodeinfo: syntax err

That did it!!!

Thanks so much for the help. I so much wish I was programmed to be a programmer.

Rob Johnson_3
Regular Advisor

Re: I need help making this work... getnodeinfo: syntax err

I've spent the last three days posting this to a couple of other forums and made it absolutely nowhere. I posted this only a few hours ago to ITRC and the problem is now solved. That shows there are truly some experts on this forum.
Thanks so much.