Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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'm trying to use an expect script to ssh to a host, execute a command, and return the output of the command to a variable. However, the variable gets filled with the output from the spawn directive:
So the result I expect in "chkselinux" is either "1" (if selinux is on), or something else (usually "cat /selinux/enforce: file not found"). However, what actually goes in the variable is all the output of the spawn directive, plus the output of the ssh command embedded in the middle.
Of course, if I can set up host keys, I can just run an ssh command from the bash script, but that is not an option in this environment.
Interact may work, but how do I tell the bash script to end the expect script?
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Because you embedded the call of the expect script in ``, all the output of the expect script goes into the variable chkselinux.
Expect echoes all output to stdout, and that is what is put as result from calling in ``
(Just a tip, use $(...) instead of `...`. It is equal, but is avoids reading mistakes)
What answer do you actually expect from your expect script (no pun intended). Should it tell you whether your login was succesful? How does that fit with the output of "cat /selinux/enforce"? Where do the variables $user, $password etc get their values from in the expect script?
I am trying to help, but I might be more succesful if I understand what you try to achieve.
Thanks for your reply - the user, password, and hostname arguments are provided by the bash script that calls the expect script, and it reads them from a file. That part of the script works - it logs in as expected and runs the desired commands. I just want to capture ONLY the output from the command, not the entire login process.
I tried using a log_file argument, but that also only captures the login process and not the single command I want to run.
Basically, I want to be able to do the same thing as ssh, assuming one has installed one's id_dsa.pub key in the server's .ssh/authorized_keys file:
Code:
output_var=$(ssh user@host "some_command")
But do it with an expect session.
BTW I know about the $() construct, but the script was written by my boss, and I don't want to throw him off
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
I see exactly what you mean.
it shouild be soooo simple to surpress the Expect output and seems to be impossible.
Maybe you could write a proc for the expect part in tcl, catch the output in a tcl var, process it to see if the expected string is in the result, and pass that result back to bash. Something like:
Thanks for the code suggestion, I may try that out - I did end up re-writing the code to just use ssh commands with installed authorized_keys, though with the number of servers we have, I need to use expect to copy the keys up the the various servers 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.