0

I am very new to perl. I am trying to create a perl script which will execute multiple unix command to create VNC session in some unix server.

Here is my script -

#!/usr/bin/perl -w
use Carp;
use strict;
use warnings;

# here get the parameters idsid
my $IdsId=$ARGV[0];


#excecute the commands here

my $user=`su -l $IdsId`;
my @finalresult=`vncserver -randr      1024x768,800x600,1024x768,1152x824,1280x1024,1280x800,1440x900,1400x1050,1600x1200,1920x1200`;

print "@finalresult";

But when I am executing this script its not working.

Please some body help me.

6
  • 2
    "Its not working" is a sadly inadequate problem description. It only begs the follow-up question "What's not working?", and you really should skip that redundant little byplay and just tell us right away. Commented Jun 11, 2013 at 9:43
  • where are you using my $user=`su -l $IdsId`;? Commented Jun 11, 2013 at 9:43
  • In this line I am trying to login as some other user. $IdsId is my parameter. Commented Jun 11, 2013 at 9:50
  • @Gulrej We can see what your code says, no need to repeat the obvious. What I wanted was things like error messages, and an actual description of what was "not working". Even if you only say "I get no output", that is more information than "its not working". Most likely, I would say that the program hangs and you have to kill the process. Commented Jun 11, 2013 at 9:54
  • It is hanging my session. There is no error message showing. One more thing I am a .net developer, so know only little about perl and Unix. Commented Jun 11, 2013 at 10:36

2 Answers 2

1

I would expect to see you executing:

exec "su", "-l", "$IdsID", "-c", "vncserver -randr ...";

which makes the Perl script largely irrelevant since you could write it in shell as:

exec su -l "${1:-$USER}" -c "vncserver -randr ..."

It might be better to use sudo rather than su. The difference is that with su, you have to know the other user's password; with sudo, you only have to know your own password (and the system administrator must have given you permission to use sudo for the task on hand).

Sign up to request clarification or add additional context in comments.

3 Comments

Hi I have the root access. So it wont ask the password, and I have tried with sudo command also. But the problem is my first command is running but the second command also trying to run as the same user. Its not able to switch the user.
What's your second command? There's not much point in executing the su on its own; it launches an interactive shell, at which you'd nominally type commands, but because it is inside back-quotes, its input comes from /dev/null, which means it executes without doing anything. I think... What is in the $IdsId variable? If it is an ID, at least one of us is confused (possibly both). If it is more than just an ID, then...well, it would help to have the details about what its value is.
$IdsId is a user id only. There are two command, 1st command is trying to sudo as an another user. second command is trying to create a VNC session for that user. Please help me. I am stucked.
0

When you use backticks, a sub shell is created and the main program is halted. When the process in that sub shell is finished, the control returns to the main program. So in this case, I imagine that su never exits, and that vncserver does not run with the user you intend (nor does it exit to return control to the perl script) -- because it is executed in another sub shell where su never happened.

What you probably need is to do these commands in the same line:

my @result = qx(su -l $IdsId; vncserver -randr ....);

Although whether this works or not you'll have to find out for yourself.

1 Comment

This also not working, my VNC session is getting hanged while executing the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.