ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I copied an Expect example script and tried to modify it for my needs, sad to say it's not working at all
What I'm trying to achieve:
1) Get an output containing 'x' data
2) Use the 'x' data in a for loop, and run it recursively for the # of instances in the original output
3) Write the final output into a file
Here is my script:
Code:
#!/usr/bin/expect
# Script to get list of all rules that have
# logging enabled
log_user 0
puts "\r"
puts "Checking for firewall rules with Logging enabled..."
puts "\r"
# Connect to the firewall with login credentials
spawn ssh -l testuser 192.168.10.8 -p4118
expect "Password:"
send "testtest\r"
expect "SW>"
set outcome {}
set writeout [open "config.txt" "w"]
# Send the command 'show rule' to the firewall, which
# will output a list of all rules and write it to the
# config.txt file.
exp_send "show rule\r"
expect {
-regexp {..*} {
set outcome "${outcome}$expect_out(0,string)"
exp_continue
}
}
puts $writeout $outcome
close $writeout
send "\r"
expect "SW>"
# Trying to read the config.txt file,
# grep the 2nd column, and run the
# 'show rule x' command. Replace 'x'
# with the actual rule number.
set newout {}
set newfile [open "newfile.txt" "w"]
set load_fh [open "config.txt" r]
set rulenum [split [read $load_fh] "\n"]
close $load_fh
foreach rule $rulenum {
if $rulnum != ""} {
exp_send "show rule $rulenum\n"
}
}
expect {
-regexp {..*} {
set newout "${newout}$expect_out(0,string)"
exp_continue
}
}
puts $newfile $newout
close $newfile
send "\r"
expect "SW>"
send "exit\r"
expect eof
I would truly appreciate if someone could point out what I'm doing wrong.
My problem is that I'm not too sure how to use the data in the output file, to start a for loop.
So I do actually get a config.txt file generated with the appropriate data. This data contains a column called 'Rule Name' which I want to assign to $rulenum. I then want the command 'show rule $rulenum' to run until all the Rule Names are exhausted, then output that to a file called newfile.txt.
I'm really bad at verbalizing my thoughts, my apologies.
But I just looked at the man page, and noted the spawn and fork commands, and also the -d debug switch.
Further, consider asking a moderator (use the "Report" button) to move this thread to the "Programming" sub-forum. You'll be much more likely to get meaningful responses if the thread is there.
My problem is that I'm not too sure how to use the data in the output file, to start a for loop.
So I do actually get a config.txt file generated with the appropriate data. This data contains a column called 'Rule Name' which I want to assign to $rulenum. I then want the command 'show rule $rulenum' to run until all the Rule Names are exhausted, then output that to a file called newfile.txt.
I'm really bad at verbalizing my thoughts, my apologies.
Thanks
So the file config.txt, has all the data you need to loop through? The issue is you don't know how to open the file to loop through it? I think I got what you are saying...
This should get your going:
# Process data file
set data [split $file_data "\n"]
foreach line $data {
# do some line processing here
}
You should be able to make your connection outside of the loop, so you are connected to one session.
Quote:
Originally Posted by PTrenholme
First, a caveat: I've never used expect.
But I just looked at the man page, and noted the spawn and fork commands, and also the -d debug switch.
Further, consider asking a moderator (use the "Report" button) to move this thread to the "Programming" sub-forum. You'll be much more likely to get meaningful responses if the thread is there.
I wouldn't consider it programming, but it would probably be better suited there. expect is just a subset of the tcl language.
Last edited by trey85stang; 08-30-2011 at 02:35 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.