LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 03: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, 05:02 AM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,622

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
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, 11:54 AM   #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, 06:44 AM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,622

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
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



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 09:24 AM
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02: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 08:25 AM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

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

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