Expect Script for backing up files on different servers


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Expect Script for backing up files on different servers
# 1  
Old 10-03-2014
Expect Script for backing up files on different servers

Hello,

I am trying to create an expect script that backups up data from multiple servers with the same username, but with different passwords for each server. For example, I have two servers with the "myuser" account with different passwords for that account for each server.

I went ahead and created a file called "IP_Passwords.txt" with the IP addresses of the servers as well as the passwords.
ex. "10.x.x.x changeme"

I am having difficulty trying to get the script to read the whole line of data so that I can separate them into separate variables to be referenced later on in the script.

I have tried the following commands, but the host variable only contains the server IP addresses:
Code:
set hostf [open "/var/tmp/IP_Passwords.txt" "r"]
foreach host [read $hostf]

Code:
set hostf [open "/var/tmp/IP_Passwords.txt" "r"]
foreach host [read -nonewline $hostf]

Is there any way for this command to read both the server IPs/Passwords so that I can use them later on? I have included my code below:

Code:
log_user 1
set hostf [open "/var/tmp/IP_Passwords.txt" "r"]
set user "myuser"
set timeout 60
set fullname ""


foreach host [read $hostf] {
   puts ""
   puts "Host: $host"
   spawn sftp -o StrictHostKeyChecking=no $user@$host
   set pass1 [read $hostf]
   puts "Password is: $pass1"
   expect {
                "*ssword:" {send "$pass1\r"}
                "(yes/no)?" {send "yes\r"; exp_continue }
                "Connection Refused" {puts "$host: Connection Refused"; continue}
                "No address associated to the name" {puts "$host: No address associated to the name"; continue}
                "no more authentication methods available" {puts "$host: Has no more authentication methods available"; continue}
                "node name or service name not known" {puts "$host: node name or service name not known"; continue}
                timeout {puts "$host: Script Timed Out"; continue}
          }
   sleep 2
   send "get /files_to_backup/*\r"
   expect "sftp> "
   sleep 1
}
close $hostf
exit

Thanks in advance.
# 2  
Old 10-03-2014
Have you tried passwordless ssh/scp/sftp instead of insecurely cramming plaintext passwords into it with the expect bruteforcing tool?

There's a reason it's so hard to cram passwords into it. It's an important security feature designed to stop you from doing exactly what you're trying to do, because it's never a good idea.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-03-2014
Corona688, thanks for the input - wasn't something I was originally considering. I wanted to also know how to do it for my own knowledge, but this definitely helps!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script to delete files from 5 different servers

Hello, I'm new to shell scripting and need a quick note on how to write a shell script to perform deletion of files from 5 different hostnames in various locations. Found out to delete files from one path by using below command and made it to work on cron job but need to do it in a shell... (2 Replies)
Discussion started by: Teja G
2 Replies

2. Shell Programming and Scripting

Shell script using expect to login to couple of remote servers and read "crontab -l"

I need a shell script using expect to login to couple of remote servers and read "crontab -l -u <username>" & "cat /etc/rc.local" & "df -h" and able to create output into a file saved locally with hostname.crontab & hostname.rc.local & disk.status. I can supply a file as list of hostname or IP... (4 Replies)
Discussion started by: jaipsharma
4 Replies

3. Solaris

Backing up Solaris 10 servers

Got 5-6 Solaris 10 servers in remote location - power work being done. At the moment, we just back up the application database that these servers run with the idea that if it all goes wrong we can rebuild and then restore the application. However, requirement is to provide a complete backup... (3 Replies)
Discussion started by: paulfoel
3 Replies

4. Shell Programming and Scripting

Help on Backing up all the files in the subdirectories under a parent directory

Hi, I am not too familiar with Unix scripting but I have to write code to find all the files under all the sub directories under a parent directory of unix location and move them to the corresponding Windows location. For eg: I have \home\sreenu\Files\ Under neath this I have multiple sub... (3 Replies)
Discussion started by: raj.sreenu
3 Replies

5. Shell Programming and Scripting

Script to login to several servers and get files

Hi ppl, I am looking out for a shell script a. That would have to login(from a main server) to say 16 servers individually. b.On each server go to a particular location, check if a particular file is generated on a date(say every sunday it gets generated and i would be interested in the latest... (8 Replies)
Discussion started by: yohasini
8 Replies

6. UNIX for Advanced & Expert Users

Script to login to several servers and get files

Hi ppl, I am looking out for a shell script a. That would have to login(from a main server) to say 16 servers individually. b.On each server go to a particular location, check if a particular file is generated on a date(say every sunday it gets generated and i would be interested in the latest... (1 Reply)
Discussion started by: yohasini
1 Replies

7. UNIX for Dummies Questions & Answers

Script to login to several servers and get files

Hi ppl, I am looking out for a shell script a. That would have to login(from a main server) to say 16 servers individually. b.On each server go to a particular location, check if a particular file is generated on a date(say every sunday it gets generated and i would be interested in the latest... (0 Replies)
Discussion started by: yohasini
0 Replies

8. UNIX for Advanced & Expert Users

automate backing up uploaded files

Hi, We are running FTP Server on UNIX (Solaris 9). Users login and upload (also download) files whenever required. Now, we have to automate the process which makes a copy of every file immediately after it gets uploaded to the FTP server (by any user). I've ruled out rsync,mirror,filesync... (2 Replies)
Discussion started by: prvnrk
2 Replies

9. UNIX for Dummies Questions & Answers

Backing up or Archiving files in UNIX

Hi All, I am very new to the UNIX world and find myself in a new position at work that requires me to archive large CADD files based in both UNIX and Windows environments on CD's. I have one engineer that wants to export these files as a table (I guess) and it appears to have a lot of paper... (2 Replies)
Discussion started by: Dsartelle
2 Replies

10. UNIX for Dummies Questions & Answers

Backing up files to a remote host question.

I need to know how to successfully back up(and compress) files from a local machine to a remote host. Will this work? tar -cvf backup.tar -C /user/somedir | gzip backup.tar | rsh some.domain.com/user/somedir thanks in advance! (3 Replies)
Discussion started by: WeNdeL
3 Replies
Login or Register to Ask a Question