Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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}
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)
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.
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?
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
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.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803
Rep:
Quote:
Originally Posted by michaelk
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.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.