LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-15-2010, 09:59 AM   #1
cheltz
Member
 
Registered: Apr 2010
Posts: 62

Rep: Reputation: 2
Scripting issue: Reading a file from a remote server


I have an situation where I want to read a file into my script that lie on a remote server. I have ssh-keys set up just don't know how to do this.

Thanks if you respond.
 
Old 10-15-2010, 10:05 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,314

Rep: Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877
Quote:
Originally Posted by cheltz View Post
I have an situation where I want to read a file into my script that lie on a remote server. I have ssh-keys set up just don't know how to do this.

Thanks if you respond.
Well, post what you've written so far, and we can help. Basic steps are:
  • Connect to remote server
  • Either copy the file to your system locally, or run a "cat" (or other), command to read the file into a variable on the fly.
  • Process according to your wishes

There are MANY bash scripting tutorials you can find on Google, if you're just starting out.
 
Old 10-15-2010, 10:32 AM   #3
cheltz
Member
 
Registered: Apr 2010
Posts: 62

Original Poster
Rep: Reputation: 2
I tried to ssh to the remote server and cat the file, but when i turned on the trace (set -x) it would sit at the $ prompt of the remote server and the rest of the script did not run.
 
Old 10-15-2010, 10:46 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,314

Rep: Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877Reputation: 3877
Quote:
Originally Posted by cheltz View Post
I tried to ssh to the remote server and cat the file, but when i turned on the trace (set -x) it would sit at the $ prompt of the remote server and the rest of the script did not run.
Ok..again, post what you've written. And have you checked out the scripting tutorials???
http://tldp.org/LDP/abs/html/

Something to try is
Code:
variable = `ssh <user>@<host> "<command string goes here>"`
Those are backticks, not single-quotes. That runs the command, and shoves it into a variable. So if you put "cat myfile.txt", you'll get the ouput of myfile.txt into that variable.
 
Old 10-15-2010, 03:09 PM   #5
ckoniecny
Member
 
Registered: Oct 2005
Posts: 162

Rep: Reputation: 30
You can set up SMB shares and use "smbmount" to mount the share across the network. That would let you easily read a file on another server.

Or you can look into using the "rcp" command.
 
Old 10-16-2010, 12:01 AM   #6
joec@home
Member
 
Registered: Sep 2009
Location: Galveston Tx
Posts: 291

Rep: Reputation: 70
I had a project where I wanted to run the same script on multiple servers from a single point. This became a small application that I am not at liberty to share, but at the heart of it was the following command:

cat remote_script | sshpass -p`echo $PASSWORD` ssh root@$LIST bash

So remote_script would be the script I wanted to run. The catch 22 was the script had to use Java style carriage returns in the script and end with an exit command.

Example

Code:
#!/bin/bash
hostname
hostname -a
uname -a
ls -la
The same code would instead look like

Code:
hostname ;\
hostname -a ;\
uname -a ;\
ls -la ;\
exit
 
Old 10-18-2010, 05:33 PM   #7
cheltz
Member
 
Registered: Apr 2010
Posts: 62

Original Poster
Rep: Reputation: 2
TB0ne's ssh string was what i was looking for. What had me hung up was that there's no colon after the ip address.

Thanks all.
 
Old 10-18-2010, 05:34 PM   #8
cheltz
Member
 
Registered: Apr 2010
Posts: 62

Original Poster
Rep: Reputation: 2
I cannot do shares by the way they are forbidden for security.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] read from keyboard while reading from file in SHELL SCRIPTING m3ll0 Programming 11 10-30-2010 09:30 AM
Server crashing, Mod security issue or bad scripting ? fryzer Linux - Security 2 01-07-2009 11:23 AM
Need help diagnosing a remote server (vps) issue. jim.thornton Linux - Server 7 07-08-2008 01:22 AM
Reading text from a file using shell scripting. mrobertson Programming 11 06-29-2005 01:12 PM


All times are GMT -5. The time now is 05:23 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration