Quantcast
Channel: Active questions tagged expect - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 157

Using expect in bash to execute a command with a password request

$
0
0

I have worked for a while on making a BASH script to go through two lists and creating a command out of it.

In the end it should execute the command and synchronize my ZFS server and ZFS backup server.

Only pinnacle is that BASH cant actually insert a password when a command ask's for it.

To my understanding this can be achieved with expect.

The command is stored in $ExecuteSyncoidAnd would look like this

/usr/sbin/syncoid <UserName>@<IP>:Storage/WallaBag Storage/Docker/WallaBag --compress none --sshcipher chacha20-poly1305@openssh.com --sshport <PortNumber> --sshkey </dest/to/keyfile> --no-privilege-elevation

The password is stored in $PWD

now i have to execute the expect from within BASH since im not really in the mood to try and recreate all the code in expect/tcl which i have never tried before.

The expect part is

Enter passphrase for key '<nameOfKey>'

Now i dont understand tcl/expectBut I have made the attempts so far and i just cant make it work even one time

expect -c 'expect "Enter" { spawn $ExecuteCommand; send -- "$PWD\r"; interact }'expect -c 'spawn $ExecuteSyncoid; expect "Enter"; send -- "$PWD\r"; interact'syncoid command/password/usr/bin/expect - <<EOF    spawn $ExecuteSyncoid    expect "Enter"    send -- "$PWD\r"EOF/usr/bin/expect <<EODspawn $ExecuteSyncoidexpect "Enter passphrase for key"send -- "$PWD\r"interactEOD/usr/bin/expect -c 'expect "Enter" { spawn $ExecuteSyncoid; send -- "$PWD\r" }'

I Allso attempted to make a seperate script

expect -f ./SynCoid-IterateThroughDataSets.exp $ExecuteSyncoid $PWD

With the code

#!/usr/bin/expect -f set ExecuteSyncoid [lindex $argv 0];set PWD [lindex $argv 1];spawn $ExecuteSyncoidexpect "Enter"send "\$PWD\r"

But unfortunately i cant figure out how to make expect see the $ExecuteSyncoid command as one consistent command.It wont execute it if i put it in quotos "" and if i dont have it in quotes the $ExecuteSyncoid bash variable is considered multiple arguments in expect.


If anyone knows how to fix this,Or have an idea about what to do i would appreciate it.

Would allso be great if i could sse the output of the expect command as it executes $ExecuteSyncoid

Best Regards,Darkyere


Viewing all articles
Browse latest Browse all 157

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>