LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-13-2016, 01:33 PM   #1
bishop2001
Member
 
Registered: Apr 2015
Posts: 58

Rep: Reputation: Disabled
Remote ssh command output save locally expect


Greetings,
i have the following below which runs an egrep command on a remote machine and sends the output to my screen, but i can't ">" redirect it to a local file. Help Please ? Thanks,

for i in $HOSTS
do
DATE=`date +%Y-%m-%d`
${expectbin} -c "
spawn ssh admin@$i egrep $DATE /var/log > ./$DATE.log
expect "Password: "
send -- "mypasswd\r"
expect eof"
done
 
Old 01-14-2016, 08:40 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Rather than use expect why not setup an ssh trust from your local user to the admin user on each of your $HOSTS? By doing that you can run ssh without it prompting for password. This is more secure because it means you don't have to store the password in clear text in your expect script. It will also let you save the output of ssh:

Code:
for i in $HOSTS
do
DATE=`date +%Y-%m-%d`
ssh admin@$i 'egrep $DATE /var/log' >> ./$DATE.log
done
In the above the items between single quotes are what you're telling the remote host to do. I also changed your ">" to ">>" because the single one would overwrite $DATE.log on each host whereas the double one appends the next host's output to the one(s) previously done.

What are your target hosts? On Linux /var/log is usually a directory so your egrep wouldn't find anything as is.

Also as an FYI the use of `<cmd>` is deprecated. You should get in the habit of using $(<cmd>) instead e.g.
DATE=$(date +%Y-%m-%d)
One main benfeit of the newer form is you can nest things more easily. It also eliminates confusion when reading your scripts between ` and '.
 
  


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
[SOLVED] SSH remote command: Pipe remote output to local machine? kenneho Linux - Server 6 12-06-2012 01:37 AM
running shell command locally and over ssh PoleStar Linux - Newbie 2 07-22-2012 12:22 PM
Transferring remote files via ssh,find,cpio locally metallica1973 Programming 11 11-28-2011 12:41 PM
ssh - compose the line locally and then submit the command? (for slow connections) jago25_98 Linux - Server 3 12-29-2010 07:43 PM
SSH remote command withouth output Ricio Linux - Newbie 5 10-20-2008 11:44 AM

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

All times are GMT -5. The time now is 11:32 PM.

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