LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-22-2013, 10:17 AM   #1
jbeiter
Member
 
Registered: Jul 2004
Posts: 105

Rep: Reputation: 15
ssh help with scripting a remote command using redirect


Hoping someone can help me find a way around this.

I have a script that does a mysql dump, transfers it to a standby system then I want to import that into the remote mysql.

It's restricted to run one command that parses input for bad stuff then when satisfied it's only running mysql, runs the command which is passed as a variable.

my problem is, ssh fails to parse a redirect. eg:

ssh user@remotehost "mysql -v -pmy_password < file_I_just_scped"

I get a syntax error from mysql. I can't run bash (to parse the < correctly) because bash won't run the binary.

Anyone know of a clever way around this, besides using a script on the remote system?
 
Old 10-23-2013, 03:09 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

you shouldn't need to scp a sript or even the file, instead pipe it into ssh. Eg
Code:
cat file_that_I_dont_need_to_scp | ssh user@remotehost mysql -v -pmy_insecurepassword
Evo2.
 
Old 10-23-2013, 03:11 AM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

hmm, just realized that you don't even need the temp file. How about:
Code:
mysqldump somedatabase | ssh user@remotehost mysql -v -ppassword
Evo2.
 
Old 10-23-2013, 07:13 AM   #4
jbeiter
Member
 
Registered: Jul 2004
Posts: 105

Original Poster
Rep: Reputation: 15
Hmm I still get a syntax error from mysql. Do I need a "<" at the end of it or should it just take the dump from the pipe?

I was able to get mysql to interpret the redirection by invoking it with sudo. I like your suggestion better though, if I can get it to work.


mysqldump -ppassw --add-drop-table --all-databases | ssh user@remotehst /usr/bin/mysql -v -ppassw

yields a syntax error from the remote mysql.

Thank you for the assistance.
 
Old 10-23-2013, 10:05 AM   #5
jbeiter
Member
 
Registered: Jul 2004
Posts: 105

Original Poster
Rep: Reputation: 15
I don't think it was playing nice with the pipe, for some reason unknown to me. I got it to work by running this on the remote/destination host:

mysql -v -ppassw < <(ssh user@source-host "mysqldump -ppassw --all-databases")
 
Old 10-25-2013, 01:10 PM   #6
jbeiter
Member
 
Registered: Jul 2004
Posts: 105

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jbeiter View Post
mysql -v -ppassw < <(ssh user@source-host "mysqldump -ppassw --all-databases")
can anyone tell me why that works fine from the command line, but if I put it into a script I get this syntax error?:

sync_mysql.sh: line 122: syntax error near unexpected token `<'
sync_mysql.sh: line 122: `/usr/bin/mysql -v -ppass < <(ssh user@source-host "mysqldump -ppassw --add-drop-table --all-databases")'
 
Old 10-25-2013, 01:56 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
You should put #!/bin/bash at the top of your script. The <() construct is bash specific.
 
Old 10-25-2013, 02:29 PM   #8
jbeiter
Member
 
Registered: Jul 2004
Posts: 105

Original Poster
Rep: Reputation: 15
yes, it's in there. That was just a snippit of a larger file
 
Old 10-25-2013, 05:26 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by jbeiter View Post
yes, it's in there.
Oh, it seems like the script is being run with /bin/sh instead of /bin/bash, if you do have #!/bin/bash, maybe you are actually running the script by sh myscript.sh? Otherwise, I don't know...
 
  


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
[SOLVED] ssh remote command flashback Linux - General 6 11-10-2011 07:14 AM
[SOLVED] Remote command execution via SSH and newgrp command SuperMegaMau Linux - General 4 05-13-2011 07:31 AM
exec and ssh : command problem in scripting shashi792 Programming 1 08-04-2010 05:51 AM
redirect output to remote server via ssh packets Programming 4 05-19-2009 08:30 PM

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

All times are GMT -5. The time now is 08:07 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