LinuxQuestions.org
Help answer threads with 0 replies.
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 03-24-2011, 09:28 AM   #1
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
ssh - remote


Hi all

I'm kinda new to linux and stuff.
I'll try to explain what the situation is:
i need to connect to a remote linux server on certain times (automated), but i need to log on to the server. So i want to make a script (shell perhaps) that periodically connects to the server and checks some files and folders.
How do i automate it and how can i log on automatically to the remote server without typing everytime the password and login (the script should do it himself)

i hope that you'll understand what i'm saying (my english is not that good).

pls help me ^^

grtz

cookie
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-24-2011, 09:36 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209
Hi and Welcome to LQ

1. This is how to run commands remotely.
http://bashcurescancer.com/run_remot..._with_ssh.html
2. To automate it you need to set up a cron job for it.
3. To avoid typing the password everytime, you could use keys
http://www.slackword.net/?p=40
 
Old 03-24-2011, 09:39 AM   #3
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello and Welcome to LinuxQuestions,

You can use key instead of password authentication as explained for example for Debian (since you didn't mention your distro) on this site. Another option is to use a program like sshpass to provide the password to ssh on the commandline or in a script for that matter. I'm assuming you'll be connecting from a Linux machine to a SSH server. If you connect from your Windows machine then other paths will need to be followed.

Kind regards,

Eric
 
1 members found this post helpful.
Old 03-24-2011, 09:42 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,477

Rep: Reputation: 3970Reputation: 3970Reputation: 3970Reputation: 3970Reputation: 3970Reputation: 3970Reputation: 3970Reputation: 3970Reputation: 3970Reputation: 3970Reputation: 3970
Quote:
Originally Posted by brownie_cookie View Post
Hi all
I'm kinda new to linux and stuff. I'll try to explain what the situation is:
i need to connect to a remote linux server on certain times (automated), but i need to log on to the server. So i want to make a script (shell perhaps) that periodically connects to the server and checks some files and folders. How do i automate it and how can i log on automatically to the remote server without typing everytime the password and login (the script should do it himself)

i hope that you'll understand what i'm saying (my english is not that good).
pls help me ^^
grtz
cookie
Please spell out your words...your English is very good, but text-speak is never easy (or good) to read/post in.

What you want is to do an SSH keyswap, so that the account on your local system can attach to the remote system, without putting in a password. This will keep you from having to put a password in a script (which is unsafe), but still let things be secure. There are many how-tos available on how to do this, but essentially, it's just running (as the user you want to use to automate things):
  • [LOCAL] ssh-keygen -t rsa (take the defaults for everything)
  • [LOCAL] cd $HOME/.ssh
  • [LOCAL] cp id_rsa.pub authorized_keys2
  • [LOCAL] scp authorized_keys2 <user ID>@<remote system IP>: (have to enter a password this time to make the copy work)
  • :: log in to remote system, run first three local steps above. ::
  • [REMOTE] cat ../authorized_keys2 >> authorized_keys2 (you copied the authorized_keys2 file from the other system to the home directory eariler, now you're appending it to the remote-systems authorized users file)
  • :: log out ::

Now, when you use that user ID to SSH again, it shouldn't prompt you for a password, just attach, since you did a key-exchange. You can use "expect" in a script, too, but it's insecure, since the password/ID will be in clear-text in the script.
 
Old 03-24-2011, 09:51 AM   #5
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
wow, thanks for all the fast replies !!

the matter of secure or not, doesn't really matter.
i just need to log on from a pc (or server) to a remote server where i need to log on and when i'm logged on, i need to check files.
I'm doing that with Centreon/Nagios (maybe someone has experience with that?)

Everything you guys suggested as solutions, i will try (tomorrow, because it's almost time to go home ).
And when i have tested it, i will post you back.

But can anyone of you tell me, all the things you say, can i put it in ONE shell script?

Greetings and untill tomorrow !!
 
Old 03-24-2011, 09:56 AM   #6
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Quote:
Originally Posted by brownie_cookie View Post
the matter of secure or not, doesn't really matter.
i just need to log on from a pc (or server) to a remote server where i need to log on and when i'm logged on, i need to check files.
I'm doing that with Centreon/Nagios (maybe someone has experience with that?)
But can anyone of you tell me, all the things you say, can i put it in ONE shell script?
Hello,

You're welcome. You'll find that LQ users are very numerous and there's always more then one member that can help out.

Believe me, if you'll be connecting to remote servers over the internet, you WILL need to take care of security whenever and wherever you can. It's better to prevent then to be sorry. Since you'll be connecting to someone else's server you need to make sure that you're not the cause of their system breaking, for which they in turn could accuse you. It doesn't cost anything, neither money nor time to set up a secure connection for you to use.

Lot's of experience with Nagios here on LQ, just depends on the question how soon you'll get a solution

Almost anything, if not everything, can be put in a script in one way or another to automate things. Just be a bit more detailed of what you need to do, what you've got and what you're missing and we'll take it from there. You will have to put in some time of yourself since ready made solutions hardly ever get offered here at LinuxQuestions.

Kind regards,

Eric
 
2 members found this post helpful.
Old 03-24-2011, 01:58 PM   #7
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by EricTRA View Post
Hello,

You're welcome. You'll find that LQ users are very numerous and there's always more then one member that can help out.

Believe me, if you'll be connecting to remote servers over the internet, you WILL need to take care of security whenever and wherever you can. It's better to prevent then to be sorry. Since you'll be connecting to someone else's server you need to make sure that you're not the cause of their system breaking, for which they in turn could accuse you. It doesn't cost anything, neither money nor time to set up a secure connection for you to use.

Lot's of experience with Nagios here on LQ, just depends on the question how soon you'll get a solution

Almost anything, if not everything, can be put in a script in one way or another to automate things. Just be a bit more detailed of what you need to do, what you've got and what you're missing and we'll take it from there. You will have to put in some time of yourself since ready made solutions hardly ever get offered here at LinuxQuestions.

Kind regards,

Eric
You couldn't have said that any better. +1 to that...
 
Old 03-25-2011, 02:14 AM   #8
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
Quote:
Originally Posted by EricTRA View Post
Hello,

You're welcome. You'll find that LQ users are very numerous and there's always more then one member that can help out.

Believe me, if you'll be connecting to remote servers over the internet, you WILL need to take care of security whenever and wherever you can. It's better to prevent then to be sorry. Since you'll be connecting to someone else's server you need to make sure that you're not the cause of their system breaking, for which they in turn could accuse you. It doesn't cost anything, neither money nor time to set up a secure connection for you to use.

Lot's of experience with Nagios here on LQ, just depends on the question how soon you'll get a solution

Almost anything, if not everything, can be put in a script in one way or another to automate things. Just be a bit more detailed of what you need to do, what you've got and what you're missing and we'll take it from there. You will have to put in some time of yourself since ready made solutions hardly ever get offered here at LinuxQuestions.

Kind regards,

Eric
Thanks ^^ this is actually one of the FEW forums that they are so kind (maybe i have been on the wrong fora, but nevermind)

Oke, security is maybe a nice thing to have, but i'm still on the testing environment, so it is at the moment not the main issue.
What i need to do:
I need to find (or make) a script (preferable in shell) that connects from server A to server B.
When the script wants to connect to server B, it needs to login (without a person that types in the requirements), so it needs to login automatically.
When it succeeded, then the script needs to check folders (like /some/folders/needed/to/be/checked), it has to check how long a file/folder has been there and it needs to check other files that theire is a line with ERROR or WARNING in it.
When that is the result, i (or another person) needs to get an notification (e-mail/sms/...).
And all of this need to be done at certain times, so at e.g. 14h a script needs to be launched (automatically) to check my objectives (but i geuss that can be done with a cronjob).

All of this i need to do with Centreon/Nagios.
So the notification will be done by Centreon, but i need to have some kind of script that check my objectives. Now, i know that theire are plugins for nagios, that control these things (folder age, file checking, ...) but these plugins are mostely for localhost folders/files.
I also know that there are plugins to check REMOTE for folders/files, but they have a problem and that is that they don't have the possibility to log on to my remote server.

I hope you guys understand my problem and i really hope you guys can help me ^^

Thx

brownie_cookie

Last edited by brownie_cookie; 03-25-2011 at 02:18 AM.
 
Old 03-25-2011, 02:28 AM   #9
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

That is indeed important to know that you're in a testing environment, but practice makes perfect I'd advice you to use security even in a testing environment to get familiar with it. Now for your question.

You can write your own script that can do whatever you need/want. Pointers are given in previous posts on how to use keys to authenticate or software to provide a password to your ssh command. You want to use it (your script) as a Nagios plugin so that you can use Nagios to execute the monitoring? Then adapt your script to do just that. No need for a cronjob in that setup since Nagios will take care of both execution and notification if configured correctly.

Now, this is the time where I (and most LQ users too) will ask you to show us what you've got and where it's failing or where you're in doubt. You're the one who will have to put in the work , LQ will provide answers where you're doubting.

Kind regards,

Eric
 
Old 03-25-2011, 02:34 AM   #10
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
Thanks for the fast reply!

I tried yesterday some things with private and public keys, it kinda worked, but i will found my problem.

The main problem is, i don't have a clue how to start with the script.

I thought the first step would be: connecting to the server
step two: trying to find the right folders
step three: controlling the folders/files
step four: give some feedback

i have no experience with shell . But can i search folders (with shell) in a windows-like environment?

I going to test now the connecting to the server with the preview posts.

brownie
 
Old 03-25-2011, 02:51 AM   #11
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

If you need starting points for Bash scripting, have a look at the following guides.
Bash Guide for Beginners
http://tldp.org/LDP/abs/html/

Those will get you started and are a great source of knowledge.

Basically, you need to be aware that your script will depend on you making it 'not needing user input'. As always there are various ways to obtain what you need. For example to execute a command on a remote server through SSH you can use a line like this:
Code:
ssh <user>@<host> '<command to execute on remote server>;<other command on remote server>'
The output will be shown on your terminal, so you can easily redirect it in your script to a variable or a file, whatever you need. The above command works without you providing a password if you've set up key authentication. If you haven't set up key authentication and need to pass the password you could use sshpass to do so. Be aware though that your password will be in plain text in the script.
Code:
sshpass -p 'yourpassword' ssh <user>@<host> '<command to execute on remote server>;<other command on remote server>'
I'd first write your basic script if you don't have experience with Nagios plugins and 'adapt' it to Nagios later on.

First step would be to decide which way you will authenticate. Set that up and test it. Then experiment with the ssh command as pointed to before, running commands on remote servers. If you're familiar with that, start with your script and test every command you put in it on the console to check if it works the way you want it to.

Kind regards,

Eric
 
Old 03-25-2011, 02:55 AM   #12
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
thanks for the links!! i will sure look at them when i the authentication is working.
Now i am trying to let the authentication work, but i have a problem (again )...

I am using these steps of an earlier post:
* [LOCAL] ssh-keygen -t rsa (take the defaults for everything)
* [LOCAL] cd $HOME/.ssh
* [LOCAL] cp id_rsa.pub authorized_keys2
* [LOCAL] scp authorized_keys2 <user ID>@<remote system IP>: (have to enter a password this time to make the copy work)
* :: log in to remote system, run first three local steps above. ::
* [REMOTE] cat ../authorized_keys2 >> authorized_keys2 (you copied the authorized_keys2 file from the other system to the home directory eariler, now you're appending it to the remote-systems authorized users file)
* :: log out ::

Everything is going fine, until i get to the step number 6. when i do this command i get an error like this:
cat: Cannot open ../authorized_keys2: No such file or directory

but the first part of the command (cat ../authorized_keys2) is that of the local server?
 
Old 03-25-2011, 02:57 AM   #13
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
WOOT !!
authorization WORKS xD !!! thanks

Eric, now i will go and test your post

i will keep you updated

greets

brownie

Last edited by brownie_cookie; 03-25-2011 at 03:16 AM.
 
Old 03-25-2011, 03:22 AM   #14
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

Fantastic! Looking forward to your updates.

Kind regards,

Eric
 
Old 03-25-2011, 03:32 AM   #15
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
so if i'm getting it right about the ssh-keyswap.
As long as i keep those files (id_rsa, id_rsa.pub and authorization_keys2) on both sides (local and remote) i can log on perfectly at the remote side?
the name authorization_keys2, can i choose another name, or must it be that name?
 
  


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
Can't SSH to remote machine: Connection closed by remote host Avatar Linux - Networking 34 05-24-2013 09:28 AM
Fedora 10/unable to ssh out from box to remote host (SSH within LAN ok) huskeypm Linux - Networking 3 04-14-2009 07:37 PM
SSH - Remote aplications in remote server Urien Linux - Newbie 11 04-04-2009 04:02 AM
Remote login with SSH, but display on remote computer. brodin Linux - Software 3 09-09-2007 03:01 AM
Remote SSH Kurzweil Linux - Newbie 23 05-14-2005 12:41 AM


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