How to script multiple ssh and scp commands to various systems
You can simplify this process a lot by tunneling ssh connections over other ssh connections (see this previous answer). The way I'd do it is to create an .ssh/config file on the LOCAL system with the following entries:
Host SYSTEM3
ProxyCommand ssh -e none SYSTEM2 exec /usr/bin/nc %h %p 2>/dev/null
HostName SYSTEM3.full.domain
User system3user
Host SYSTEM2
ProxyCommand ssh -e none SYSTEM1 exec /usr/bin/nc %h %p 2>/dev/null
HostName SYSTEM2.full.domain
User system2user
Host SYSTEM1
HostName SYSTEM1.full.domain
User system1user
(That's assuming both intermediate hosts have netcat installed as /usr/bin/nc -- if not, you may have to find/install some equivalent way of gatewaying stdin&stdout into a TCP session.)
With this set up, you can use scp SYSTEM3:/data /data
on LOCAL, and it'll automatically tunnel through SYSTEM1 and SYSTEM2 (and ask for the passwords for the three SYSTEMn's in order -- this can be a little confusing, especially if you mistype one).
What is the cleanest way to ssh and run multiple commands in Bash?
How about a Bash Here Document:
ssh otherhost << EOF
ls some_folder;
./someaction.sh 'some params'
pwd
./some_other_action 'other params'
EOF
To avoid the problems mentioned by @Globalz in the comments, you may be able to (depending what you're doing on the remote site) get away with replacing the first line with
ssh otherhost /bin/bash << EOF
Note that you can do variable substitution in the Here document, but you may have to deal with quoting issues. For instance, if you quote the "limit string" (ie. EOF
in the above), then you can't do variable substitutions. But without quoting the limit string, variables are substituted. For example, if you have defined $NAME
above in your shell script, you could do
ssh otherhost /bin/bash << EOF
touch "/tmp/${NAME}"
EOF
and it would create a file on the destination otherhost
with the name of whatever you'd assigned to $NAME
. Other rules about shell script quoting also apply, but are too complicated to go into here.
Automate SCP with multiple files with expect script
I found what I wanted with much more googleing. Thankyou for your help, hope this helps others
http://www.linuxquestions.org/questions/linux-general-1/scp-with-wildcard-in-expect-834813/
#!/usr/bin/expect -f
spawn bash -c "scp /path/* root@IP:/tmp/"
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "Password\r"
}
}
interact
Related Topics
Golang Math Can Not Finished with My Code, But Python Is Ok
Sublime Text 2 Build (Ctrl +B) Intel Fortran Compiler
How to Run a Linux Executable from Any Directory in Terminal
Printing Floating Point Numbers in Assembler
Nohup Create New Files Nohup.Out by Day
Linux Select() and Fifo Ordering of Multiple Sockets
Chef-Server Stuck Ruby_Block[Supervise_Rabbitmq_Sleep] Action Run on Docker Container
Linux Grep/Sed Certain Lines - Space Removal
Why a Static Library Can Depend on a Shared a Library
Unexpected Eof While Looking for Matching '"'
Grep in File and Replace String
Level Triggered Interrupt Handling and Nested Interrupts