LinuxQuestions.org
Visit Jeremy's Blog.
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-07-2015, 03:29 AM   #1
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Rep: Reputation: Disabled
expect feature to automate login to target host and "sudo su - user"


Hi Guru's
just want to ask if this is possible, I'm trying to automate auto login to target host followed by "sudo su - user" to it but seems like I'm not sure if it is possible. Please advise, thanks

I am using cygwin

script I used = connect.sh

Code:
#!/usr/bin/expect

set timeout 5

set host [lindex $argv 0]

set user [lindex $argv 1]

set password [lindex $argv 2]

set password2 [lindex $argv 3]

spawn ssh -qt "$user\@$host"

expect "pogi_ako@target_host's password:"

send "password\r";

spawn sudo su - user

expect "[sudo] password for user:"

send "password\r";

interact

outcome of my test connect

Code:
$ ./connect.sh target_host pogi_ako password password
spawn ssh -qt pogi_ako@target_host
pogi_ako@target_host's password: spawn sudo su - user
couldn't execute "sudo": no such file or directory
    while executing
"spawn sudo su - user"
    (file "./connect.sh" line 19)

Last edited by apss_evaluator; 10-07-2015 at 03:32 AM.
 
Old 10-07-2015, 05:32 PM   #2
jefro
Moderator
 
Registered: Mar 2008
Posts: 22,052

Rep: Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632
Try making the script with autoexpect. It seems to work better.
 
Old 10-07-2015, 06:38 PM   #3
Ztcoracat
LQ Guru
 
Registered: Dec 2011
Distribution: Slackware, Debian 12 & Devuan
Posts: 9,514
Blog Entries: 15

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Do you have to run that script every time the system is turned on?
 
Old 10-07-2015, 07:21 PM   #4
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Ztcoracat View Post
Do you have to run that script every time the system is turned on?
Hi Ztcoracat,
the script will be executed under alias to the name of the target host
 
Old 10-07-2015, 07:43 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,378

Rep: Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755
I I saw this
Code:
couldn't execute "sudo": no such file or directory
the first thing I'd suspect is that its telling the truth ie the sudo cmd is not in the PATH after it initially logs in.
How about logging in manually and checking
Code:
which sudo

/usr/bin/sudo
then hardcode that path into the expect script ?
 
Old 10-07-2015, 08:02 PM   #6
Ztcoracat
LQ Guru
 
Registered: Dec 2011
Distribution: Slackware, Debian 12 & Devuan
Posts: 9,514
Blog Entries: 15

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by apss_evaluator View Post
Hi Ztcoracat,
the script will be executed under alias to the name of the target host
Got it-
 
Old 10-08-2015, 02:53 AM   #7
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Saw a solution from online and got it working, though I'm really not yet fully aware on the add ons :-)

connect.sh
Code:
#!/usr/bin/expect -f

set host [lindex $argv 0]

set user [lindex $argv 1]

set password [lindex $argv 2]

set user2 [lindex $argv 3]

set password2 [lindex $argv 4]

spawn ssh -qt "$user\@$host"

expect "password:"

send "password\r";

send -- "sudo su - $user2\r"

expect -gl {[sudo] password for pogi_ako:}

send "password\r"

interact


Tried and execute:
Code:
pogi_ako@MY_CYGWIN ~
$ ./connect.sh target_host pogi_ako password user password
spawn ssh -qt pogi_ako@target_host
pogi_ako@target_host's password:
Last login: Thu Oct  8 01:19:07 2015 from 192.168.0.101
[pogi_ako@target_host ~]$ sudo su - user
[sudo] password for pogi_ako:
[user@target_host ~]$
From alias
Code:
alias target_host='echo "connecting on to target_host" ; ./connect.sh target_host pogi_ako password user password'
Test alias from my terminal
Code:
pogi_ako@MY_CYGWIN ~
$ target_host
connecting on to target_host
spawn ssh -qt pogi_ako@target_host
pogi_ako@target_host's password:
Last login: Thu Oct  8 02:42:35 2015 from 192.168.0.101
[pogi_ako@target_host ~]$ sudo su - user
[sudo] password for pogi_ako:
[user@target_host ~]$
 
  


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] "sudo: Can't mkdir /var/run/sudo/%user%: File exists" CNBarnes Linux - Server 7 01-10-2011 04:11 PM
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
"Have to automate the SSH login procedure through shell scripting" sorav Programming 2 11-21-2007 04:53 AM
what is the difference between "configure" host and target? linuxlah Programming 3 09-18-2004 06:47 AM
normal user want to perform "init 6" by using " sudo acbenny Linux - General 3 08-08-2004 07:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:49 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
Open Source Consulting | Domain Registration