Expect script not passing password / commands ??


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 2 Weeks Ago
Expect script not passing password / commands ??

Newbie here. My goal is to have the expect script log into the Ubuntu 18.04 server and run two commands (lsb_release -a and ip addr) and eventually pipe the output/results to a file. For now, I would be happy to get this one command or two to run successfully. How to fix this?

Code:
#!/usr/bin/expect 
set timeout 60 
spawn ssh admin@192.168.1.12
expect " admin@192.168.1.12's password: " 
send " admin\r" 
expect " admin@server1:~$ " 
send " \r" 
expect " admin@server1:~$ "
send " lsb_release -a "
expect " admin@server1:~$ "

Error: I get the password prompt but the script isn't passing the password (??)
Code:
admin@server1:~$ expect test.exp
spawn ssh admin@192.168.1.12
admin@192.168.1.12's password:

Normal response/output using "ssh -l" from the cli.
Code:
admin@server1:~$ ssh -l admin 192.168.1.12
admin@192.168.1.12's password:
Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-45-generic x86_64)

... misc output here

*** System restart required ***
Last login: Mon Mar  4 14:40:27 2019 from 192.168.1.10
admin@server1:~$

Moderator's Comments:
Mod Comment Please use CODE tags for sample input, sample output, AND code segments.

Last edited by Don Cragun; 2 Weeks Ago at 08:07 PM..
# 2  
Old 2 Weeks Ago
Quote:
Originally Posted by jacob600
Newbie here. My goal is to have the expect script log into the Ubuntu 18.04 server and run two commands (lsb_release -a and ip addr) and eventually pipe the output/results to a file. For now, I would be happy to get this one command or two to run successfully. How to fix this?

Code:
#!/usr/bin/expect 
set timeout 60 
spawn ssh admin@192.168.1.12
expect " admin@192.168.1.12's password: " <---
send " admin\r"  <---
expect " admin@server1:~$ "  <---
send " \r" 
expect " admin@server1:~$ " <---
send " lsb_release -a " <---
expect " admin@server1:~$ "

Error: I get the password prompt but the script isn't passing the password (??)
Code:
admin@server1:~$ expect test.exp
spawn ssh admin@192.168.1.12
admin@192.168.1.12's password:

Normal response/output using "ssh -l" from the cli.
Code:
admin@server1:~$ ssh -l admin 192.168.1.12
admin@192.168.1.12's password:
Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-45-generic x86_64)

... misc output here

*** System restart required ***
Last login: Mon Mar  4 14:40:27 2019 from 192.168.1.10
admin@server1:~$

Moderator's Comments:
Mod Comment Please use CODE tags for sample input, sample output, AND code segments.
Why do you have so many <space>s at the start of the strings you expect to see from the remote host? You don't show those leading spaces in the normal output that you get when you login manually.

Why do you have <space>s at the start of the strings you send to the remote host? I assume that "admin" is a sample and not your real password for the user "admin" on that server, but you have specified that the first character of the password you are sending to the remote host is a <space>. Is that really what you want?

Why do you have <space>s at the start of the strings you expect to see from the remote host? You haven't shown us <space>s in those spots in the output you get from the remote hosts when you login manually. Why do you expect the remote host to send those <space>s when you try to login programmatically?

You're probably better off just using expect to look for the end of what you expect the remote host to send instead of looking for everything. There is less chance that you have a typo that will keep you from logging in. (I.e., look for "password: " instead of looking for " admin@192.168.1.12's password: " and include the <space> at the end of that ONLY if the remote server actually includes a <space> in the output it sends when it is asking you to supply your password.)

If you type the command lsb-release -a with a leading and trailing <space>, but do not hit the enter key; what does your local system do? Does it guess that you're done and run the command for you, or does it patiently sit there waiting for you to finish the command by hitting the enter key?

If you want the remote system to run that command, shouldn't you send it a \r to tell it that you're done entering the command?
# 3  
Old 2 Weeks Ago
When debugging these sort of scripts you should have a much shorter timeout. You don't want to wait for 60 seconds for expect to tell you it missed something:

Code:
set timeout 3


Once it's working OK then bump you timeout up to a reasonable value, I still think 60 seconds it too long for a login prompt or for those simple commands to complete.
# 4  
Old 2 Weeks Ago
Is there a reason not to generate an SSH key to provide a passwordless connection? Using expect is trying to break through the well considered security practice of SSH.

If you get it set up, then the client just needs to call in in one line. The following allow for progressively more complex calls according to your need. You only need the simplest one that works for you:-
Code:
ssh $target_server_name $remote_command_to_run
ssh $user_name@$target_server_name $remote_command_to_run
ssh -i $private_key_location $user_name@$target_server_name $remote_command_to_run

Would that not simplify the client side code? Is there a reason to attack SSH with expect? I assume you have credentials to get to each server. This will take a little effort first time to get the SSH connection set up, but then it is so much easier to actually use and leaves your script much cleaner.



Kind regards,
Robin
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Expect script not expecting the password prompt Junaid Subhani Shell Programming and Scripting 1 09-26-2016 05:14 AM
How to execute commands on remote server using expect script? kchinnam Shell Programming and Scripting 1 03-20-2016 10:22 PM
SFTP or scp with password in a batch script without using SSH keys and expect script ss112233 Shell Programming and Scripting 5 04-22-2015 11:10 AM
Password check in bash script calling on expect 2legit2quit Shell Programming and Scripting 1 04-21-2015 12:17 PM
Odd behaviour with Expect password update script whetu Shell Programming and Scripting 0 01-12-2014 01:25 AM
Trouble passing commands with expect bbbngowc Shell Programming and Scripting 1 03-31-2013 07:21 PM
Passing username and password to a script running inside "expect" script baddykam Shell Programming and Scripting 1 08-23-2012 05:39 PM
Passing password for ssh in Script yogeshpawar Shell Programming and Scripting 4 12-16-2011 01:00 AM
Passing Password to SSH without using expect in a Script ss135r Shell Programming and Scripting 4 08-10-2011 08:51 AM
expect script for random password and random commands vanid Ubuntu 0 03-18-2011 01:29 AM
Expect Script sending password with $ and symbols markdjones82 Shell Programming and Scripting 2 12-15-2010 11:47 AM
Encrypt the password ,source it in a expect script...!! ak835 UNIX for Advanced & Expert Users 8 04-13-2010 01:00 PM
passing password in shell script manojbarot1 Shell Programming and Scripting 1 11-18-2008 06:17 AM
Expect Script....encrypt password and use cubs0729 Shell Programming and Scripting 2 12-01-2006 04:07 AM
Password changing in a Script (shell and expect) chellam Shell Programming and Scripting 1 08-14-2006 11:12 PM