LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 02-12-2009, 12:57 PM   #1
keber314
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Rep: Reputation: 0
How to write a script with auto login ssh and then run commands?


Hi,

I am trying to write a script to login to a server and then issues two other commands and then take those contents and send it out in an email everyday. I have been able to get the SSH to work using a public private keygen and that works fine. What I am having trouble with is after the login using SSH takes place, no other commands in the script execute until the SSH session has closed. I want the two echo commands to happened while I am still connected via SSH. There is a prompt that appears after SSH occurs.

ssh username:7000@servername

echo 'i'

echo 'show fail'


//then send contents via an email on a daily basis


Thanks for your help,

keber314
 
Old 02-12-2009, 01:43 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

Try:
Code:
ssh username:7000@servername 'echo "i"; echo "show fail"' | mailx -s "Subject" you@youremailaddress.com
Dave

Last edited by ilikejam; 02-12-2009 at 01:45 PM.
 
Old 02-12-2009, 01:44 PM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Double post

Last edited by ilikejam; 02-12-2009 at 01:45 PM.
 
Old 02-12-2009, 01:53 PM   #4
keber314
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
I tried the above code and I get an email but nothing is contained within the email. Also after I initiate the script I receive the message: Connection to "server" closed by remote host. So I am guessing that the ssh is working but the two echo commands are not working. But the email is working. Any suggestions?

Thanks,

keber314
 
Old 02-12-2009, 01:59 PM   #5
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
"Connection to "server" closed by remote host" means the remote side shut down the connection.

Does 'ssh username:7000@servername' work?

Dave
 
Old 02-12-2009, 02:07 PM   #6
keber314
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Yes it does work by itself. That is what is confusing. Now if I put:
Code:
 ssh user:7000@servername
          echo 'i'
          echo 'show fail'
Then I get ssh to work just fine. But the echo commands do not execute until after I close the ssh connection myself. I heard that I might possibly want to fork off the ssh and send commands to it that way? But I do not know how to do that.
 
Old 02-12-2009, 02:13 PM   #7
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Well this is all very odd.

What about:
ssh username:7000@servername 'echo "i"; echo "show fail"'

?
 
Old 02-12-2009, 02:39 PM   #8
keber314
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
It is doing the same thing. But the email i receive is blank and contains no information. So I am not sure if it is transferring the output from running those commands into the email or not?
 
Old 02-14-2009, 06:53 AM   #9
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
That command shouldn't have emailed anything.

If you do:

ssh user@remotehost "hostname"

which hostname do you get back?
 
Old 02-14-2009, 07:16 AM   #10
sleddog
Member
 
Registered: Jan 2002
Location: Labrador, Canada
Distribution: CentOS, Debian
Posts: 182

Rep: Reputation: 35
For me,

Code:
[user@host1] ssh user@host2 'echo "i"'
echoes locally -- on host1.

On the other hand,

Code:
[user@host1] ssh user@host2 'echo "i" > /tmp/echo-test'
Writes output to the file /tmp/echo-test on host2 as expected.

Essentially I think that when you run a remote command that produces screen output, the output is shown on the local display.

So you could try redirecting the output from your remote commands to a file, then mail that file.

Last edited by sleddog; 02-14-2009 at 07:17 AM.
 
Old 02-14-2009, 08:05 AM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,001
Blog Entries: 54

Rep: Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756Reputation: 2756
I don't know any common commands called "i" or "show". Are these shell aliases? UI applications? Actual binaries outside your accounts $PATH? What's the deal?
 
Old 02-16-2009, 08:33 AM   #12
keber314
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
unSpawn,

These commands are being using on an HP HSG 80. It is not a normal shell. Normal linux commands do not work within it. I am trying to login to this server remotely and run an error check, "show fail" and then I want to send the output from that command in an email.

Hope this helps you understanding.

Thanks,

Keber314
 
Old 02-16-2009, 08:37 AM   #13
keber314
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
ilikejam,

I forgot to say that I have code that tells it to email. Sorry about that. This is my code I have right now and it is still sending a blank email:
Code:
ssh username:7017@server 'echo "i"; echo "show fail" > /tmp/echo-test' | mailx -s "Subject" user@yahoo.com
Also I do not think it is creating the "echo-test" file because I can not find it anywhere in my "tmp" directory. I am still looking though.

Thanks for your help,

keber314
 
Old 02-16-2009, 03:37 PM   #14
keber314
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
I figured out the answer to my question after working with a friend. I used the following code:

Code:
( sleep 30; echo -ne 'i\r'; sleep 30; echo -ne '\r'; sleep 30; echo -ne 'show fail\r'; sleep 30; echo '~.' ) | ssh -tt user:7000@server > showfail.out
I needed the sleep 30's to allow for ample time to communicate via the remote connection. I then was able to take the contents and send it in an email.

Thanks for your help,

keber314
 
  


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
Auto SSH-login expect script creatorrr Linux - Software 7 10-26-2011 03:09 AM
auto-login script for connecting to remote m/n by using telnet/ftp/ssh Uday123 Programming 5 10-12-2011 11:31 AM
ssh auto login script zerocool22 Linux - Server 4 05-13-2008 09:43 PM
After login attempts, can a script be run from ssh daemon ? frenchn00b Linux - Software 2 01-02-2008 11:41 PM
Write a script to run several commands ??? wahaha Linux - Software 1 04-13-2007 08:20 PM


All times are GMT -5. The time now is 09:59 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration