Pass password for download script - bash

I'm downloading huge files and have a python downloading script. Since the data are to huge to be stored in their original format, I only download a couple of years, process them, then delete the original before I download the next few years. For every set of years I have a separate download script and I just build a quick bash script to run them
#!/bin/bash
python download_1901-1909.py
## processing, delete
python download_1910-1919.py
## processing, delete
python download_1920-1929.py
## processing, delete
[...]
For the download scipt, I have to type in a password. I know it's is possible to do some automatic password passing with spawn, but I couldn't make it work. Trying the spawn thing does this
#!/usr/bin/expect -f
spawn python download_1901-1909.py
expect "Password:"
send "mypassword"
interact
yields
sh test.sh
test.sh: line 2: spawn: command not found
couldn't read file "Password:": no such file or directory
test.sh: line 4: send: command not found
test.sh: line 5: interact: command not found
How can I pass a password to a python download script?

This answer is trying to solve your question of finishing what you need, not the question of executing a #!/usr/bin/expect, which may be what you are asking.
You may try to use the -c param of expect inline.
For example,
#!/bin/bash
expect -c "spawn python download-1901_1909.py; expect \"assword\"; send \"mypassword\r\n\"; interact"
expect -c "spawn python download-1910_1919.py; expect \"assword\"; send \"mypassword\r\n\"; interact"
# and the remaining, similarly
Note:
Your send part may need to send the \r\n to "simulate" the Enter key of finishing enter the password.
You may use sed within a for to simply the lines.
"assword" is to prevent the case of first letter of that word, "Password" vs "password", for some portability.

Related

Write ActiveState Tcl Program for Windows that expects a password and enters it

I want to write a shell program in windows that runs another shell script and expects a password prompt from the Git bash terminal and inputs it.
This is what I have so far:
#!/bin/sh
# \
exec tclsh "$0" ${1+"$#"}
package require Expect
spawn sampleScript.sh
expect "Password:"
send "pass123"
sampleScript.sh code:
echo 'Hello, world.' >foo.txt
my program outputs the following:
'The operation completed successfully. while executing "spawn sampleScript.sh"
(file "compare.tcl" line 6)'
However, there is no foo.txt that is created in my local file folder where the scripts are. Can you help?
The key with expect programs is to let the spawned program exit gracefully. As it currently stands, after your expect script sends the password, it immediately exits, and that kills the spawned program too early.
If you don't need to interact with the sampleScript (i.e. just let it run to completion), the last line in the expect script should be
expect eof
Otherwise, use
interact
Read How to create a Minimal, Reproducible Example -- your updated code does not reproduce the error you're seeing
Tcl code:
when you send something, you usually need to "hit Enter": send "password\r"
Did you add expect eof to the Tcl script? If not, you might be killing sampleScript.sh before it has a chance to create the output file
sampleScript.sh: Is that really your sample script? Where's the password prompt?

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

automate getting diagnostic files from a controller via ssh commands

I'd like to automate getting diagnostic files from a controller that responds to ssh commands, like e.g.
ssh diag#controller tarred > diags.tgz
Unfortunately, I have to type a password to make the above command go through.
What have I considered to get around that:
using ssh keys: not possible, since I can't login to the controller, it just expects commands and doesn't offer a shell
using ssh-pass package: I don't have admin rights on the machine and can't install packages
using "expect": works to some extent, but the resulting file is corrupted.
Here's the "expect" script I've used:
#!/usr/bin/expect -f
log_user 0
set timeout 300
spawn ssh diag#controller tarred
expect "?assword:"
send "unrealpassword\r"
expect \r\n
log_user 1
expect eof
The script makes sure that only the required output gets stored with the "log_user" commands until eof is encountered.
I've piped this script to a file and that file is corrupted, i.e. it's either too short (because of a timeout?) or too long (?).
Any idea about what goes wrong here.?

How to send commands stored in a file when using Expect

I'm writing a script that will eventually execute a list of commands on a switch (via SSH). These commands are stored in a file and the number of commands will vary
However, I'm not sure how this can be done using Expect. I know Expect can use a while loop, but I can't find a clear example. can someone here help?
/usr/bin/expect <<EOD
spawn ssh -o StrictHostKeyChecking=no admin#$switch
expect "*Enter password for admin\:"
send "password\r"
expect "*#"
send "????"
there should be a while loop that reads line by line from a file called "commands" that looks like this
command 1
command 2
command 3
...
Extreme Networks XOS has an XML API. You can use this for executing arbitrary commands. See the ExtremeXOS XML API Developer Guide which is listed on the support documentation page.
Managing switches by expect-scripting their CLIs is often erratic and error prone, I'd recommend that you avoid doing so if possible.

Spawn id exp7 not open while executing error shell script

I am using cron to run a script that connects to another server via sftp and uploads files. Once in a while, I will receive the error: Spawn-id-exp7-not-open while executing send \"ls\r\" and the script will stop executing (files will not be sent). However, most of the time, the script will run with no errors.
Below is my script I use to connect :
#!/bin/bash
HOST="removed.com:\API"
USER="user"
PASS="removed"
VAR=$(expect -c "
spawn sftp -o \"BatchMode no\" -b /var/www/prep/cmd -P 13266 $USER#$HOST
expect \"Password:\"
send \"$PASS\r\"
expect \"\\\\$\"
send \"ls\r\"
expect -re \"$USER.*\"
send \"exit\"
")
echo "==============="
echo "$VAR"
Below is the script that runs after I connect (cmd) :
put "/var/www/prep/1.xml"
put "/var/www/prep/2.xml"
I took a look at a similar question found at send: spawn id exp7 not open , but it has not helped me. Perhaps the connection ends before the cmd script can run?
Thanks for your help!
You're using sftp with -b. Therefore, once you provide the password, there will be no interaction: sftp will read commands from the batch file and exit.
From the man page:
-b batchfile
Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication. A batchfile of ‘-’ may be used to indicate standard input. sftp will abort if any of the following commands fail: get, put, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir. Termination on error can be suppressed on a command by command basis by prefixing the command with a ‘-’ character (for example, -rm /tmp/blah*).

Resources