SSH to multiple server and then sftp and copy the file to a server? [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0
















This question already has an answer here:




  • How to run the sftp command with a password from Bash script?

    9 answers




I want to ssh to multiple server using bash script and automate this script using crontab. I use "expect" to ssh to multiple server because of authentication need. But, I don't know how to copy the file in destination server using SFTP to my server. Can someone give me some clue for this problem.



Here is my code to SSH to multiple server (in this case I make tunneling to server destination):



/home/users/script/expect.sh 45108 username password "command" 
/home/users/script/expect.sh 45109 username password "command"
#45108 is port for tunneling, username and password is using like in shell terminal (ssh username@ipadd -p $server)


and this is the expect script that I use:



#!/usr/bin/expect
set timeout 10
set node [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]


spawn ssh $username@localhost -p $node
expect {
"(yes/no)?"
{
send "yesn"
expect "*assword:" { send "$passwordn"}
}
"*assword:" { send "$passwordn" }
}

expect {
"*#" { send "$commandn" }
}

expect {
"*#" { send "exitn" }
}

expect eof


Thank you










share|improve this question













marked as duplicate by Martin Prikryl sftp
Users with the  sftp badge can single-handedly close sftp questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 8 at 6:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    What about How to run the sftp command with a password from Bash script?

    – Martin Prikryl
    Jan 4 at 8:07











  • Do you want to scp instead of ssh, or should both happen? In what sequence? Repurposing your expect script to use scp instead of ssh should be completely trivial, though the prompts etc will differ.

    – tripleee
    Jan 4 at 8:07











  • if we use "sshpass", is it already ssh to remote server ?... and as you know, I need to upload the file to my server and with one script I need to ssh to multiple remote server. is it possible? @MartinPrikryl

    – Kresna Lita
    Jan 4 at 8:27











  • I do not understand what you mean by "if we use "sshpass", is it already ssh to remote server".

    – Martin Prikryl
    Jan 4 at 8:28













  • sorry I'm newbie at bash script. I will read about ssh and scp. Thanks for your clue @tripleee

    – Kresna Lita
    Jan 4 at 8:34


















0
















This question already has an answer here:




  • How to run the sftp command with a password from Bash script?

    9 answers




I want to ssh to multiple server using bash script and automate this script using crontab. I use "expect" to ssh to multiple server because of authentication need. But, I don't know how to copy the file in destination server using SFTP to my server. Can someone give me some clue for this problem.



Here is my code to SSH to multiple server (in this case I make tunneling to server destination):



/home/users/script/expect.sh 45108 username password "command" 
/home/users/script/expect.sh 45109 username password "command"
#45108 is port for tunneling, username and password is using like in shell terminal (ssh username@ipadd -p $server)


and this is the expect script that I use:



#!/usr/bin/expect
set timeout 10
set node [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]


spawn ssh $username@localhost -p $node
expect {
"(yes/no)?"
{
send "yesn"
expect "*assword:" { send "$passwordn"}
}
"*assword:" { send "$passwordn" }
}

expect {
"*#" { send "$commandn" }
}

expect {
"*#" { send "exitn" }
}

expect eof


Thank you










share|improve this question













marked as duplicate by Martin Prikryl sftp
Users with the  sftp badge can single-handedly close sftp questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 8 at 6:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    What about How to run the sftp command with a password from Bash script?

    – Martin Prikryl
    Jan 4 at 8:07











  • Do you want to scp instead of ssh, or should both happen? In what sequence? Repurposing your expect script to use scp instead of ssh should be completely trivial, though the prompts etc will differ.

    – tripleee
    Jan 4 at 8:07











  • if we use "sshpass", is it already ssh to remote server ?... and as you know, I need to upload the file to my server and with one script I need to ssh to multiple remote server. is it possible? @MartinPrikryl

    – Kresna Lita
    Jan 4 at 8:27











  • I do not understand what you mean by "if we use "sshpass", is it already ssh to remote server".

    – Martin Prikryl
    Jan 4 at 8:28













  • sorry I'm newbie at bash script. I will read about ssh and scp. Thanks for your clue @tripleee

    – Kresna Lita
    Jan 4 at 8:34














0












0








0


1







This question already has an answer here:




  • How to run the sftp command with a password from Bash script?

    9 answers




I want to ssh to multiple server using bash script and automate this script using crontab. I use "expect" to ssh to multiple server because of authentication need. But, I don't know how to copy the file in destination server using SFTP to my server. Can someone give me some clue for this problem.



Here is my code to SSH to multiple server (in this case I make tunneling to server destination):



/home/users/script/expect.sh 45108 username password "command" 
/home/users/script/expect.sh 45109 username password "command"
#45108 is port for tunneling, username and password is using like in shell terminal (ssh username@ipadd -p $server)


and this is the expect script that I use:



#!/usr/bin/expect
set timeout 10
set node [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]


spawn ssh $username@localhost -p $node
expect {
"(yes/no)?"
{
send "yesn"
expect "*assword:" { send "$passwordn"}
}
"*assword:" { send "$passwordn" }
}

expect {
"*#" { send "$commandn" }
}

expect {
"*#" { send "exitn" }
}

expect eof


Thank you










share|improve this question















This question already has an answer here:




  • How to run the sftp command with a password from Bash script?

    9 answers




I want to ssh to multiple server using bash script and automate this script using crontab. I use "expect" to ssh to multiple server because of authentication need. But, I don't know how to copy the file in destination server using SFTP to my server. Can someone give me some clue for this problem.



Here is my code to SSH to multiple server (in this case I make tunneling to server destination):



/home/users/script/expect.sh 45108 username password "command" 
/home/users/script/expect.sh 45109 username password "command"
#45108 is port for tunneling, username and password is using like in shell terminal (ssh username@ipadd -p $server)


and this is the expect script that I use:



#!/usr/bin/expect
set timeout 10
set node [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]


spawn ssh $username@localhost -p $node
expect {
"(yes/no)?"
{
send "yesn"
expect "*assword:" { send "$passwordn"}
}
"*assword:" { send "$passwordn" }
}

expect {
"*#" { send "$commandn" }
}

expect {
"*#" { send "exitn" }
}

expect eof


Thank you





This question already has an answer here:




  • How to run the sftp command with a password from Bash script?

    9 answers








linux bash ssh sftp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 4 at 7:49









Kresna LitaKresna Lita

64




64




marked as duplicate by Martin Prikryl sftp
Users with the  sftp badge can single-handedly close sftp questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 8 at 6:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Martin Prikryl sftp
Users with the  sftp badge can single-handedly close sftp questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 8 at 6:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    What about How to run the sftp command with a password from Bash script?

    – Martin Prikryl
    Jan 4 at 8:07











  • Do you want to scp instead of ssh, or should both happen? In what sequence? Repurposing your expect script to use scp instead of ssh should be completely trivial, though the prompts etc will differ.

    – tripleee
    Jan 4 at 8:07











  • if we use "sshpass", is it already ssh to remote server ?... and as you know, I need to upload the file to my server and with one script I need to ssh to multiple remote server. is it possible? @MartinPrikryl

    – Kresna Lita
    Jan 4 at 8:27











  • I do not understand what you mean by "if we use "sshpass", is it already ssh to remote server".

    – Martin Prikryl
    Jan 4 at 8:28













  • sorry I'm newbie at bash script. I will read about ssh and scp. Thanks for your clue @tripleee

    – Kresna Lita
    Jan 4 at 8:34














  • 1





    What about How to run the sftp command with a password from Bash script?

    – Martin Prikryl
    Jan 4 at 8:07











  • Do you want to scp instead of ssh, or should both happen? In what sequence? Repurposing your expect script to use scp instead of ssh should be completely trivial, though the prompts etc will differ.

    – tripleee
    Jan 4 at 8:07











  • if we use "sshpass", is it already ssh to remote server ?... and as you know, I need to upload the file to my server and with one script I need to ssh to multiple remote server. is it possible? @MartinPrikryl

    – Kresna Lita
    Jan 4 at 8:27











  • I do not understand what you mean by "if we use "sshpass", is it already ssh to remote server".

    – Martin Prikryl
    Jan 4 at 8:28













  • sorry I'm newbie at bash script. I will read about ssh and scp. Thanks for your clue @tripleee

    – Kresna Lita
    Jan 4 at 8:34








1




1





What about How to run the sftp command with a password from Bash script?

– Martin Prikryl
Jan 4 at 8:07





What about How to run the sftp command with a password from Bash script?

– Martin Prikryl
Jan 4 at 8:07













Do you want to scp instead of ssh, or should both happen? In what sequence? Repurposing your expect script to use scp instead of ssh should be completely trivial, though the prompts etc will differ.

– tripleee
Jan 4 at 8:07





Do you want to scp instead of ssh, or should both happen? In what sequence? Repurposing your expect script to use scp instead of ssh should be completely trivial, though the prompts etc will differ.

– tripleee
Jan 4 at 8:07













if we use "sshpass", is it already ssh to remote server ?... and as you know, I need to upload the file to my server and with one script I need to ssh to multiple remote server. is it possible? @MartinPrikryl

– Kresna Lita
Jan 4 at 8:27





if we use "sshpass", is it already ssh to remote server ?... and as you know, I need to upload the file to my server and with one script I need to ssh to multiple remote server. is it possible? @MartinPrikryl

– Kresna Lita
Jan 4 at 8:27













I do not understand what you mean by "if we use "sshpass", is it already ssh to remote server".

– Martin Prikryl
Jan 4 at 8:28







I do not understand what you mean by "if we use "sshpass", is it already ssh to remote server".

– Martin Prikryl
Jan 4 at 8:28















sorry I'm newbie at bash script. I will read about ssh and scp. Thanks for your clue @tripleee

– Kresna Lita
Jan 4 at 8:34





sorry I'm newbie at bash script. I will read about ssh and scp. Thanks for your clue @tripleee

– Kresna Lita
Jan 4 at 8:34












1 Answer
1






active

oldest

votes


















-1















  1. Add your servers in ~/.ssh/config with use syntax:


```



Host server1
HostName 192.168.1.10

Host server2
HostName 192.168.1.11


```




  1. Create ssh key: ssh-keygen,

  2. Add your public key ~/.ssh/rsa.pub to ~/.ssh/authorized_keys on server.


Now, you can send file by use scp: scp file.zip server1:~/
https://nerderati.com/2011/03/17/simplify-your-life-with-an-ssh-config-file/






share|improve this answer
























  • Thanks for your answer. But, I already make a tunnel and what I want to know is sftp and in your answer is using scp right. So, I think it's clear.

    – Kresna Lita
    Jan 4 at 8:41











  • Ok, maybe this: rsync -av -e "sshpass -f '/tmp/pass.txt' ssh -o StrictHostKeyChecking=no" . USER@HOST:backups/ > /dev/null. Write password in file /tmp/pass.txt before echo "pass" > /tmp/pass.txt. This is solve for problem with password auth.

    – Mikołaj
    Jan 4 at 8:51






  • 1





    Why sftp instead of scp? ugh...

    – Paul Hodges
    Jan 4 at 14:38


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1















  1. Add your servers in ~/.ssh/config with use syntax:


```



Host server1
HostName 192.168.1.10

Host server2
HostName 192.168.1.11


```




  1. Create ssh key: ssh-keygen,

  2. Add your public key ~/.ssh/rsa.pub to ~/.ssh/authorized_keys on server.


Now, you can send file by use scp: scp file.zip server1:~/
https://nerderati.com/2011/03/17/simplify-your-life-with-an-ssh-config-file/






share|improve this answer
























  • Thanks for your answer. But, I already make a tunnel and what I want to know is sftp and in your answer is using scp right. So, I think it's clear.

    – Kresna Lita
    Jan 4 at 8:41











  • Ok, maybe this: rsync -av -e "sshpass -f '/tmp/pass.txt' ssh -o StrictHostKeyChecking=no" . USER@HOST:backups/ > /dev/null. Write password in file /tmp/pass.txt before echo "pass" > /tmp/pass.txt. This is solve for problem with password auth.

    – Mikołaj
    Jan 4 at 8:51






  • 1





    Why sftp instead of scp? ugh...

    – Paul Hodges
    Jan 4 at 14:38
















-1















  1. Add your servers in ~/.ssh/config with use syntax:


```



Host server1
HostName 192.168.1.10

Host server2
HostName 192.168.1.11


```




  1. Create ssh key: ssh-keygen,

  2. Add your public key ~/.ssh/rsa.pub to ~/.ssh/authorized_keys on server.


Now, you can send file by use scp: scp file.zip server1:~/
https://nerderati.com/2011/03/17/simplify-your-life-with-an-ssh-config-file/






share|improve this answer
























  • Thanks for your answer. But, I already make a tunnel and what I want to know is sftp and in your answer is using scp right. So, I think it's clear.

    – Kresna Lita
    Jan 4 at 8:41











  • Ok, maybe this: rsync -av -e "sshpass -f '/tmp/pass.txt' ssh -o StrictHostKeyChecking=no" . USER@HOST:backups/ > /dev/null. Write password in file /tmp/pass.txt before echo "pass" > /tmp/pass.txt. This is solve for problem with password auth.

    – Mikołaj
    Jan 4 at 8:51






  • 1





    Why sftp instead of scp? ugh...

    – Paul Hodges
    Jan 4 at 14:38














-1












-1








-1








  1. Add your servers in ~/.ssh/config with use syntax:


```



Host server1
HostName 192.168.1.10

Host server2
HostName 192.168.1.11


```




  1. Create ssh key: ssh-keygen,

  2. Add your public key ~/.ssh/rsa.pub to ~/.ssh/authorized_keys on server.


Now, you can send file by use scp: scp file.zip server1:~/
https://nerderati.com/2011/03/17/simplify-your-life-with-an-ssh-config-file/






share|improve this answer














  1. Add your servers in ~/.ssh/config with use syntax:


```



Host server1
HostName 192.168.1.10

Host server2
HostName 192.168.1.11


```




  1. Create ssh key: ssh-keygen,

  2. Add your public key ~/.ssh/rsa.pub to ~/.ssh/authorized_keys on server.


Now, you can send file by use scp: scp file.zip server1:~/
https://nerderati.com/2011/03/17/simplify-your-life-with-an-ssh-config-file/







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 4 at 7:57









MikołajMikołaj

616




616













  • Thanks for your answer. But, I already make a tunnel and what I want to know is sftp and in your answer is using scp right. So, I think it's clear.

    – Kresna Lita
    Jan 4 at 8:41











  • Ok, maybe this: rsync -av -e "sshpass -f '/tmp/pass.txt' ssh -o StrictHostKeyChecking=no" . USER@HOST:backups/ > /dev/null. Write password in file /tmp/pass.txt before echo "pass" > /tmp/pass.txt. This is solve for problem with password auth.

    – Mikołaj
    Jan 4 at 8:51






  • 1





    Why sftp instead of scp? ugh...

    – Paul Hodges
    Jan 4 at 14:38



















  • Thanks for your answer. But, I already make a tunnel and what I want to know is sftp and in your answer is using scp right. So, I think it's clear.

    – Kresna Lita
    Jan 4 at 8:41











  • Ok, maybe this: rsync -av -e "sshpass -f '/tmp/pass.txt' ssh -o StrictHostKeyChecking=no" . USER@HOST:backups/ > /dev/null. Write password in file /tmp/pass.txt before echo "pass" > /tmp/pass.txt. This is solve for problem with password auth.

    – Mikołaj
    Jan 4 at 8:51






  • 1





    Why sftp instead of scp? ugh...

    – Paul Hodges
    Jan 4 at 14:38

















Thanks for your answer. But, I already make a tunnel and what I want to know is sftp and in your answer is using scp right. So, I think it's clear.

– Kresna Lita
Jan 4 at 8:41





Thanks for your answer. But, I already make a tunnel and what I want to know is sftp and in your answer is using scp right. So, I think it's clear.

– Kresna Lita
Jan 4 at 8:41













Ok, maybe this: rsync -av -e "sshpass -f '/tmp/pass.txt' ssh -o StrictHostKeyChecking=no" . USER@HOST:backups/ > /dev/null. Write password in file /tmp/pass.txt before echo "pass" > /tmp/pass.txt. This is solve for problem with password auth.

– Mikołaj
Jan 4 at 8:51





Ok, maybe this: rsync -av -e "sshpass -f '/tmp/pass.txt' ssh -o StrictHostKeyChecking=no" . USER@HOST:backups/ > /dev/null. Write password in file /tmp/pass.txt before echo "pass" > /tmp/pass.txt. This is solve for problem with password auth.

– Mikołaj
Jan 4 at 8:51




1




1





Why sftp instead of scp? ugh...

– Paul Hodges
Jan 4 at 14:38





Why sftp instead of scp? ugh...

– Paul Hodges
Jan 4 at 14:38





Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas