Need help in implementing expect


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in implementing expect
# 1  
Old 11-22-2011
MySQL Need help in implementing expect

Hello All,

I am trying a shell script for automatically login to test servers and pulling the output of top command from all using expect.

----snippet of code ---
Code:
#!/usr/bin/expect -f
#!/bin/bash
server1=10.251.222.51
server=("$server1")
i=1
for exp_server in ${server[@]}; do
 expect -c "
 spawn ssh @rtq1@$server
 expect {
 "*password:*" { send $Password\r\n;
 expect "*[/home/rtq1]"
 send "last reboot > file.txt"
 expect "*[/home/rtq1]"
 }
 eof {exit}
 exit
 "
 let "1=1+1"
 done

This is throwing error as shown below
==================================
Code:
$ ./sshlogin
invalid command name "server1=10.251.222.51"
    while executing
"server1=10.251.222.51"
    (file "./sshlogin" line 3)

=====================================
Please help me out in finding the error or give me another logic which can do the work...

Thanks
RR


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-22-2011 at 04:02 AM.. Reason: Code tags
# 2  
Old 11-22-2011
If you can configure passwordless ssh between the machines, it will be lot more easier and secure. Any thought on that?

--ahamed
# 3  
Old 11-22-2011
No. Passwordless login will not allow in my test servers
# 4  
Old 11-22-2011
Try this...
Code:
#!/bin/bash
password="password"
server1="10.251.222.51"
server=($server1)

for exp_server in ${server[@]}; 
do
        expect -c "
        spawn ssh $exp_server last reboot > /tmp/log
        expect \"*password* \" { send -- \"$password\n\" }
        interact; exit" 
done

We strongly advice you to use passwordless ssh!!!

HTH
--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Expect script returning string following a found expect.

I'm fairly new to scripting so this might not be possible. I am using Expect with Cisco switches and need to capture the string after finding the expect request. For example, when I issue "show version" on a Nexus switch, I'm looking to capture the current firmware version: #show version ... (0 Replies)
Discussion started by: IBGaryA
0 Replies

2. Programming

Calling expect script inside another expect

Hi, Am very new to expect scripting.. Can You please suggest me how to call an expect script inside another expect script.. I tried with spawn /usr/bin/ksh send "expect main.exp\r" expect $root_prompt and spawn /usr/bin/ksh send "main.exp\r" expect $root_prompt Both... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

3. Shell Programming and Scripting

Expect - Comparison of expect value and loop selection

Hello All, I am trying to automate an installation process using expect and sh script. My problem is that during the installation process the expected value can change according to the situation. For Example if this is a first time installation then at step 3 I'll get "Do you want to accept... (0 Replies)
Discussion started by: alokrm
0 Replies

4. Shell Programming and Scripting

Help with implementing logging

I'm trying to add logging to an existing script which echos a number of lines to the screen. I've added a switch to the script that is going to suppress much of this output and put it in a file instead. The way I envisioned it was like this: $log would be set to either "" or the log files... (8 Replies)
Discussion started by: cheetobandito
8 Replies

5. Shell Programming and Scripting

Need help with Expect script for Cisco IPS Sensors, Expect sleep and quoting

This Expect script provides expect with a list of IP addresses to Cisco IPS sensors and commands to configure Cisco IPS sensors. The user, password, IP addresses, prompt regex, etc. have been anonymized. In general this script will log into the sensors and send commands successfully but there are... (1 Reply)
Discussion started by: genewolfe
1 Replies

6. Shell Programming and Scripting

Implementing Password

I am trying to implement a login screen to the following code how would i go about doing so. I have try to place the password in a variable using if statements which would usually work but as i have the system in a while loop i think i need to find another method. #!/bin/bash #Filename:... (4 Replies)
Discussion started by: warlock129
4 Replies

7. Shell Programming and Scripting

strange expect script behavior, or am i misunderstanding expect scripting?

Hello to all...this is my first post (so please go easy). :) I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Discussion started by: v1k0d3n
2 Replies

8. Programming

Implementing the redirection

Hi all I am facing a problem with redirection. Its somewhat related to parsing. I am following the following steps. 1. take the command and tokenize it. 2. if redirection is there then give it to redirection unit 3. if pipe is there give it to piping unit. 4. do until the command ends ... (0 Replies)
Discussion started by: mobile01
0 Replies

9. Programming

Implementing a shell in C

Hi, I am implementing a shell in C, with the following problem... Suppose the shell is invoked from the command line as >> myshell < test.in > test.out 2>&1 I have to execute the commands in test.in and redirect them to test.out How does one detect in the main function that the shell... (1 Reply)
Discussion started by: jacques83
1 Replies

10. Programming

Implementing a shell

I'm implementing a shell in C that supports piping, output redirection, and background processing, and a few other commands. I was wondering how I'd go about implementing the output redirection. So, I'd open a file and I'd fork and execute the command. But how would I get stdout into the file? Any... (10 Replies)
Discussion started by: ununium
10 Replies
Login or Register to Ask a Question