To send ID and Password for each command using expect feature in bash script


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
To send ID and Password for each command using expect feature in bash script

Dear Tech Guys,

I am trying to send some commands on the local server and it always asks for user name and password after each command. To serve the purpose I am using expect function as follows:

Code:
#!/usr/bin/expect

set timeout 20



spawn "./data1.sh"

expect "Please Enter UserName: " { send "admin\r" }
expect "*Password: " { send "admin123\r" }

interact

where data1.sh have the say:

Code:
#!/bin/bash
command1
command2
command3

Now the problem is that, my expect script provides username and password for only command1 but not providing username and password for command2 onwards and stops at the username prompt. Please help me out to troubleshoot this issue.
# 2  
Does your OS have the sudo command?

Putting passwords in a script is not all secure.

If you would please tell us: your OS, and your shell, we could give you a good answer - which may be an expect script.
# 3  
Its as follows:

NAME="Red Hat Enterprise Linux Server"
VERSION="7.3 (Maipo)"

-bash

--- Post updated at 12:38 PM ---

there is no sudo option. its the need of command to enter id password after that so must be needing such kind of solution.
# 5  
Its an application based command which shows the routing table just as below. After each such get command it asks for the user id and password. Problem with my script is that it is successfully taking id password for 101 and displaying the routing table but after that it executes the get command for 102 but doesn't takes id and password and stuck at the user name prompt.

Code:
cmcli get 101
cmcli get 102
cmcli get 103

# 6  
Try a here document, assumes it it asks for a user, then outputs a line feed and wants password:

Code:
#!/bin/bash
cmdcli get 101 <<!
user
pass
!
cmdcli get 102 <<!
user
pass
!
cmdcli get 103 <<!
user
pass
!

# 7  
Quote:
Originally Posted by Xtreme
Dear Tech Guys,

I am trying to send some commands on the local server and it always asks for user name and password after each command. To serve the purpose I am using expect function as follows:

Code:
#!/usr/bin/expect

set timeout 20



spawn "./data1.sh"

expect "Please Enter UserName: " { send "admin\r" }
expect "*Password: " { send "admin123\r" }

interact

where data1.sh have the say:

Code:
#!/bin/bash
command1
command2
command3

Now the problem is that, my expect script provides username and password for only command1 but not providing username and password for command2 onwards and stops at the username prompt. Please help me out to troubleshoot this issue.
Quote:
Originally Posted by Xtreme
Its an application based command which shows the routing table just as below. After each such get command it asks for the user id and password.
Code:
cmcli get 101
cmcli get 102
cmcli get 103

If i connect that correctly, a command like:

Code:
cmcli get 101

asks itself for a user/password. Your expect-script provides that only once, not for every command. My suggestion is to do it the other way round. Put the following in a script "execasadmin.exp":

Code:
#!/usr/bin/expect
set timeout 20
set mycmd [lindex $argv 0];

spawn $mycmd

expect "Please Enter UserName: " { send "admin\r" }
expect "*Password: " { send "admin123\r" }

interact

This will expect a command line as argument, execute this commandline, then feed it the user and password. Call this in a loop like this:

Code:
#! /bin/bash

mycmdarr[1]="command1"
mycmdarr[2]="command2"
mycmdarr[3]="command3"
mycmdarr[4]="command4"
# [...]
i=1

while [ $i -le ${#mycmdarr[@]} ] ; do
     /path/to/execasadmin.exp "${mycmdarr[$i]}"
     (( i++ ))
done

exit 0

Notice that there might be difficulties once you include redirections, pipelines and other advanced shell features into your commands, but for single/simple commands it should work.

I hope this helps.

bakunin
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
Password check in bash script calling on expect
2legit2quit
password check in bash script calling on expect Background: I have to copy a file from one server, to over 100 servers in a test environment. once the file is copied, it requires to have the permissions on the file changed/verified. These are all linux servers. most of them have the same...... Shell Programming and Scripting
1
Shell Programming and Scripting
Bash Script: Send files to SFTP using Expect
krux_rap
I have to send few gzipped files from local server to SFTP server. My Server Info Distributor ID: Ubuntu Description: Ubuntu 12.04.4 LTS Release: 12.04 Codename: precise Created a bash script and could able to send files to sftp, but i want to send email if transfer is successful. ...... Shell Programming and Scripting
1
Shell Programming and Scripting
Make a password protected bash script resist/refuse “bash -x” when the password is given
frad
I want to give my long scripts to customer. The customer must not be able to read the scripts even if he has the password. The following command locks and unlocks the script but the set +x is simply ignored. The code: read -p 'Script: ' S && C=$S.crypt H='eval "$((dd if=$0 bs=1 skip=//|gpg...... Shell Programming and Scripting
7
Shell Programming and Scripting
Command substitution in send/expect. Please help!
thulasidharan2k
Hi, the following code is not working. How can I cat the last modified file in the path /asdf. Please help! expect "asdf%" {send "cat `ls -rt /asdf|tail -1` \r"}... Shell Programming and Scripting
2
Shell Programming and Scripting
Use Send command of Expect package
neha123
HI All, I am currently working on one command line application on AIX (Unix Platform).Here i need to use Expect package. By using Expect package at the top of the script,i want to use just Send command of Expect package to send characters like, 1. Press Enter key 2. Press spacebar 3....... Shell Programming and Scripting
6
Shell Programming and Scripting

Featured Tech Videos