ssh through "expect" in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ssh through "expect" in shell script
# 1  
Old 09-08-2011
ssh through "expect" in shell script

Hi,

I am trying to use "Expect" in shell script to ssh and do some work in remote server but I am unable to connect. Here is the code I am using.

#save as test.sh
Code:
set ip "10.10.10.10"
set username "uname"
set password "upass"
spawn ssh $username@$ip
expect "Password:"
send "$password\r"
#expect "->"
#send "ls\r"

Doing above gives me error:
./test.sh[4]: spawn: not found
couldn't read file "Password:": no such file or directory
./test.sh[6]: send: not found

System Configuration:
"Solaris 5.10"
expect -v gives "expect version 5.31.2"

Another thing is I cannot find expect in /usr/bin/ or /usr/local/bin/ though "man expect" and "expect -v" is working.

Thanks for help.
# 2  
Old 09-08-2011
If you use ssh keys, you won't need to kludge in insecure stored-plaintext passwords in with a third-party utility unavailable on most machines.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-08-2011
Thanks Corona, Can you let me know how to use ssh keys. Do you mean RSA keys?

---------- Post updated at 04:33 PM ---------- Previous update was at 04:32 PM ----------

Also, I cannot install OpenSSH in my system as I don't have all permission rights.
# 4  
Old 09-08-2011
There are heaps of articles on the web showing how to setup public/private RSA keys for ssh for example Using SSH keys for Password-less login .

It dosn't necessarily require Openssh to use them, if your server already has a non-Openssh version of sshd that should be fine.

Once this is working you should be able to do:
Code:
$ ssh uname@$ip id

Without typing a password.

BTW There are a few reasons why the expect script didn't work:
1. Use type expect to see where it is being run from
2. You need #!/path/to/expect on the first line of your script - otherwise it will be run by sh not expect
3. ssh will not allow passwords to come from a non tty session like expect - it has been specially coded to disallow this.

Last edited by Chubler_XL; 09-08-2011 at 09:01 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 09-09-2011
Can't confirm statement 3. of Chubler_XL. I'm using expect for ssh connections.
One might think of extending the expect command to handle the ssh login state machine,
e.g. something like this:
Code:
      expect {
        "continue connecting (yes/no)? " {
          exp_send "yes\r"
          exp_continue
        }
        -nocase "password:" {
          exp_send "$password\r"
          exp_continue
        }
        "Permission denied" {
          puts "\ninvalid user/password\n"
        }
        "->" {
          puts "\nssh login successful\n"
        }
      }

This User Gave Thanks to hfreyer For This Post:
# 6  
Old 09-09-2011
We can also generate the key through ssh-keygen command and copy that key on that server where you want to ssh.
This User Gave Thanks to rohit22hamirpur For This Post:
# 7  
Old 09-09-2011
Thanks Everyone.

I am trying ssh-keygen.

Home addresses after logging into local and remote hosts are:
Local host: /mm/ums/home/user1/
Remote host: /home/user2/
The users are different, may be due to this I am getting error, I don't know.

I did the following steps:
(1) ssh-keygen -t rsa in local host
(2) Used default passphrase. id_rsa and id_rsa.pub are created in /mm/ums/home/user1/.ssh
(3) ftp to remote host and copied id_rsa.pub to /home/user2/.ssh/authorized_keys with access 600.

Now when I am doing ssh user2@<remote-host-ip>, its asking me for password.
Can someone tell me where I went wrong.

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Expect: spawn id exp5 not open while executing "expect "$" { send "sudo su -\r" }"

Hi All, i am trying to ssh to a remote machine and execute certain command to remote machine through script. i am able to ssh but after its getting hung at the promt and after pressing ctrl +d i am gettin the out put as expect: spawn id exp5 not open while executing "expect "$" {... (3 Replies)
Discussion started by: Siddharth shivh
3 Replies

3. Shell Programming and Scripting

Can someone please show me a very simple "expect" script to change password in Solaris please?

Ladies & Gents, Can one of you gurus please show me a very simple "expect" script to change the password in Solaris in a script, please? Nothing fancy, no error checking, no nothing. Just to change the password of a new user, it's all. Many thanks in advance. U guys have honestly earned my... (1 Reply)
Discussion started by: Hiroshi
1 Replies

4. Shell Programming and Scripting

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (14 Replies)
Discussion started by: thisissouvik
14 Replies

5. AIX

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (2 Replies)
Discussion started by: thisissouvik
2 Replies

6. UNIX for Dummies Questions & Answers

What is the significance of sh -s in ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh?

Please can you help me understand the significance of providing arguments under sh -s in > ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh (4 Replies)
Discussion started by: Sree10
4 Replies

7. Shell Programming and Scripting

Shell script using expect to login to couple of remote servers and read "crontab -l"

I need a shell script using expect to login to couple of remote servers and read "crontab -l -u <username>" & "cat /etc/rc.local" & "df -h" and able to create output into a file saved locally with hostname.crontab & hostname.rc.local & disk.status. I can supply a file as list of hostname or IP... (4 Replies)
Discussion started by: jaipsharma
4 Replies

8. Shell Programming and Scripting

exec perl in expect script yields "invalid command"

I'm trying to execute something like this: exec perl -i -pe 's/\015/\012/g' '${file}' in my expect script and I get: error "invalid command name \"perl\". however, if I run perl -i -pe 's/\015/\012/g' "/Users/Shared/menu-items.txt" directly in my terminal, it runs fine. I'm an... (4 Replies)
Discussion started by: dpouliot
4 Replies

9. AIX

"too big" and "not enough memory" errors in shell script

Hi, This is odd, however here goes. There are several shell scripts that run in our production environment AIX 595 LPAR m/c, which has sufficient memory 14GB (physical memory) and horsepower 5CPUs. However from time to time we get the following errors in these shell scripts. The time when these... (11 Replies)
Discussion started by: jerardfjay
11 Replies

10. Shell Programming and Scripting

Need Help on "Expect" command in shell programming

Hi, Iam using Solaris 10 OS in the server.iam unable to find expect command in bin directory.how can i install the script in the server to use "expect" in shell programming. (1 Reply)
Discussion started by: sudhakaryadav
1 Replies
Login or Register to Ask a Question