Can not enter password in Shell Script - linux

I can not enter my pass word.
My shell script is here.
#!/bin/bash
expect -c "
set timeout 2
spawn ftp ${HOST}
expect \"Name\(${HOST}:root\):\"
send ${USER}\n
expect \"Password:\"
send ${PASS}\n
interact
"
I can enter user-name, but password is not work....
like this return.
spawn ftp ******
Connected to ****** (******).
220 (vsFTPd 2.2.2)
Name (******:root): ${USER}
331 Please specify the password.
Password:[root#........ bin]#
I think I send password... Could you please help me?
Thank you.
SOLVE MY QUESTION
I think it is easy for me to use this.
it is easy for me to use this.
#!/bin/sh
SERVER=$1
USER=$2
PASS=$3
FILE=$4
ftp -n <<END
open $SERVER
user $USER $PASS
cd /tmp
binary
prompt
mput $FILE
END

you have to prompt password and send it to variable
echo -n "Enter Password:"
read PASS

Related

Is there a way to automatically answer for user prompt when doing ssh in a shell script without using expect or spawn?

I'm trying to test ssh trust between a linux box against 12 other linux boxes using a shell script and I'm trying to pass user input as 'yes' for the question below automatically.
Are you sure you want to continue connecting (yes/no)?
but the script is failing with error 'Host key verification failed'. I manually executed the ssh command with << EOT on one of the server but the I still get user prompt question. Is there any-other way to pass input value for user prompts automatically while running ssh command?
Note: I cannot use spawn or except do you some system limitation and I cannot install them due to organisations access restrictions.
I tried with the following options but none of them worked for me
[command] << [EOT, EOL, EOF]
echo 'yes'
[EOT, EOL, EOF]
yes | ./script.sh
printf "yes" | ./script.sh
echo "yes" | ./script.sh
./script.sh 'read -p "Are you sure you want to continue connecting (yes/no)?";echo "yes"'
sh```
for server in `cat server_list` ; do
UPPER_MACHINE_NAME=`echo $server | cut -d '.' -f 1`
UPPER_MACHINE_NAME=${UPPER_MACHINE_NAME^^}
ssh -tt user#$UPPER_MACHINE_NAME << EOT
echo 'yes'
touch /usr/Finastra/sshtest.txt
EOT
done
```

expect, interact and then again expect

There are several posts regarding the same, but i still not able to make my expect script work properly. My intention is to automate everything but leave the password enter for the user. So there are 3 parts of the script:
automated login
give the user interaction to enter the password
give control back to Expect script to continue work
So i have script which will be spawned and which have 3 read commands. First and last should be filled by Expect and second one i would like to enter my self:
#!/bin/ksh
read user?User:
echo "Expect entered the username $user"
read pass?Password:
echo "User entered the password $pass"
read command?"Shell>"
echo "Expect entered the command $command"
My expect script:
#!/usr/bin/expect
spawn ./some_script
expect User
send I-am-expect\r
expect Password
interact
expect Shell
send I-am-expect-again
Unfortunately after i have entered the password the script does not continue and left in the interact mode:
[root#localhost ~]# ./my-expect
spawn ./some_script
User:I-am-expect
Expect entered the username I-am-expect
Password:i am user
User entered the password i am user
Shell>
And finally when i entering something on the "Shell" and pressing [ENTER] expect exits with the error:
Expect entered the command
expect: spawn id exp4 not open
while executing
"expect Shell"
(file "./my-expect" line 7)
[root#localhost ~]#
I appriciate any explanation or resolution of this issue. I am using expect version 5.45
You can read (expect_user) the user's password by yourself and then send it to the spawn'ed program. For example:
[STEP 101] # cat foo.exp
proc expect_prompt {} \
{
global spawn_id
expect -re {bash-[.0-9]+(#|\$)}
}
spawn ssh -t 127.0.0.1 bash --noprofile --norc
expect "password: "
stty -echo
expect_user -timeout 3600 -re "(.*)\[\r\n]"
stty echo
send "$expect_out(1,string)\r"
expect_prompt
send "exit\r"
expect eof
[STEP 102] # expect foo.exp
spawn ssh -t 127.0.0.1 bash --noprofile --norc
root#127.0.0.1's password:
bash-4.3# exit
exit
Connection to 127.0.0.1 closed.
[STEP 103] #
The interact should be given with proper condition for the exit criteria.
The following script will execute the user commands in the shell
exeCmds.sh
#!/bin/bash
read -p "User: " user
echo "Expect entered the username $user"
read -p "Password: " pass
echo "User entered the password $pass"
while :
do
# Simply executing the user inputs in the shell
read -p "Shell> " command
$command
done
automateCmdsExec.exp
#!/usr/bin/expect
spawn ./exeCmds.sh
expect User
send dinesh\r
expect Password
send welcome!2E\r
expect Shell>
puts "\nUser can interact now..."
puts -nonewline "Type 'proceed' for the script to take over\nShell> "
while 1 {
interact "proceed" {puts "User interaction completed.";break}
}
puts "Script take over the control now.."
# Now, sending 'whoami' command from script to shell
send "whoami\r"
expect Shell>
# Your further code here...
The script automateCmdsExec.exp will address the login needs of the bash script and when the prompt arrives, it will hand over the control to user.
We should define an exit criteria for the interact for which I have used the word proceed. (You can alter it as per your need).
Once interact matched the word proceed. it will return the control back to the expect script.
For demo purpose, I kept one more send-expect pair of command.
i.e.
send "whoami\r"
expect Shell>
You can keep your further code below the interact, thus it can be executed by script.

Bash Script Here Documents FTP

I recently discovered "here" statements while working with a bash script for automating an ftp process.
Reference for Here Documents: http://tldp.org/LDP/abs/html/here-docs.html
The ftp process takes fairly long in the bash script, and I wanted specifically to run it in the background and have the next line of the bash script continue after the ftp process. How would I do this for "here" documents?
FTP snippet:
USER="test"
PASSWD="test"
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
quit
END_SCRIPT
For example:
I want to be able to do this:
run ftp snippet &
run other shell commands
but I'm not quite sure where to put the &
I have tried so far:
Attempt 1: (I believe that this is syntactically incorrect and does not work):
function do_ftp() {
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
quit
END_SCRIPT
}
do_ftp &
//additional commands
Attempt 2:
USER="test"
PASSWD="test"
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
quit
END_SCRIPT &
Attempt 3 :
USER="test"
PASSWD="test"
ftp -n $HOST & <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
quit
END_SCRIPT
I haven't tested it on ftp BUT:
I know when you want to put variables on HEREDOC you should do something else
For example I tried the following for ssh command:
while read pass port user ip fileinput fileoutput filetemp; do
sshpass -p$pass ssh -o 'StrictHostKeyChecking no' -p $port $user#$ip fileinput=$fileinput fileoutput=$fileoutput filetemp=$filetemp 'bash -s'<<ENDSSH1
python /path/to/f.py $fileinput $fileoutput $filetemp
ENDSSH1
done <<____HERE1
PASS PORT USER IP FILE-INPUT FILE-OUTPUT FILE-TEMP
____HERE1
May be something like this is needed by you...

How to pass a value from bash into expect?

I've got a really simple bash script which requires expect.
I need to pass a value from bash into expect and I'm not trying to ssh into another server or anything (cause I only seem to find questions regarding logging into another server via ssh).
The idea is simply something like this:
#!/usr/bin/env bash
echo "Please enter your password: "
read PASSWD
x=$(expect -c '
spawn su -c 'whoami'
expect "Password:"
send "$PASSWD\r"
interact
')
So this doesn't work. The expect shell doesn't recognize the $PASSWD variable.
How may this be accomplished?
Thank you.
Another option would be to store the PASSWD in the environment and let expect pick it up there:
read -p "Your password: " passwd
export passwd
expect -c '... ; send "$env(passwd)\r"; ...'
Probably the best choice security-wise is have expect prompt for the password: then, the password will not show up on the command line nor in the environment.
expect -c <<'END'
stty -echo
send_user "Your password: "
expect_user -re "(.*)\n"
send_user "\n"
set passwd $expect_out(1,string)
stty echo
# your script starts here
...
send "$passwd\r"
...
END
Variables within single quotes are not expanded by the shell, that's why in this case $PASSWD remains the literal string $PASSWD.
Try changing the quotes:
#!/usr/bin/env bash
echo "Please enter your password: "
read PASSWD
x=$(expect -c "
spawn su -c 'whoami'
expect 'Password:'
send '$PASSWD\r'
interact
")
One more note: you should be aware that this could pose a security risk, as the password will be visible in plaintext in the process list while the command is running.

SFTP Connection with out password prompt

I am trying to move txt/ log file from server1 to server2.
I am trying to connect to a server from server1 to server2 using SFTP but some how it is asking for password in the prompt.Can anyone let me know how to give password as input through script and execute this functionality using a Script.
Please let me know asap......
MY CODE:
test.sh is script and 1.txt file has password details.....
code: test.sh
sftp mwctrl#sacsun11 < 1.txt <> out.log 2>&1
cd /usr/ftadapters/logs/adapters/rivaadp
lcd /export/home/eisape
put *.txt
exit
EOF
1.txt:
password m33tzn3
Actually you need add ssh keys to remote machine. Check article below:
Using sftp without password (http://says-story.blogspot.nl/2008/01/using-ssh-scp-sftp-without-password.html)
Setting up ssh keys is relatively simple. Takes <1 min following the instructions in the link posted above by fxzuz.
But generally passing passwords as parameters / storing in config files is considered a security risk.
However, if you still want to go ahead, here is a link -- http://nixcraft.com/shell-scripting/4489-ssh-passing-unix-login-passwords-through-shell-scripts.html
try using this
/usr/bin/expect <<EOF
spawn sftp -oStrictHostKeyChecking=no -oCheckHostIP=no mwctrl#sacsun11 \
"cd /usr/ftadapters/logs/adapters/rivaadp \
lcd /export/home/eisape \
put *.txt \"
expect "*?assword:*"
send "m33tzn3"
send "\r"
set timeout -1
send "\r"
expect EOF

Resources