Pass variables from one Bash script to another after SSH - bash

Okay, so I am new to bash scripting, about 2 whole hours and I'm banging my head against a wall. I need to carry some variables from one script to another and can't seem to get it to work.
This works fine
script1.sh
echo "Enter your name"
read name
export name
./script2.sh
sript2.sh
echo $name
This does not
script1.sh
echo "Enter your name"
read name
export name
ssh $user#$domain "bash -s" < ./script2.sh
sript2.sh
echo $name
The code is dumb, I get that but basically what I want to do is pass database variables to script 2 to use on the server I've SSH'd into. I would do this all in one script but it seems the only way that I can get the server commands to run is to call a second script after the login, otherwise it runs the server commands locally and fails. I'm doing this from a Mac if that makes any difference at all.
Help?
** Update with more legitimate code ****
login.sh
#!/bin/bash
echo "Enter server username:"
read user
echo "Enter server domain"
read domain
echo "Enter your password"
read password
echo "Enter database name"
read database
export database
export user
export domain
export password
ssh $user#$domain "bash -s" < ./create_files.sh
create_files.sh
#!/bin/bash
cd public_html
tar -zcvf test.tar.gz test.html
echo "db:"$database
mysqldump -u $user -p"$password" $database > $database.sql
The goal here is to use the details from login.sh in create_files.sh after this is done I also need to use the login details to scp the created files back to my local machine. Hope this clarifies the problem.

When invoking ssh, it creates a new shell process which is not inheriting the new variables that you exported in the first script. Instead, try adding them as commandline parameters to the second script, like so:
#!/bin/bash
echo "Enter server username:"
read user
echo "Enter server domain"
read domain
echo "Enter your password"
read password
echo "Enter database name"
read database
ssh $user#$domain "bash -s" < ./create_files.sh "$user" "$password" "$database"
Then modify your second script to look like this:
#!/bin/bash
cd public_html
tar -zcvf test.tar.gz test.html
echo "db:$3"
mysqldump -u "$1" -p"$2" "$3" > "$3".sql

Related

How to use encrypted password with sftp login?

I want to write a shell script where user interaction is not allowed and I want to use sftp login with in the script. Now I have few challenges to execute this approach.
There is no user interaction so I can only provide the options as a argument while executing the script. Script can be like this.
#!/bin/bash
if [[ "${#}" -lt 4 ]]; then
echo -e "Usage: ${0} <sftpUser> <sftpPassword> <sftpHost> <sftpPort>"
exit 1
fi
sftpUser=${1}
sftpPassword=${2}
sftpHost=${3}
sftpPort=${4}
remote_dir="/home/vagrant"
source_dir="/home/vagrant/sftpDir"
sftpFile="/tmp/tempfile"
echo "cd ${remote_dir}" >> ${sftpFile}
echo "mget * ${source_dir}" >> ${sftpFile}
echo "quit" >> ${sftpFile}
expect -c "
spawn sftp -P ${sftpPort} -o "BatchMode=no" -b "${sftpFile}" ${sftpUser}#${sftpHost}
expect -nocase \"*Password:\" { send \"${sftpPassword}\r\"; interact }
"
rm -rf ${sftpFile}
$ ./shellscript.sh user1 password#123 192.168.0.1 22
Here, we need to provide the argument with the script itself and here we are using the plain text format for password password#123
How can we use the encrypted password in the argument as this can be a risk to expose the password?
Is there any other approach to execute this scenario?
I am not able to find any approach to pass the encrypted password with SFTP login.

sshpass not working in shell script while giving username and password in varibales

I am using this below script and i am getting this error -p: command not found
Code:
echo "Enter the server name"
read -s server
echo "Enter the Remote Username"
read -s Rem_user
echo "Enter the Remote Password"
read -s rmtpasswrd
output=sshpass -p ${rmtpasswrd} ssh ${Rem_user}#${server} -T 'df -h;'
echo $output
Please let me know what is the error in the script.
You need to use a command substitution to run ssh and capture its output.
output=$(ssh "${Rem_user}#${server}" 'df -h')
There is no reason to use sshpass if you are going to prompt the user for a password. ssh already does that (and more securely than you can).

One password prompt for bash script including SCP and SSH

Printing documents from printer connected to internet is really slow at my university. Therefore I'm writing a script that sends a file to a remote computer with SCP, sends a series of commands over SSH to print the document from the remote computer (which has better connection with the printer) and then delete the file on the remote computer.
It works like a charm but the annoying part is that it prompts for the password two times, one time when it sends the file with SCP and one time when it sends commands over SSH. How can this be solved? I read that you can use a identity file? The thing is though that multiple users will use it and many has very limited experience with bash programming so the script must do everything including creating the file.
Users will mostly use Mac and the remote computer uses Red Hat. Here's the code so far:
#!/bin/sh
FILENAME="$1"
PRINTER="$2"
# checks if second argument is set, else prompt for it
if [ -z ${PRINTER:+x} ]; then
printf "Printer: ";
read PRINTER;
fi
# prompt for username
printf "CID: "
read CID
scp $FILENAME $CID#adress:$FILENAME
ssh -t $CID#adress bash -c "'
lpr -P $PRINTER $FILENAME
rm $FILENAME
exit
'"
You don't need to copy the file at all; you can simply send it to lpr via standard input.
ssh -t $CID#adress lpr -P "$PRINTER" < "$FILENAME"
(ssh reads from $FILENAME and forwards it to the remote command.)
start an ssh-agent and add your key to it:
eval $(ssh-agent -s)
ssh-add # here you will be prompted
scp "$FILENAME" "$CID#adress:$FILENAME"
ssh -t "$CID#adress" bash -c <<END
lpr -P "$PRINTER" "$FILENAME"
rm "$FILENAME"
END
ssh-agent -k # kill the agent

Passing Value to a shell script via ssh

I want to pass a value to the shell script via ssh:
I have two Linux machines: machine1 and machine2
I have two scripts on those machines: "script1" on machine1 and "script2" machine2.
I have done ssh setting so I can login to machine2 from machine1 without password
/opt/script1
#!/bin/sh
echo "enter your name"
read name
ssh root#machine2 "/opt/script2 $name"
/opt/script2
#!/bin/sh
echo "$name"
but no string is printing
Please let me know the procedure to do this.
can u try the scripts like this
script1.sh
echo "enter your name"
read name
ssh root#machine2 "/opt/script2.sh $name"
script2.sh
echo $1
Passing command-line parameter to shell script
$0 is the name of the command
$1 first parameter
$2 second parameter
$3 third parameter etc. etc
$# total number of parameters
$# all the parameters will be listed
If i entered my name as Rupert script1 would be doing this
ssh root#machine2 "/opt/script2 Rupert"
Script2 is looking for $name but this has not been set on machine2 so the script will print nothing.
You can edit script2 to the below
#!/bin/sh
name=$1
echo "$name"
You should change the script 1 as following:
#!/bin/sh
echo "enter your name"
read name
ssh root#machine2 `/opt/script2 $name`

Bash script not waiting on read

I'm trying to run commands as another user and read input as that user. For some reason the script doesn't pause on the read command, I'm unsure as to why. Here's an example of the script:
#!/bin/bash
username='anthony'
sudo -H -u "$username" bash << 'END_COMMAND'
# Commands to be run as new user
# -------------------------------
echo "#! Running as new user $USER..."
echo "#! Gathering setup information for $USER..."
echo -n "Enter your name and press [ENTER]: "
read name
echo -n "Enter your email and press [ENTER]: "
read email
END_COMMAND
echo "Done"
Any ideas as to why this isn't stopping on read name or read email?
read is reading from stdin, and bash is inheriting its stdin from sudo, which is coming from the heredoc. If you want it to come from somewhere else, you need to be explicit. For example:
bash 3<&0 << 'END_COMMAND'
...
read <&3
...
This does not work with sudo, however, since sudo closes the non-standard file descriptors. But sudo does not close stderr, so if you can get away with reusing that file descriptor, you might be able to do:
sudo -H -u "$username" bash 2<&0 << 'END_COMMAND'
...
read -u 2 email
...
But it's probably much safer to do:
sudo -H -u "$username" bash << 'END_COMMAND'
...
read email < /dev/tty
...
Can you have this script check if it's running as sudo, and if it's not, exec itself with sudo?
#!/bin/bash
username='anthony'
if [[ -z "${SUDO_USER}" ]]; then
exec sudo -H -u "${username}" -- $0
fi
echo "
# Commands to be run as new user
# -------------------------------
"
echo "#! Running as new user $USER..."
echo "#! Gathering setup information for $USER..."
echo -n "Enter your name and press [ENTER]: "
read name
echo -n "Enter your email and press [ENTER]: "
read email

Resources