LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-03-2021, 11:51 AM   #1
rparavastu
LQ Newbie
 
Registered: Jan 2012
Posts: 7

Rep: Reputation: Disabled
Post ssh to remote server and switch user


Hi all,

I have installed sshpass.

I am using sshpass and ssh to login to remote server.
Once I log into to the remote server I would like to switch to webapp user for which I would like to pass the password from the same command line where I initiated the sshpass on my terminal.

example:

sshpass -f passfile ssh myself@abc.com 'su - webapp'

The above command cnnects to abc.com host and asks for password and when I type the password is visible what I type, and fails to switch to webapp user.

Can someone help?

Thanks,
Ram.
 
Old 02-03-2021, 12:08 PM   #2
Hammett
Senior Member
 
Registered: Aug 2003
Location: Barcelona, Catalunya
Distribution: Gentoo
Posts: 1,074

Rep: Reputation: 59
I do not think you can do that, as the passfile you are using for sshpass is for the user who connects to the remote machine, and not the webapp user.

Can't you just ssh with webapp user directly?
 
Old 02-03-2021, 12:11 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
You could maybe do that with the TCL-derivative, Expect. However, first, it would be a very good idea to set up SSH-key or SSH-certificate based authentication and turn off password authentication. Then you can do the rest with Expect.
 
Old 02-03-2021, 12:19 PM   #4
rparavastu
LQ Newbie
 
Registered: Jan 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Hammett View Post
I do not think you can do that, as the passfile you are using for sshpass is for the user who connects to the remote machine, and not the webapp user.

Can't you just ssh with webapp user directly?
No, due to security reasons, we are supposed to log in as ourselves and then switch to the webapp user. That is the policy. I am trying for a one line code using sshpass ssh and or any other commands that connects me to the server and then switch me to webapp.

Thanks,
Ram.
 
Old 02-03-2021, 12:20 PM   #5
rparavastu
LQ Newbie
 
Registered: Jan 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
You could maybe do that with the TCL-derivative, Expect. However, first, it would be a very good idea to set up SSH-key or SSH-certificate based authentication and turn off password authentication. Then you can do the rest with Expect.
Based on my example can you help me out with the code how to use the expect.

Thanks,
Ram.
 
Old 02-03-2021, 12:25 PM   #6
rparavastu
LQ Newbie
 
Registered: Jan 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
You could maybe do that with the TCL-derivative, Expect. However, first, it would be a very good idea to set up SSH-key or SSH-certificate based authentication and turn off password authentication. Then you can do the rest with Expect.
I have setup a password less authentication. and I can log into the remote server just by typing ssh myself@abc.com.
The difficulty is to switch to webapp where I have to pass the password. Any help is appreciated.

Thanks,
Ram.
 
Old 02-03-2021, 12:27 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by rparavastu View Post
No, due to security reasons, we are supposed to log in as ourselves and then switch to the webapp user. That is the policy. I am trying for a one line code using sshpass ssh and or any other commands that connects me to the server and then switch me to webapp.
There is no 'one line' to do this. If this is for security purposes, why on earth are you trying to script a login?? Anyone who gets on your machine can use the stored credentials and log into your web server. What is the command you're trying to run, or what are you trying to accomplish on the webapp server with this command?
Quote:
Originally Posted by rparavastu
Based on my example can you help me out with the code how to use the expect.
No, sorry...we aren't going to write your scripts/code for you. We are happy to HELP you, so if you post your work and show your efforts and tell us where you're stuck, we can assist. Otherwise, you can find THOUSANDS of tutorials for expect and scripting with a brief Internet search.
 
Old 02-03-2021, 02:32 PM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by rparavastu View Post
I have setup a password less authentication. and I can log into the remote server just by typing ssh myself@abc.com. The difficulty is to switch to webapp where I have to pass the password. Any help is appreciated.
Right; again, you can use expect to do this in a simple script. But you still haven't said what it is you want to accomplish by doing this. Because if you have to run command(s) as that webapp user, you have to type THOSE in as well, so what's the big deal about typing in a password?
 
Old 02-03-2021, 04:01 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Yes, there is a one liner.

ssh -t user@server 'su - webapp'

ssh does not allocate a pseudo-tty by default and not having an interactive tty prevents you from entering the password correctly. The -t option forces a tty

Last edited by michaelk; 02-03-2021 at 09:13 PM.
 
Old 02-03-2021, 07:20 PM   #10
rparavastu
LQ Newbie
 
Registered: Jan 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
Right; again, you can use expect to do this in a simple script. But you still haven't said what it is you want to accomplish by doing this. Because if you have to run command(s) as that webapp user, you have to type THOSE in as well, so what's the big deal about typing in a password?
Point well taken. I agree with you, if I were running one or two commands, I would not worry about it. I have multiple scripts that I need to deploy every other day, some are repetitive tasks, I would rather have a single line login script and create an alias for it, and deploy the scripts remotely when connected as webapp on the server. As of now I am connecting manually, and deploying my scripts. I am trying to minimize work, if that makes sense. Thank You very much for suggesting the usage of expect. I am in the process of exploring the usage of expect, however, due to other production issues coming up, I am giving less priority for now, in the mean time if I can get some directions (definitely not code) I will work on it time permitting. Hope I am clear now, as to what I am trying to accomplish.

Thanks,
Ram.
 
Old 02-03-2021, 09:11 PM   #11
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
If this is truly routine and repetitive have you considered using a cron job for the webapp user? Also if possible you should probably be using key authentication over the nastiness that is sshpass.
 
Old 02-03-2021, 10:50 PM   #12
rparavastu
LQ Newbie
 
Registered: Jan 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank You everyone for the suggestions and help. We have chosen to go with another plan, to use CA Automation tool AUTOMIC (UC4) which gives us better control of deploying the scripts based on the completion of a particular process, using workflows.

Thanks,
Ram.
 
  


Reply

Tags
linux command



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
LXer: What Is SSH Command And How To Use SSH To Connect To Remote Server LXer Syndicated Linux News 0 07-24-2018 02:52 PM
[SOLVED] using switch in a function to call another function with a switch but it is not going into 2 switch ... BW-userx Programming 2 08-20-2017 11:47 PM
Work on a remote project with Eclipse via SSH and SVN+SSH (2 remote Linux) alwaystudent Linux - Software 5 08-12-2015 11:40 AM
Help with Ubuntu server remote ssh and local network ssh issues using putty. scottpops Linux - Server 8 05-17-2012 05:07 PM
SSH - Remote aplications in remote server Urien Linux - Newbie 11 04-04-2009 04:02 AM

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

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