Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get expect -c to work in single line rather than script

Running:

my_machine~/opt/ams/data/ep/success$ expect -c "spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml [email protected]:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml; expect { '*password:*' { send 'ad'\r\n }}"

Does not seem to work as I am still asked for the password.

spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml [email protected]:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml
[email protected]'s password: 

If I run it as ascript it runs ok.

my_machine~/opt/ams/data/ep/success$ ./try.sh
spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml [email protected]:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml
[email protected]'s password:
xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml                                                                      100%   13MB  13.2MB/s   00:01
my_machine~/opt/ams/data/ep/success$ cat try.sh
#!/bin/bash
expect -c "
        spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml [email protected]:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml
        expect {
          "*password:*" { send "ad"\r\n; interact }
          eof { exit }
        }
        exit
        "

my_machine~/opt/ams/data/ep/success$

I would like to run this in a one line command rather than a script. Has anyone got any ideas?

Thanks in advance

A

I answered my own question below

like image 222
amadain Avatar asked Jun 30 '10 12:06

amadain


People also ask

What is the expect command?

The Linux expect command takes script writing to an entirely new level. Instead of automating processes, it automates running and responding to other scripts. In other words, you can write a script that asks how you are and then create an expect script that both runs it and tells it that you're ok.

What is Interact command in Expect script?

Interact is an Expect command which gives control of the current process to the user, so that keystrokes are sent to the current process, and the stdout and stderr of the current process are returned.

How expect works?

Expect is used to automate control of interactive applications such as Telnet, FTP, passwd, fsck, rlogin, tip, SSH, and others. Expect uses pseudo terminals (Unix) or emulates a console (Windows), starts the target program, and then communicates with it, just as a human would, via the terminal or console interface.


1 Answers

Got it: The following code scps a file called Sean_Lilly.zip from my box to another box without entering a password:

expect -c "spawn /usr/bin/scp Sean_Lilly.zip [email protected]:/opt/ams/epf_3_4/Sean_Lilly.zip; sleep 5; expect -re \"password\"; send \"ad\r\n\"; set timeout -1; expect -re \"100%\";"

I know this can be done by setting passwordless ssh access between the two boxes but I wanted to do it in one command line using expect. Thanks fuzzy lollipop for the inspiration. Note if you run expect -d -c "spawn ... you get excellent debug on what is happening including whether your regex is good enough

like image 125
amadain Avatar answered Oct 10 '22 03:10

amadain