Using expect, spawn with scp - shell

I'm currently trying to automate a file transfer using the scp command with a shell script and the expect package. Based on what I've seen it seems that I should have
#!/usr/bin/expect -f
But when I did that I still get the errors:
DirectoryChange.sh: line 33: spawn: command not found
couldn't read file "*Password:*": no such file or directory
DirectoryChange.sh: line 35: send: command not found
DirectoryChange.sh: line 36: interact: command not found
The code I have works something along these lines:
#!/usr/bin/expect -f
repository=$PWD"/subdirectory/"
set pass "***********"
cd $repository
spawn scp -r user#host:/copyDirectory/ .
expect "*Password:*"
send "${pass}\r";
interact

It's a bad practice to store passwords in scripts or any other file. Use SSH authentication keys instead.
Take a look at this tutorial.

Looks like you're invoking your expect script like sh DirectoryChange.sh. Clearly sh is not the correct interpreter for an expect script.
change the file extension: ".sh" is for shell scripts
make sure it has execute permissions then launch it with ./DirectoryChange.exp
repository=$PWD"/subdirectory/" is not how to assign variables in expect. remove this line and edit the cd line to cd subdirectory
you don't have to interact with scp, so change the last line to expect eof

Related

Run .sh file terminal [duplicate]

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Difference between sh and Bash
(11 answers)
Closed last month.
I ran below command line from a terminal and it works:
sftp -i ~/.ssh/id_rsa username#host
put csv_file
put manifest_file
I want to automatically run this so I created a .sh file that looks like below:
#!/bin/bash
sftp -i ~/.ssh/id_rsa username#host
put csv_file
put manifest_file
and save it as run.sh. I ran it like below:
sh run.sh
It connect to the host but the next commands, the put lines, did not run.
Can I get insights why is that and how can I solve it?
It worked interactively because put commands were not consumed by shell but running sftp process. Script you have attempts to run put commands as standalone.
Redirection
To run put you can either feed them to standard input of the sftp:
#!/bin/bash
sftp -i ~/.ssh/id_rsa username#host <<END
put csv_file
put manifest_file
END
where <<END denotes take next lines until seeing END and redirect them (<<) to the standard input of sftp.
Batch File
Or alternatively if you can automate them via batch file called e.g. batch and containing simply the put lines:
put csv_file
put manifest_file
and then tell sftp it should process commands in batch file like this instead of having shell script:
sftp -i ~/.ssh/id_rsa username#host -b batch
NOTE: Since you have bash shebang in file you can make that file executable by chmod +x run.sh and then simply run it as ./run.sh.

Old version of script is run unless invoked with "sh scriptname"

I'm making a small edit to a shell script I use to mask password inputs like so:
#!/bin/bash
printf "Enter login and press [ENTER]\n"
read user
printf "Enter password and press [ENTER]\n"
read -s -p pass
With the read -s -p pass being the updated part. For some reason I'm not seeing the changes when I run it normally by entering script.sh into the command line but I do see the changes when I run sh script.sh. I've tried opening new terminal windows, and have run it in both ITerm and the default Mac terminal. I'm far from a scripting master, does anyone know why I'm not seeing the changes without the prefix?
Use a full or relative path to the script to make sure you're running what you think you're running.
If you are running it as simply script.sh then the shell will PATH environment variable lookup to locate it. To see which script.sh bash would be using in that case, run type script.sh.
Relative Path
./script.sh
Full Path
/path/to/my/script.sh

Why this command "google-auth" works in the terminal but not from bash script?

I have installed libpam-google-authenticator and freeradius on server ubuntu 16.0405. Everything works good, except for if I use the command google-auth in bash script I get a error message "google-auth: command not found"
But the same works if I put it on terminal directly.
#!/bin/bash
google-auth
That is not a bash script.
To make it a bash script, your first line needs to include a "#" as follows:
#!/bin/bash
google-auth
Also, you need to ensure that the script is executable:
chmod +x yourscript.sh
Hopefully that will solve your problem.
As per the comments below, it seems like the command "google-auth" was an alias which wasn't being established in the child shell.

Shc encrypted shell script not executable

I created an encrypted shell script with the tool shc.
The script works just fine on my computer but when I transfer it to
another one (solaris 10 to solaris 10) I get the following error:
invalid argument
It's not a permission problem and the encrypted script should be ok I guess it's a header/compiler problem.
The shc command used wasshc -rf <filename> so the script should work on another computer!?
According to The Geek Stuff you need to use the -r option to relax security and -f to specify your script file:
shc -r -f script.sh

bash command doesnt seem to work, but its echo does?

Well, I'm new to linux so this may be a very newbie kinda of thing, here it goes:
I have a script in which I'm trying to send some different jobs to remote computers (in fact Amazon's EC2 instances), these jobs are in fact the same function which I run with different parameters.
eventually in the script code I have this line:
nohup ssh -fqi key.pem ubuntu#${Instance_Id[idx]} $tmp
if I do:
echo nohup ssh -fqi key.pem ubuntu#${Instance_Id[idx]} $tmp
I get:
nohup ssh -fqi key.pem ubuntu#ec2-72-44-41-228.compute-1.amazonaws.com '(nohup ./Script.sh 11 1&)'
Now the weird thing. If I run the code with no echo in the script it doesnt work! it says in the nohup.out (in my laptop, no nohup.out is created in the remote instance) bash: (nohup ./Script.sh 10 1&): No such file or directory
The file does exist locally and remotely and is chmod +x.
If I simply run the very same script with an echo in front of the problematic line and copy its output and paste in the terminal, it works!.
Any clues welcome, thanks!
Try removing the single quotes from $tmp. It looks like bash is treating (nohup ./Script.sh 10 1&) as the command with no parameters, but technically nohup is the command with the parameters ./Script.sh 10 1.
The problem is the single quotes around the nohup command in your $tmp variable. These don't get used on the shell locally, so SSH passes them verbatim. This means remotely the ssh server tries to interpret (nohup ./Script.sh 10 1&) as a command (looks for a file named that) which there clearly isn't. Make sure you remove the single quotes in $tmp.

Resources