Jump to content

BASH Script Help Please :)

Hey Guys,

Wondering if you can help me here.

Have a little script that will run as a cron job on my NAS that uploads a configuration file to a remote SFTP.

server.

The code has no trouble getting the file and uploading it. All i need for it to work as planned is automatically insert the password so that i can run it unattended.

I've added comments for clarification

Issue is on line 13 & 14 i believe.

 

Background about systems:
Local NAS = Freenas(FreeBSD)

Remote NAS = Synology DiskStation

#!/bin/sh#Store password in varPASS=myPassword#Local Path of file to uploadcd /mnt/DATA/confback#The File to uploadupload_file=$(ls | grep `date +%Y%m%d`.db)echo "${upload_file} Will now be uploaded"#initiate SFTP sftp -P 801 sftpAddress:/remote/path <<EOF# Auto enter password# Having issue with auto entering the passwordexpect "password"send "$PASS"# Upload the fileput $upload_file 

Cheers for the help. 

while (true) {

    live();

}

Link to comment
Share on other sites

Link to post
Share on other sites

Hi, I believe you just need a colon after password in line 13

 

expect "password:"

Link to comment
Share on other sites

Link to post
Share on other sites

Hi, I believe you just need a colon after password in line 13

 

expect "password:"

 

Just gave that a go.

Still no result.

I also tried copying the entire line that would show up e.g "user @ domain 's password:" but did not succeed :( 

while (true) {

    live();

}

Link to comment
Share on other sites

Link to post
Share on other sites

Also, make sure you add a newline \n after your password (I think haha...), haven't dont this in a while...

Link to comment
Share on other sites

Link to post
Share on other sites

I put the \n new line after the $PASS

expect "password:"send "$PASS\n"put $upload_file

Any it still asks for a password:

[root@[member='Adam']-nas] /mnt/DATA/scripts# sh ./script.sh20141127.db Will now be uploadeduser @ domain's password: 

If I type the password manually it continues through the script as follows:

[root@[member='Adam']-nas] /mnt/DATA/scripts# sh ./script.sh20141127.db Will now be uploadeduser @ DOMAIN password:Connected to remote.server.Changing to: /Adam FreeNAS/configssftp> expect "password:"Invalid command.sftp> send "myPassword\n"Invalid command.sftp> put 20141127.dbUploading 20141127.db to /Adam FreeNAS/configs/20141127.db20141127.db                                   100%  287KB 143.5KB/s   00:02sftp>sftp>[root@[member='Adam']-nas] /mnt/DATA/scripts#

Hope that's enough information. 

Bash scripting is still new to me, getting all the syntax correct is super fun. 

 

EDIT:

It seems that after it connects it tries to use the expect and send commands. 

They possibly might not be the correct commands for freebsd ? 

while (true) {

    live();

}

Link to comment
Share on other sites

Link to post
Share on other sites

Mmmmmmmmmmm, I think the problem may be related (corrections so far aside) to the script header

I'm not experienced with the expect package, but looking around I see that you may need to use this:

#!/usr/local/bin/expect

 

Try to create a short script just to connect to it: I found this short example:

#!/usr/bin/expect

spawn sftp username@hostname.com
expect
"password:"
send "yourpasswordhere\n"
expect "sftp>"
send "cd logdirectory\n"
expect "sftp>"
send "put /var/log/file.log\n"
expect "sftp>"
send "exit\n"
interact

 

Source: http://stackoverflow.com/questions/5386482/how-to-run-the-sftp-command-with-a-password-from-bash-script

 

You could create a short script just to connect and try to invoke it from your bash script. First make sure you are able to connect with an Expect script, using that header.

 

I don't have a unix environment at home, so I can't try myself. I'll install a virtual machine and try later.

 

Hope it helps

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah from doing some more research I saw the expect env and gave that ago before...

Seems that FreeNAS(freeBSD) doesn't have that installed by default so I'll have to get that on there.

When ever you use those you just get:

./test.sh: spawn: not found./test.sh: expect: not found./test.sh: send: not found

The search continues :D 

while (true) {

    live();

}

Link to comment
Share on other sites

Link to post
Share on other sites

Oh, yes, make sure expect is installed on wherever you plan on running it xD. expect and send are not bash commands.

Link to comment
Share on other sites

Link to post
Share on other sites

Oh, yes, make sure expect is installed on wherever you plan on running it xD. expect and send are not bash commands.

Yeah, looking for a port now for freeBSD... Will report back when I fix it :P 

while (true) {

    live();

}

Link to comment
Share on other sites

Link to post
Share on other sites

I put the \n new line after the $PASS

expect "password:"send "$PASS\n"put $upload_file

Any it still asks for a password:

[root@[member='Adam']-nas] /mnt/DATA/scripts# sh ./script.sh20141127.db Will now be uploadeduser @ domain's password: 

If I type the password manually it continues through the script as follows:

[root@[member='Adam']-nas] /mnt/DATA/scripts# sh ./script.sh20141127.db Will now be uploadeduser @ DOMAIN password:Connected to remote.server.Changing to: /Adam FreeNAS/configssftp> expect "password:"Invalid command.sftp> send "myPassword\n"Invalid command.sftp> put 20141127.dbUploading 20141127.db to /Adam FreeNAS/configs/20141127.db20141127.db                                   100%  287KB 143.5KB/s   00:02sftp>sftp>[root@[member='Adam']-nas] /mnt/DATA/scripts#

I know this is completely off topic. But i received a ltt "notification" saying that you mentioned me in a post. huh

Link to comment
Share on other sites

Link to post
Share on other sites

I know this is completely off topic. But i received a ltt "notification" saying that you mentioned me in a post. huh

was probably when I put in the host name for my server: root @ adam - nas.

I'd say that's it 

while (true) {

    live();

}

Link to comment
Share on other sites

Link to post
Share on other sites

Shouldn't the connection string be in this format:

user:pass@hostname

EDIT:

 

To add a little bit more info, just do something like this:

#!/bin/sh#Store password in varUSER=myUserPASS=myPassword#Local Path of file to uploadcd /mnt/DATA/confback#The File to uploadupload_file=$(ls | grep `date +%Y%m%d`.db)echo "${upload_file} Will now be uploaded"#initiate SFTP sftp -P 801 $USER:$PASS@sftpAddress:/remote/path <<EOF# Upload the fileput $upload_file 

// TODO: Update signature to include PC buid.

Link to comment
Share on other sites

Link to post
Share on other sites

#initiate SFTP sftp -P 801 $USER:$PASS@sftpAddress:/remote/path <<EOF

 

Gave that a go, Still is asking me for the password . 

The variables are passing fine it's just automatically filling in the password that's the issue :( 

[root @ adam-nas] /mnt/DATA/scripts# ./script.sh Will now be uploadedmyUser:myPassword @ domain's password:

while (true) {

    live();

}

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×