1827807 Members
2985 Online
109969 Solutions
New Discussion

Re: sftp script

 
SOLVED
Go to solution
Davis Paul
Valued Contributor

sftp script

Hi All
I have a script as follows:

cat script1
#!/usr/local/bin/expect
spawn sftp -v root@192.168.55.242
expect "password:"
send "root123\n";
interact

This will work up to sftp prompt. I need to add three sftp command
1) cd /var
2)put star.txt
3) bye

Where I can add these three commands in above scripts?

Thanks and regards
Davis Paul

16 REPLIES 16
Mel Burslan
Honored Contributor

Re: sftp script

after send "root123\n" line you will need to expect a string telling you that you logged in successfully as root. I am not sure what this string will be. you need to login manually to get the string from the session log. then you can enter your commands after the expect line as follows:

expect "my successful login string here"
cd /var
put star.txt
bye


HTH
________________________________
UNIX because I majored in cryptology...
James R. Ferguson
Acclaimed Contributor

Re: sftp script

Hi:

I would setup public SSH keys and do something like I do in this thread:

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1308338

Regards!

...JRF...
Court Campbell
Honored Contributor

Re: sftp script

I am not sure what your goal is. But if you are only going to copy one file across the network, you could just use scp.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Ganesan R
Honored Contributor

Re: sftp script

Hi Davis Paul,

keeping the root password in script or any text file is not a secure way.

You can simply configure ssh password less login between source and destination server and write a simple script to transfer the files like below.

#--------------------------------------------
# Connect to server and put the files
#--------------------------------------------
sftp root@193.23.116.107 <<**
lcd
cd /var
put star.txt
**
Best wishes,

Ganesh.
Davis Paul
Valued Contributor

Re: sftp script

Mel Burslan ,
How to get the string?

Court Campbell,
My goal is to copy one file across the network. How can I change this one in to scp? That should not ask username and password( means automatic file transfer)

Regards
Davis Paul

Michael Mike Reaser
Valued Contributor

Re: sftp script

Court> But if you are only going to copy one file across the network, you could just use scp.

Not necessarily. I have to exchange one file a day with an external vendor using their sFTP server, and they will *NOT* allow me to just scp the sucker. I had to dance the Key Exchange Dance and am forced to use sftp and ONLY sftp for the transfers. Resulting in a long-ish back-and-forth script. For ONE file. :-(

(Can you tell that having to jump thru eleventy-bazillion hoops to transfer a single file that could be sent in one easy step has me Somewhat Annoyed?)
There's no place like 127.0.0.1

HP-Server-Literate since 1979
Davis Paul
Valued Contributor

Re: sftp script

Hi
I cannot create a password less login . Because the destination server control is not in our hand.


Regards
Davis
Ganesan R
Honored Contributor

Re: sftp script

Hi Again,

Follow these steps to configure ssh password less login.

1.Login to serverA as user1.

2.$ssh-keygen -t dsa
Press enter for all the questions.It will create the private/public keys
under $HOME/.ssh/
File names are id_rsa and id_rsa.pub

3.Now you need to copy the id_rsa.pub file content into ServerB $HOME/.ssh/authorized_keys file
ServerA#scp ~user1/.ssh/id_rsa.pub ServerB:/tmp

Login to ServerB server

ServerB#cat /tmp/id_rsa.pub >> ~user1/.ssh/authorized_keys

Make sure the following permissions on serverB.

Home directory should have 755 permission (users home directory)
$HOME/.ssh directory should have 700 permission
$HOME/.ssh/authorized_keys file should have 600 permission
Best wishes,

Ganesh.
Mel Burslan
Honored Contributor

Re: sftp script

The answer to your "how to get the string" depends on your server configuration.

Using an interactove session, not the script, login to the destination server.

watch the screen after you enter the password, and note the last line printed on the screen. That should be your expect string. My servers do not say anything upon successful login, so I can skip that expect statement if I were to use your script, but you might (or might not) need it.
________________________________
UNIX because I majored in cryptology...
Ganesan R
Honored Contributor

Re: sftp script

Hi Davis Paul,

If you cannot have ssh password less login, and afford to use ftp instead of sftp follow the below steps.

1.Put a entry in root .netrc file like this.

#vi /.netrc
machine 192.168.55.242 login root passwd root123

2.Your script should be like this

#--------------------------------------------
# Connect to 192.168.55.242 and put the file
#--------------------------------------------
ftp <<**
open 192.168.55.242
lcd /path
cd /var
bin
put star.txt
bye
**
#--------------------------------------------

This will work. But note that this is plain transfer not like sftp which is more secure.
Best wishes,

Ganesh.
Davis Paul
Valued Contributor

Re: sftp script


Mel Burslan,

nothing is printed as below:

# sftp root@192.168.55.242
Connecting to 192.168.55.242...
Password:
sftp>
sftp> bye
#


Then , how to edit the script???





Mel Burslan
Honored Contributor

Re: sftp script

then modify your script as follows:

#!/usr/local/bin/expect
spawn sftp -v root@192.168.55.242
expect "password:"
send "root123\n";
interact
cd /var
put star.txt
bye

Note that you are trusting the system that it logged you in successfully. If your login has failed, unpredictable implications may occur. Hence, use it at your own risk.
________________________________
UNIX because I majored in cryptology...
Court Campbell
Honored Contributor

Re: sftp script

Mike Reaser,

Thanks for the 2 cent comment, but there is a difference in a suggestion and a definitive answer. If you read carefully you would notice that I stated I did not know what DP's goal is.

And to answer your quesiton DP. Just setup a priv/pub key that has no passphrase. Then put the pub key in the aurthorized key file on the remove server. Then you could just

scp /your/file remote_server:/remote/directory

But as Mike has pointed out that mayu not work for your situation. since this is a simple one liner you could easily add it to a crontab.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
OldSchool
Honored Contributor
Solution

Re: sftp script

DP said:

"nothing is printed as below:

# sftp root@192.168.55.242
Connecting to 192.168.55.242...
Password:
sftp> <---
sftp> bye"

No...you got "sftp>" (see "<---" above)

something like the following *might* work:

#!/usr/local/bin/expect
spawn sftp -v root@192.168.55.242
expect "password:"
send "root123\n";
expect "sftp>"
send "cd /var"
expect "sftp>"
send "put star.txt"
expect "sftp>"
send "bye"

If you've an account on the remote server, consider the public key methods described above. Or possibly contacting them to see if they can arrange to install your key for you.
Davis Paul
Valued Contributor

Re: sftp script

OldSchool,
Its working.

Regards
Davis Paul
Davis Paul
Valued Contributor

Re: sftp script

Thanks to all ....