LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-25-2016, 04:53 AM   #1
Asung
LQ Newbie
 
Registered: Mar 2016
Posts: 2

Rep: Reputation: Disabled
Bash Help


Hi all

Wasn't sure exactly were to post this as I couldn't see a scripting section?
Anyway I have created a bash script which logs me via ssh automatically. I then want it to perform further commands after login, however I cannot get this to work. I am connect to my linux server via a Mac OS X.

Please see script below any help is appreciated

#!/usr/bin/expect -f
spawn ssh root@192.168.0.40
expect "password:"
send "password here"
interact
sleep 2
send "service tor start"
send "service tor status"
end
 
Old 03-25-2016, 06:02 AM   #2
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,194

Rep: Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384
Quote:
Originally Posted by Asung View Post
Hi all

Wasn't sure exactly were to post this as I couldn't see a scripting section?
Anyway I have created a bash script which logs me via ssh automatically. I then want it to perform further commands after login, however I cannot get this to work. I am connect to my linux server via a Mac OS X.

Please see script below any help is appreciated

#!/usr/bin/expect -f
spawn ssh root@192.168.0.40
expect "password:"
send "password here"
interact
sleep 2
send "service tor start"
send "service tor status"
end
Can you tell me why you are using expect?
FWIW, this would not really be a bash question, more of an expect quesxtion or an OpenSSH question.

The normal and supported way to automate ssh logon is to use certificates, rather than expect. Is there some reason you are not using certificates to automate logon?
 
Old 03-25-2016, 12:54 PM   #3
Asung
LQ Newbie
 
Registered: Mar 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by wpeckham View Post
Can you tell me why you are using expect?
FWIW, this would not really be a bash question, more of an expect quesxtion or an OpenSSH question.

The normal and supported way to automate ssh logon is to use certificates, rather than expect. Is there some reason you are not using certificates to automate logon?
Thanks for you reply.
Im not very good at scripting yet.
I copied the script from another forum online and modified it to my liking.
Is there a better way to do it?

The cert is already installed on my machine. The script will connect me to the SSH session with no problem at all.
Its the commands after e.g. service tor start
 
Old 03-26-2016, 07:44 AM   #4
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,194

Rep: Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384Reputation: 1384
Unclear I was...

Forgive me, I did not make myself clear. Your expect script detects and responds to a password prompt. If your personal public cert were installed on the remote host there would be no password prompt.

You could run a command like
Code:
 ssh root@192.168.0.40 uptime
and see the output of the command appear on the next line of your local terminal.

Once that works, you could do something like
Code:
 ssh root@192.168.0.40 -t "command 1;command 2;bash"
to run the two commands and then remain in session on the remote host. Check the man page for ssh, and google for examples. It is easier than you think.
 
  


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
[To share Bash knowledge]Notes for Advanced Bash-Scripting Version 10 (Latest) jcky Programming 4 07-31-2014 10:24 AM
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 03:57 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 09:25 AM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 06:52 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 07:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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