Can't get expect to work. send will not display an output
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
Can't get expect to work. send will not display an output
I'm trying to ssh into multiple servers and get data to display on the screen. I can ssh in just fine. But once I'm in, I can't seem to get anything to display using the send command. Here's the expect part when the prompt appears:
so the main problem is no matter what command I try to place, it seems to skip it and just display the send_user stuff. Am I missing something to this? I just want to run simple things like cat or ls and display a result. Can anyone help?
well you never actually send a valid command. the cat has no return at the end - \r - and the exit has it wrong - /r. You managed it correctly on the password entry, why not elsewhere?
well you never actually send a valid command. the cat has no return at the end - \r - and the exit has it wrong - /r. You managed it correctly on the password entry, why not elsewhere?
I don't know why it won't work. This is the first time (using expect) that I'm trying to do screen outputs like this. I've made password change script and a user creation script using expect. Those work fine because I know what I'm "expecting" (which is usually a prompt) and then I send the next command. Here it just doesn't seem to work for me. This is only the 3 script I've ever written in expect.
actually the /r was just a typo from copying it over. and i've included the \r at the end of the cat and it looks like it skips it and doesn't display the output of cat.
no, that doesn't make sense. The whole point is that you need to expect something that will only be there after your command has completed, e.g. another bash prompt
I've done that but I get the command echoing. I would like something to this effect as an output:
Latest file in directory: hosts
And to get that output I would run something like this in a normal situation:
ls -ltr /etc | head -1 | cut -d" " -f3 (i'm not sure if the command is right but it's suppose to just grab the file name)
And to script it, I would do:
send_user "Latest file in directory: "
send "ls -ltr /etc | head -1 | cut -d" " -f3\r"
But if I did it this way, I get an echo of the command and it ruins the output.
Now, I tried doing this:
set $result [exec {ls -ltr /etc | head -1 | cut -d" " -f3}]
send_user "Latest file in directory: $result"
and that is the type of output I would want, BUT, that command is run on the server I'M on and not the server that I've expect ssh'd into.
I think I can do this if I figure out the spawned ID and assign the command to that ID but I don't see examples on how to do that.
I've written a password change script and a user name add/delete/reset password script and they work fine because I run commands right after a prompt and I do a log_user 0 so I don't need to see anything on screen. But what I'm trying to do here is just display information by ssh'ing into many machines.
I've managed to find a command that helps. I found using "set temp [string trimright "ls -ltr /etc | head -1 | cut -d\" \" -f3\r] works to eliminate the echo.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.