Expect script not passing password / commands ??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect script not passing password / commands ??
# 1  
Old 03-04-2019
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; 03-04-2019 at 09:07 PM..
# 2  
Old 03-04-2019
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 03-04-2019
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 03-05-2019
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 Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute commands on remote server using expect script?

I need to copy python script file to around 100 servers using expect script. 1. Copy script to my user home first(/home/myhome) on each remote server 2. change permissions on copied file to 766. 3. sudo to appuser1 account on remote server. copy script file from my user home to /usr/bin/... (1 Reply)
Discussion started by: kchinnam
1 Replies

2. Shell Programming and Scripting

SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All, I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use... (5 Replies)
Discussion started by: ss112233
5 Replies

3. Shell Programming and Scripting

Trouble passing commands with expect

Hello All, I hope someone could help me with this. I'm creating a shell script to run a process. The trouble is, part of the process has to be ran as a different user. I can 'su' to the user ok, but I'm having trouble passing a 'cd' command as well as some variables I set earlier in the... (1 Reply)
Discussion started by: bbbngowc
1 Replies

4. Shell Programming and Scripting

Passing username and password to a script running inside "expect" script

Hi I'm trying to run a script " abc.sh" which triggers "use.sh" . abc.sh is nothing but a "expect" script which provides username and password automatically to the use.sh script. Please find below the scripts: #abc.sh #!/usr/bin/expect -f exec /root/use.sh expect "*name*" send... (1 Reply)
Discussion started by: baddykam
1 Replies

5. Shell Programming and Scripting

Passing Password to SSH without using expect in a Script

How can I pass password in SSH command without using expect in a shell program. I don't have expect installed on my Solaris server. #!/bin/bash ssh user@hotname (how to supply pass in script?:wall:) Experts please help its very urgent. Shrawan Kumar Sahu (4 Replies)
Discussion started by: ss135r
4 Replies

6. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

7. Shell Programming and Scripting

Expect Script sending password with $ and symbols

All, I am trying to use expect to send SFTP password because I am unable to share a key with the vendor. They gave me a password that uses some symbols in it like $ and ! When i try to use the send command in expect it thinks the $ is a variable. Is there anyway to have it send the... (2 Replies)
Discussion started by: markdjones82
2 Replies

8. UNIX for Advanced & Expert Users

Encrypt the password ,source it in a expect script...!!

Hello folks I have a conf file ,say 'pass.conf' ,which is storing ascii password : PASS1111. I need to encrypt this password once and store it in a file. I ,then need to write a script which would read this encrypted password and decrypts it.The o/p o this script shud be this decrypted... (8 Replies)
Discussion started by: ak835
8 Replies

9. Shell Programming and Scripting

Expect Script....encrypt password and use

Could someone please help me...I have an expect script. There's a need for a log in during the script and a password is required...right now the password is just a variable in the expect script...what would be the best way to put that in an encrypted flat file and have the expect script pull the... (2 Replies)
Discussion started by: cubs0729
2 Replies

10. Shell Programming and Scripting

Password changing in a Script (shell and expect)

Hi, Does anybody know how to change the password on multiple servers with a script. I have 300 Sun boxes and the password expiry is set to 30 days. Im in a process to build a script using expect. Need a help from an expert who has already done it. Regards, Vinod (1 Reply)
Discussion started by: chellam
1 Replies
Login or Register to Ask a Question