Using bash variable with expect - bash

My script look like this , but not getting the password out.
#!/usr/bin/bash
export -p curdir=`basename ${PWD%/*/*/}`
/usr/bin/expect <<EOF
spawn scp -v -i ... <local file> <remote file containing $curdir>
expect -re "Enter passphrase.+:"
send "<password>\n"
exit;
EOF
exit
Something is wrong, as the passsword is evidently not sent. Something wrong with the expect line, the send line, or ???
When this script is executed it is asking for the password.
Thanks for any help.

I made this way too hard. ftp does everything I needed to do. And there are lots of places on the internet that explain how to use it. One can define bash variables as part of a bash script, and the password is simply part of what you define. scp is great of one copy, but ftp as part of a script is really easy.

Related

standard expect works but xargs does not

I am currently trying to improve my script when using snmpwalk and wanted to incorporate using xargs as this would help me gather information from multiple devices at the same time. Taking small steps I created a simple expect file with the following code:
#!/usr/bin/expect -f
set hostname [lindex $argv 0]
spawn snmpwalk $hostname
interact
From there I created a simple bash script to test the functionality of it with the code here:
#!/bin/bash
cfg=$(cat ./ips)
for c in $cfg
do
expect ./EXPECT/01.exp $c
done
Doing this produces the proper information but takes time as it goes through each IP, one at a time.
I then commented the above code out and created the xargs version here:
#!/bin/bash
xargs -I ADDRESS -P4 expect ./EXPECT/01.exp ADDRESS < ./ips
Once I run the bash script, nothing is produced other then the command from the expect file echoing with no other action. I then looked into it and saw that you can add a "-noecho" switch to it but this didn't fix anything.
I then changed the spawn command to exec but the same issue happens and nothing is shown other then an echo of the command.
Would be interested to see what issue could be causing this.
So the answer was rather simple and a bit embarrassing. The expect file needs some type of expect statement before it will run correctly. I simply put in the following expect syntax regardless if it would see this or not and it worked fine:
expect {
"(yes/no)?" {
send "yes\r"
exp_continue
}
}
From here the script worked as expected and it began to run 4 IP's at a time.

Using Expect to fill a password in a bash script

I am relatively new to working in bash and one of the biggest pains with this script I have to run is that I get prompted for passwords repeatedly when running this script. I am unable to pass ssh keys or use any options except expect due to security restrictions but I am struggling to understand how to use expect.
Does Expect require a separate file from this script to call itself, it seems that way looking at tutorials but they seem rather complex and confusing for a new user. Also how do I input into my script that I want it to auto fill in any prompt that says Password: ? Also this script runs with 3 separate unique variables every time the script is called. How do I make sure that those are gathered but the password is still automatically filled?
Any assistance is greatly appreciated.
#!/bin/bash
zero=`echo $2`
TMPIP=`python bin/dgip.py $zero`
IP=`echo $TMPIP`
folder1=`echo $zero | cut -c 1-6`
folder2=`echo $zero`
mkdir $folder1
cd $folder1
mkdir $folder2
cd $folder2
scp $1#`echo $IP`:$3 .
Embedding expect code in an shell script is not too difficult. We have to be careful to get the quoting correct. You'll do something like this:
#!/usr/bin/env bash
user=$1
zero=$2
files=$3
IP=$(python bin/dgip.py "$zero")
mkdir -p "${zero:0:6}/$zero"
cd "${zero:0:6}/$zero"
export user IP files
expect <<<'END_EXPECT' # note the single quotes here!
set timeout -1
spawn scp $env(user)#$env(IP):$env(files) .
expect {assword:}
send "$env(my_password)\r"
expect eof
END_EXPECT
Before you run this, put your password into your shell's exported environment variables:
export my_password=abc123
bash script.sh joe zero bigfile1.tgz
bash script.sh joe zero bigfile2.tgz
...
Having said all that, public key authentication is much more secure. Use that, or get your sysadmins to enable it, if at all possible.

Passing variable to Expect and Spawn

I'm writing a script that will scp a tar file from my local server to a remote host. Since the script generates the file through a pre-requisite process, the name is generated dynamically. My script needs to take the name of the file and pass it to scp for transfer.
#!/usr/bin/expect -f
spawn scp test.$(date +%y%m%d_%H%M).tar user#IP-ADDRESS:/destination/folder
set pass "password"
expect "password: "
send -- "$pass\r"
expect eof
I've tried setting the filename as a variable but keep seeing the same error:
can't read "(date +%y%m%d_%H%M)": no such variable
while executing "spawn scp test.$(date +%y%m%d_%H%M).tar user#IP-ADDRESS:/destination/folder"
$(date +%y%m%d_%H%M) is not a Tcl command. If you use expect, you have to learn Tcl. To get a formatted date in Tcl, use the clock command. Also, interpolation of the result from a command in Tcl is not done by $(....), but by [....]. You can find examples for this construct here.
Decided to go another route since the team was able to provision a new Artifactory repo for this binary and alike. However, to the advice provided here I was able to make a few discoveries which I used to fix my issues:
I also had a password with $ symbol and that also caused a world of issues.
#!/bin/bash
TEST=$(date +%y%m%d_%H%M)
/usr/bin/expect <<eof
set password {pas\$word}
spawn scp "$TEST" user#IP-ADDRESS:/destination/folder
expect "*password:"
send "$pasword\r"
expect eof

how to script commands that will be executed on a device connected via ssh?

So, I've established a connection via ssh to a remote machine; and now what I would like to do is to execute few commands, grab some files and copy them back to my host machine.
I am aware that I can run
ssh user#host "command1; command2;....command_n"
and then close the connection, but how can I do the same without use the aforememtioned syntax? I have a lot of complex commands that has a bunch of quote and characters that would be a mess to escape.
Thanks!
My immediate thought is why not create a script and push it over to the remote machine to have it run locally in a text file? If you can't for whatever reason, I fiddled around with this and I think you could probably do well with a HEREDOC:
ssh -t jane#stackoverflow.com bash << 'EOF'
command 1 ...
command 2 ...
command 3 ...
EOF
and it seems to do the right thing. Play with your heredoc to keep your quotes safe, but it will get tricky. The only other thing I can offer (and I totally don't recomend this) is you could use a toy like perl to read and write to the ssh process like so:
open S, "| ssh -i ~/.ssh/host_dsa -t jane#stackoverflow.com bash";
print S "date\n"; # and so on
but this is a really crummy way to go about things. Note that you can do this in other languages.
Instead of the shell use some scripting language (Perl, Python, Ruby, etc.) and some module that takes care of the ugly work. For example:
#!/usr/bin/perl
use Net::OpenSSH;
my $ssh = Net::OpenSSH->new($host, user => $user);
$ssh->system('echo', 'Net::Open$$H', 'Quot%$', 'Th|s', '>For', 'You!');
$ssh->system({stdout_file => '/tmp/ls.out'}, 'ls');
$ssh->scp_put($local_path, $remote_path);
my $out = $ssh->capture("find /etc");
From here: Can I ssh somewhere, run some commands, and then leave myself a prompt?
The use of an expect script seems pretty straightforward... Copied from the above link for convenience, not mine, but I found it very useful.
#!/usr/bin/expect -f
spawn ssh $argv
send "export V=hello\n"
send "export W=world\n"
send "echo \$V \$W\n"
interact
I'm guessing a line like
send "scp -Cpvr someLocalFileOrDirectory you#10.10.10.10/home/you
would get you your files back...
and then:
send "exit"
would terminate the session - or you could end with interact and type in the exit yourself..

expect vs. bash read

I'm trying to use expect to talk to a bash script, but I'm missing something. My script is:
#!/bin/bash
echo -n "foo? "
read
echo $REPLY
and my expect script is:
#!/usr/bin/expect
spawn ./script.sh
expect "foo? "
send "bar\r\n"
But I never see bar when I run the expect script. What am I missing here?
Dumb me, I needed to add interact to my expect script so that it can finish "interacting" with the script:
#!/usr/bin/expect
spawn ./script.sh
expect "foo? "
send "bar\r\n"
interact
I found the answer here two minutes after asking this question.
I don't familiar well with expect syntax, but you worth try autoexpect:
autoexpect ./script.sh
It will run script.sh and after you'll finish running it an expect script script.exp will be created in current directory.
After than you can edit it if you need to tune something in it.

Resources