Scripts triggered via 'expect' - stderr lost


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripts triggered via 'expect' - stderr lost
# 1  
Old 07-26-2017
Display Scripts triggered via 'expect' - stderr lost

I have a bash script on server that runs fine when run interactively and writes stderr output to a file.

However, when invoked through a 'expect' script run on Mac OS my laptop which does ssh to the server : generates the expected file, but file has no content. I suspect the stderr is getting lost somewhere in bash or in 'expect'.

My 'expect' script (run from Mac OS) :

Code:
/usr/bin/expect -f - <<EOD
spawn ssh -t myUserName@theServer
expect "myUserName@theServer:~> " { send "sudo -su adminRole\r" }
expect "myUserName's password:" { send "myUserNamePassword^\r" }
expect "adminRole@theServer:/home/myUserName" { send "the_script.sh\r" }
EOD

the_script.sh (residing on remote SuSE server):
Code:
#!/bin/bash
/usr/bin/java -cp '/some/lib/lib.jar' -Dproperties=some_app.properties com.example.MyApp 2>output.txt

Note :
I only have myUserName account on theServer and have been granted the 'adminRole' to perform specific tasks, one of which is running the MyApp java program & looking at its output (monitoring). This program writes its output to stderr stream instead of stdout, for reasons I do not know , nor can I change it. Also, the lib.jar location is only available as adminRole, so I cannot execute the script as myUserName without doing a sudo.

I have copied my ssh key to server so I can login myself (as myUserName) by password-less login. However, for doing 'sudo' I have to resort to the above 'expect' script.

Now, when I ssh from Mac OS native terminal to theServer, do a 'sudo -su adminRole', and then run the_script.sh , I correctly get the output.txt file with java program's output.
On the other hand, when I run the above 'expect' script from Mac OS terminal , it does login to theServer and create the output.txt file , but the file is empty.

Since I'm really having remote script itself direct output to a file, I suspect this is not a problem with 'expect' itself.

I realize embedding password in script is not good , but for certain reasons, it is acceptable for the time being. I contemplated getting access to resources to myUserName , so that the sudo juggling is avoided, but that is not an option for bureaucratic reasons beyond my control.

Can anyone point what may be wrong in my script ? I've tried to google a lot around 'expect' and how sudo works, but in vain.

Last edited by sdudc; 07-26-2017 at 05:15 PM.. Reason: code tags, please!
# 2  
Old 07-26-2017
Quote:
Originally Posted by sdudc
Can anyone point what may be wrong in my script ?
I hope so. This:

Code:
[...]App 2>output.txt

means the output is locally(!) put into a file "output.txt" in whatever happens to be the current directory. Might it be that the file is not "created" empty, but simply not in the place where you look?

It might be a good idea to put a fixed path in front of the filename to remove any ambiguity:

Code:
[...]App 2>/path/to/output.txt

You might start with "/tmp" and - if you need to keep several versions of this file - qualify the name with a date- and/or time-extension, like this, see man date for appropriate format descriptors:

Code:
[...]App 2>/path/to/output.$(date +'%Y%m%d').txt

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Lost redirecting stderr & stdout to 3 files - one each plus combined

Hi folks I need/want to redirect output (stdout, stderr) from an exec call to separate files. One for stderr only and two(!) different (!) ones for the combined output of stderr and stdout. After some research and testing i got this so far : (( exec ${command} ${command_parameters} 3>&1... (6 Replies)
Discussion started by: MDominok
6 Replies

2. Shell Programming and Scripting

Many scripts [lost noob]

Ok, so I'm a complete scripting noob. My instructor is not very good at giving direction and rushes through things leaving us to the wolves. (Google has been my friend lately). I/We (we means class) have 1 week, to write 5 scripts within a single program. Requirements: Must have a menu to-... (1 Reply)
Discussion started by: nhb1
1 Replies

3. Shell Programming and Scripting

Expect command when triggered by launchd

Hi folks, Im having trouble with an expect command in a shell script I use to back up mac os x servers. This one is being run on a 10.8.2 Server as part of a larger bash script. The script executes perfectly all the way through when triggered on the command line, but when launchd triggers it at... (4 Replies)
Discussion started by: rowie718
4 Replies

4. Shell Programming and Scripting

Using Expect scripts to port files to different unix box

Hello all, I've been trying to design a script which will help me transfer files from one unix box to another. Following is the code: #!/usr/local/bin/expect spawn /usr/bin/scp ms_cp5_daily.ksh userid@cat:/prod/env/ms.txt set pass xxxxxx expect { password: {send "$pass\r";... (4 Replies)
Discussion started by: sethmj
4 Replies

5. Shell Programming and Scripting

Using expect doesn't preserve the stderr

Hello, I am performing the follwing line from a unix server: expect -c 'spawn ssh otherHost chown -R user:group /usr ; expect password: ; send 123456\n ; interact ;' I am getting return value 0, an empty stderr and a stdout that says "/usr/... Not owner, /usr/... Not Owner ..". If I... (2 Replies)
Discussion started by: lastZenMaster
2 Replies

6. UNIX for Dummies Questions & Answers

Difference between using Here document and Expect in scripts

Hi, I am confused between using here document and using expect for writing interactive shell scripts(like changing password ,FTP or doing su). My questions are : 1)Why here documents cant change password from shell script. 2)Why we need to use expect for same? 3) Can Sourcing a script can do... (2 Replies)
Discussion started by: kailash19
2 Replies

7. Shell Programming and Scripting

Need help using expect in shell scripts

hi all, i have this script that accepts passwords automatically and its working in one host only. my problem is how will i use it if i need it to run in more than one host/server let say by using "for loop statement" working : spawn bundle linux -r hostname checkpath... (2 Replies)
Discussion started by: linuxgeek
2 Replies

8. Shell Programming and Scripting

How to handle scripts that expect an input

Hi I would like to know how to handle my script that expects an input when calling the script and the user doesn't enter anything, I need to re-direct to my helpfile. Bascically here is my script: #!/bin/bash csvdir="/var/local/dsx/csv/general" csvfile="$csvdir/$csvfile"... (3 Replies)
Discussion started by: ladyAnne
3 Replies

9. Shell Programming and Scripting

Calling expect scripts from other expect scripts

Hi, First, let me explain the issue I am trying to solve. We have a lot of expect scripts with the duplicated send/expect commands. So, I'd like to be able to extract the duplicated code into the common scripts that can be used by other scripts. Below is my test where I am trying to call... (0 Replies)
Discussion started by: seva
0 Replies
Login or Register to Ask a Question