Foreground sesu SSH with here document


 
Thread Tools Search this Thread
Top Forums Programming Foreground sesu SSH with here document
# 1  
Old 04-27-2012
Foreground sesu SSH with here document

I'm trying to write a foreground (password prompt) ssh command, passing in a here doc - any idea where I'm going wrong?

Code:
ssh <user>@<targetserver> /usr/seos/bin/sesu - <targuser> -c /usr/bin/ksh "param1" << EOF
date
echo ${1}
EOF

Tells me "Invalid option param1". However a simple date call with no params work (always prompts for password btw)

Code:
ssh <user>@<targetserver> /usr/seos/bin/sesu - <targuser> -c "/usr/bin/ksh" << EOF
date
EOF

Looking for the correct method to do this in the foreground with password login (I previously posted about the method in the background with passwordless ssh setup) at: Background SSH using here document
# 2  
Old 04-27-2012
ssh, and most other sane login systems, are designed to never, ever read a password from a script. This is a security feature, since retrievably-stored passwords are almost impossible to keep safe. "interactive password authentication" means "password typed by a human being in realtime authentication" and nothing else is supposed to do. You'd need to install third-party brute forcing utilities like expect to even try, and that'd be much harder than the proper way, i.e. using some noninteractive form of authentication.

The proper way to do automatic ssh logins is with keys.

ssh is able to prompt for a password even when stdin is redirected because it can simply open /dev/tty direct, whenever a terminal is available; and if one isn't available, it has no business accepting a password...
# 3  
Old 04-27-2012
But that's my point - this is in the foreground - it is interactive. I want it to prompt the user for a password and for them to enter it. This is a backup method in case SSH DSA keys are corrupted or not in place for some reason.
# 4  
Old 04-27-2012
You're just having problem to pass parameters to shell. If you give -s then the shell expects script from stdin, and will take your arguments:

Code:
[mute@geek ~]$ ssh mute@localhost "/bin/sh -s -- 'Hello World' 'this is mute'" <<'EOF'
printf 'Arg: %s\n' "$@"
EOF

mute@localhost's password:
Arg: Hello World
Arg: this is mute

This User Gave Thanks to neutronscott For This Post:
# 5  
Old 04-27-2012
Quote:
Originally Posted by doonan_79
But that's my point - this is in the foreground - it is interactive. I want it to prompt the user for a password and for them to enter it. This is a backup method in case SSH DSA keys are corrupted or not in place for some reason.
Ahh, I see. Pardon me.

A here-document won't stop it from being able to read a password from the user. ssh just opens /dev/tty to do that.
# 6  
Old 04-27-2012
I'm running a Korn shell (ksh) and I think the "-s" switch gave me issues.... are those two dashes required? i.e. the "--" after "-s"? Thanks!

---------- Post updated at 18:18 ---------- Previous update was at 18:17 ----------

Quote:
Originally Posted by Corona688
Ahh, I see. Pardon me.

A here-document won't stop it from being able to read a password from the user. ssh just opens /dev/tty to do that.
Nay bother. the interactive password check is working AOK. It's just the process of passing through params into a new shell with commands within the here document that I'm struggling a little with.
# 7  
Old 04-27-2012
I'll explain what the various parts do better. -s tells it to read from standard input even when arguments are given, so it assumes arguments are arguments ($1 $2 $3) instead of files.

Try this in your shell:
Code:
$ sh -s -- arg1 arg2 arg3
# you are now in a new shell
$ echo $1
arg1
$ echo $2
arg2
$ echo $3
arg3
$ exit
$

So the arg1, arg2, arg3 force the $1, $2, $3 variables into what you want.

So when you do the same thing over ssh:

Code:
ssh username@host exec /bin/sh -s -- arg1 arg2 arg3 <<'EOF'
echo "$1"
echo "$2"
echo "$3"
EOF

...Since it's <<"EOF" and not <<EOF, the contents of the here-document are sent totally raw and uninterpreted. You get no substitution. The only things you get are the ones you pass in where arg1, arg2, arg3 are. These substitute as usual, before the command is run.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automatic logon via PUTTY and sesu -

Hi, I need to write a script to automate the logon via putty with Sesu-. can someone help me? thanks in advance (1 Reply)
Discussion started by: austrold
1 Replies

2. Shell Programming and Scripting

ssh uses here-document problem

I try to ssh uses bash script in here-document like this. ssh root@$SERVER <<EOF if ; then service httpd start fi exit EOF But got an error below. maybe the if command causes the problem. Thanks for help. (2 Replies)
Discussion started by: muffle
2 Replies

3. Programming

Background SSH using here document

I'm trying to use Here documents to pass several commands to a remote server over SSH. However I'm having problems. The code is as follows: nohup ssh $i_hostname exec /usr/bin/ksh -s << EOF >> $AS_STOPSAP_LOG & echo $i_instname; ps -ef | grep name | grep ${i_instname} |grep -v grep ... (4 Replies)
Discussion started by: doonan_79
4 Replies

4. Shell Programming and Scripting

Here document inside a here document?

Can we use a here document inside a here document? Something like this ssh user@remotehost << REMOTE sudo vserver vsernamename enter << VSERVER perform actions on vserver. VSERVER REMOTE (6 Replies)
Discussion started by: mnanavati
6 Replies

5. UNIX for Advanced & Expert Users

Bringing a nohup process to foreground

Hi, I have used nohup command to run a process in background. i ran successfully and i exit from that terminal. But when i login for the next time I am not able to see the process but it is working, since I am getting a e-mail alert for error issue. Please let me know how to bring that process... (3 Replies)
Discussion started by: mbguy
3 Replies

6. UNIX for Dummies Questions & Answers

bringing a process to the foreground

If i have a single file i would just press fg but if i have multiple files running in the backgound and want to bring a specific one to the foreground how would i do that? Thanks!! (1 Reply)
Discussion started by: JamieMurry
1 Replies

7. Shell Programming and Scripting

Background and Foreground of a process within a script

I'm not sure if it is even possible but I figured if it was someone here would know how to do it... I am running a script which starts a bunch of processes in the background but there is one process I would like to bring back to the foreground when complete. Unfortunately the process that I... (2 Replies)
Discussion started by: ctruhn
2 Replies

8. Shell Programming and Scripting

ssh and here document

Hi :) how can I use here doc to use ssh? I am facing a problem with the below script: #!/bin/bash ssh hosein@localhost << * 123456 * "123456" is my password Thanks (2 Replies)
Discussion started by: htabesh
2 Replies

9. Shell Programming and Scripting

how to get background job to foreground

hi, i am just wondering that wen we give the following code we make a process run in background...can the viceversa be performed?i.e can this be made foreground again # sleep 75& 21751 # (4 Replies)
Discussion started by: sandilya
4 Replies

10. Shell Programming and Scripting

Execute SAS Foreground

Hi, I am executing the SAS program using Unix script to call SAS program, but i would like to run that SAS program in forground mode, does anyone know what is command. Thanks in advance!! (0 Replies)
Discussion started by: terala
0 Replies
Login or Register to Ask a Question