LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-19-2011, 12:34 PM   #1
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Rep: Reputation: 17
Expect script


I am writing an expect backup script to log in to a lot of systems and run a large tar command and send it to the backupserver. the problem is I am getting this error:
Code:
extra characters after close-quote
    while executing
 "send "sudo tar -zcvf - /dir/dir/dir..."

I am totaly stuck and I have no idea how to fix this.

Below you will find my script I am writing that is getting this error. Assume that all of the variables are correct.

Code:
#!/bin/bash
for server in ${servers}; do
   /usr/bin/expect  << EOF
        spawn ssh -t ${username}@${server}
        expect "*?assword: "
        send "${PASSWD}\n"
        expect "*]$ "
        send "sudo su -\n"
        expect "*?assword*${username}: "
        send "$( echo -n "$PASSWD" | od -An -vtx1 | tr -d '\n' | sed 's/ /\\x/g' )\n"
        expect "*]# "
        send "sudo tar -zcvf - $(find /long/list/of/diffrent/directories/and/files -mtime -90 -mtime +1 -type f) | ssh -t $username@$backupserver "cat > /backup/directory/$(hostname -s)/$(hostname -s)_$(date +%Y%m%d).tgz""
        send "\n"
        expect { "*(yes/no)? " { send "yes\n" } "*?assword*${username}: " { send "$( echo -n "$PASSWD" | od -An -vtx1 | tr -d '\n' | sed 's/ /\\x/g' )\n" } }
        send "\r"
        expect "*]# "

EOF
done
 
Old 08-19-2011, 01:08 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by edwardcode View Post
I am writing an expect backup script to log in to a lot of systems and run a large tar command and send it to the backupserver. the problem is I am getting this error:
Code:
extra characters after close-quote
    while executing
 "send "sudo tar -zcvf - /dir/dir/dir..."

I am totaly stuck and I have no idea how to fix this.

Below you will find my script I am writing that is getting this error. Assume that all of the variables are correct.

Code:
#!/bin/bash
for server in ${servers}; do
   /usr/bin/expect  << EOF
        spawn ssh -t ${username}@${server}
        expect "*?assword: "
        send "${PASSWD}\n"
        expect "*]$ "
        send "sudo su -\n"
        expect "*?assword*${username}: "
        send "$( echo -n "$PASSWD" | od -An -vtx1 | tr -d '\n' | sed 's/ /\\x/g' )\n"
        expect "*]# "
        send "sudo tar -zcvf - $(find /long/list/of/diffrent/directories/and/files -mtime -90 -mtime +1 -type f) | ssh -t $username@$backupserver "cat > /backup/directory/$(hostname -s)/$(hostname -s)_$(date +%Y%m%d).tgz""
        send "\n"
        expect { "*(yes/no)? " { send "yes\n" } "*?assword*${username}: " { send "$( echo -n "$PASSWD" | od -An -vtx1 | tr -d '\n' | sed 's/ /\\x/g' )\n" } }
        send "\r"
        expect "*]# "

EOF
done
You need to escape the nested ", or use ':
Code:
send "sudo tar -zcvf - $(find /long/list/of/diffrent/directories/and/files -mtime -90 -mtime +1 -type f) | ssh -t $username@$backupserver \"cat > /backup/directory/$(hostname -s)/$(hostname -s)_$(date +%Y%m%d).tgz\""
You should use public-key authentication with ssh instead of expect. It will allow you to log in non-interactively, which will let you pass the commands directly.
Kevin Barry
 
Old 08-19-2011, 01:33 PM   #3
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Original Poster
Rep: Reputation: 17
Thanks. That did bring an other problem up though. When I ran the script it showed me this output(found below). One of the problems is it looks like it is trying to run each directory found by the "find" portion of the tar command, as its own command. I have no idea why? I have run the tar command as its own script an it works fine but when I put it in this script it craps out on me.


Code:
[root@hostname ~]# sudo tar -zcvf - /one/of/the/directories/or/files
tar: Removing leading `/' from member names
/var/log/httpd/error_log
Â¥NèÂÂÃÃÂ}í§ÃIkÃJª^ÂÃB
af#m$µ+öÃ*Ãä$lÇ Â¸Â´Ãw#¤ýÿÃA«5ÃþJÃ¥~amÂÂ
 ÂKeÃT,Â*RÙ²ÃÃBÃn´à¶9\ÂÆøvå¬Ã
Ü
ðÂÂo¡Ã*à                                                                                                                                                                                                                                  \Ã*2ÂhÃ>´¾ÂoÃÃ×ƴµZf¥©Uz¾Â*jè¤5©yHÂ|,s§q£ÂÂÂ*Â
qU£+®ø
T-4f§¤ÃïÂxÒ±$ÂÂpô¢Ã~%
                            Ã_¯Â
FÂF9Â)ÃÞ¬J!Â*ÃÃ袱3eÃñ²MÃâÂó_(к]Z£ ö3Â}ûòõþڥ¢\ÂÃÂX+ªºÃ*ÂUM28ÃöÂVxÂÂöô=ÃÃZ½;úZóS°ÃeO3Ã¥./lSê»ÛÃ
                                         Â                                                                             0Â
kÃÃN
³ï¥tX+µ*ÃV"û
                                                                                                ;hõÂ*ñÃþAAAAAAAÃ
                                 ÃçHOéÃÿøªnÃ1¯«³ÂLÃÃ#óÂVÿCÂ*pOÂÃ/cxp°tþ,µÃYjÃÃ÷d                  ÃoÿÃdS([root@hostname ~]#
[root@hostname ~]# /var/log/audit/audit.log.3
PuTTY-bash: /var/log/audit/audit.log.3: No such file or directory
PuTTY[root@hostname ~]# /var/log/audit/audit.log.1
-bash: /var/log/audit/audit.log.1: No such file or directory
[root@hostname ~]# /var/log/audit/audit.log.2
-bash: /var/log/audit/audit.log.2: No such file or directory
[root@hostname ~]# /var/log/audit/audit.log.4
-bash: /var/log/audit/audit.log.4: No such file or directory
This is the out put it should have

Code:
 sudo tar -zcvf - /var/log/audit/audit.log.3 /var/log/audit/audit.log.4 /var/log/audit/audit.log.2
tar: Removing leading `/' from member names
/list/a/lot/of/files
then ssh to the backup server for "tar"ing

Last edited by edwardcode; 08-19-2011 at 01:35 PM.
 
Old 08-19-2011, 02:03 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by edwardcode View Post
Thanks. That did bring an other problem up though. When I ran the script it showed me this output(found below). One of the problems is it looks like it is trying to run each directory found by the "find" portion of the tar command, as its own command. I have no idea why? I have run the tar command as its own script an it works fine but when I put it in this script it craps out on me.
I still think you should use passwordless ssh login to avoid having to use expect. Regardless, what happens if you change this:
Code:
sudo tar -zcvf - $(find /long/list/of/diffrent/directories/and/files -mtime -90 -mtime +1 -type f)
to this:
Code:
find /long/list/of/diffrent/directories/and/files -mtime -90 -mtime +1 -type f | sudo tar -zcvf - -T -
Kevin Barry
 
Old 08-24-2011, 11:23 AM   #5
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Original Poster
Rep: Reputation: 17
I figured out what was causing the error. you need to escape a lot of characters. see below


Code:
send "sudo tar -zcvf - `$`(find /long/list/of/diffrent/directories/and/files -mtime -90 -mtime +1 -type f\) | ssh -t $username@$backupserver \"cat > /backup/directory/\`hostname -s\`/\`hostname -s\`_\$(\date +%Y%m%d\).tgz\""

Last edited by edwardcode; 08-24-2011 at 11:25 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem running an Expect script within a Bash script mbeipi Programming 9 02-10-2018 05:00 AM
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
running shell script within expect script xombboxer Linux - Newbie 2 05-01-2011 12:42 PM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration