LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-04-2019, 03:35 PM   #1
jroquet
LQ Newbie
 
Registered: Aug 2019
Posts: 4

Rep: Reputation: Disabled
getting error when finishing script with expect


Hi all, this script with expect runs fine but when it finish reading the file it gives an error as per below

device> spawn ssh user@
ssh: : node name or service name not known
expect: spawn id exp8 not open
while executing
"expect eof"
(file "./script_check_power_pe.sh" line 38)

#!/usr/local/bin/expect


set timeout 7

#Looks for list of devices to modify in a PER.txt
set f [open "1.txt"]
set hosts [split [read $f] "\n"]
close $f

foreach host $hosts {

# Defines possible prompts
set prompt "(%|#|\\$|>|~| $"
spawn ssh username@$host

expect {
"Are you sure you want to continue connecting (yes/no)" {send "yes\r"exp_continue}

"assword:" {
send "password\r"
expect -re $prompt


expect -re $prompt
send "show chassis environment pem\r"
sleep 4
expect -re $prompt
send "show system uptime\r"


expect -re $prompt
sleep 2
send "exit\r"
}
}
}
expect eof

script runs fine but at the end of the script gives this error
user@device> spawn ssh user@
ssh: : node name or service name not known
expect: spawn id exp8 not open
while executing
"expect eof"
(file "./script_check_power_pe.sh" line 38)

how can I get rid of this error?

thanks in advance
 
Old 08-04-2019, 03:57 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
proper formating and plaement may be an issue here.

Code:
#!/usr/local/bin/expect


set timeout 7

#Looks for list of devices to modify in a PER.txt
set f [open "1.txt"]
set hosts [split [read $f] "\n"]
close $f

foreach host $hosts {

# Defines possible prompts
set prompt "(%|#|\\$|>|~| $"
spawn ssh username@$host
#expect is here 
expect {
"Are you sure you want to continue connecting (yes/no)" {send "yes\r"exp_continue}

"assword:" {
send "password\r"
expect -re $prompt


expect -re $prompt
send "show chassis environment pem\r"
sleep 4
expect -re $prompt
send "show system uptime\r"


expect -re $prompt
sleep 2
send "exit\r"
	}
	expect eof
	} #end bracket for expect is here, maybe this is it
	#you are not in the place it should be.
}
many things to learn here.
please, use code blocks while posting you code in here, thank you. That helps too make it easier to read as well. that is the # sign in advance mode. Just place your code between then open and close blocks. <HTML> lingo / jargon.

Last edited by BW-userx; 08-04-2019 at 04:00 PM.
 
Old 08-04-2019, 04:50 PM   #3
jroquet
LQ Newbie
 
Registered: Aug 2019
Posts: 4

Original Poster
Rep: Reputation: Disabled
getting error when finishing script with expect Reply to Thread

Hi thanks for the prompt response, unfortunately still showing same error

#############
#!/usr/local/bin/expect


set timeout 7

#Looks for list of devices to modify in a PER.txt
set f [open "1.txt"]
set hosts [split [read $f] "\n"]
close $f

foreach host $hosts {

# Defines possible prompts
set prompt "(%|#|\\$|>|~| $"
spawn ssh user@$host

expect {
"Are you sure you want to continue connecting (yes/no)" {send "yes\r"exp_continue}

"assword:" {
send "pass\r"
expect -re $prompt


expect -re $prompt
send "show chassis environment pem\r"
sleep 4
expect -re $prompt
send "show system uptime\r"


expect -re $prompt
sleep 2
send "exit\r"
}
expect eof
}
}
#########################

user@device> spawn ssh user@
ssh: : node name or service name not known

any ideas?

thanks
 
Old 08-04-2019, 05:08 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
unfortunately you don't seem to want to learn about code tags, two I know absolutely nothing about expect, three you're beginning to look like you want someone else to figure out what you need to learn, how to figure out why ones own code is not working.

what trouble shooting have you tried on it so far to see the why it is not doing what you want it to do?

what is that telling you?
Code:
sh: : node name or service name not known

Last edited by BW-userx; 08-04-2019 at 05:12 PM.
 
Old 08-04-2019, 10:42 PM   #5
jroquet
LQ Newbie
 
Registered: Aug 2019
Posts: 4

Original Poster
Rep: Reputation: Disabled
hi, the output is saying that when the script reaches the end of the .txt where the hostnames are, keeps on trying to ssh but obviously cannot find any other hostname.
This is the first time using this forum so a little bit of patience would be very much appreciated.
thanks for your understanding
 
Old 08-05-2019, 07:10 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by jroquet View Post
hi, the output is saying that when the script reaches the end of the .txt where the hostnames are, keeps on trying to ssh but obviously cannot find any other hostname.
This is the first time using this forum so a little bit of patience would be very much appreciated.
thanks for your understanding
what is wrong with asking questions, after all this to is what you are doing.
someones abilities to follow simple instructions plays a big part in here and everywhere.
you seem to be rather apt at picking up on how the two work together, bash and expect, now work on code tags. 12 extra keystrokes, or a simple click of the mouse to obtain them. So to make it conform to this forums ways of doing things in here. and all of the other forums I've been in that one adds code to.


It looks like you know what is going on with the hostname, so there is where you should start looking and experimenting with your code to see if you can get it to do what you want. I have no idea what you want it to do, so that is on you.

do you know how to use conditionals?
what is your programing background?

if it keeps doing something that it shouldn't keep doing then how do you check for it to know when to tell it to stop doing that?
Code:
expect {
"Are you sure you want to continue connecting (yes/no)" {send "yes\r"exp_continue}
it was told to keep sending yes?

one, I have never used this external program to script nor am I seeing the bash script it has to use in order for it to work, so you're leading me and others in half blind by giving only half of the equations to your problem and solutions then asking us for the complete answers to your questions.

Last edited by BW-userx; 08-05-2019 at 07:26 AM.
 
Old 08-05-2019, 07:42 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
If your 1.txt file has a blank line before the end of file marker i.e
Code:
host1
host1
(blank line)
your code will read the blank line as a host name which is why you see:
spawn ssh user@

Last edited by michaelk; 08-05-2019 at 07:48 AM.
 
Old 08-05-2019, 10:59 AM   #8
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by michaelk View Post
If your 1.txt file has a blank line before the end of file marker i.e
Code:
host1
host1
(blank line)
Rather than assume that the file will be in the correct format, it's always best to employ a little defensive programming. Test that the record read is non-blank:
Code:
if {[string length $record] > 0} {
    # Use the content in this record
...
}
Except for maybe an initial version of a script, I never assume the input data is in the desired format. What would the Expect script do if the user later added comments or decided to comment out one of the hostnames? (You'd need to split on '#' first.)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
[root@fugo trace]# sh expect.sh expect.sh: line 9: expect: command not found sivaloga Linux - Kernel 1 08-22-2013 04:29 AM
Screen, Have to attach before finishing script themattbeballin Programming 1 04-01-2011 05:10 PM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM
Need help finishing Bash Script to add new user and map virtual e-mail info. O_Chaos Linux - Newbie 1 02-09-2004 04:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration