execution of system command from perl script using su - perl

I have a perl script as below:
test.pl
#!/usr/bin/perl
system("ls -lart");
this works fine but when i run it as below it is failing:
su guest test.pl
it is failing at 2nd line as below:
0403-057 Syntax error at line 2 : `(' is not expected.

From man 1 su:
OPTIONS
The options which apply to the su command are:
-c, --command COMMAND
Specify a command that will be invoked by the shell using its -c.
The executed command will have no controlling terminal. This option cannot be used to execute interractive programs which need a controlling TTY.
So you should use
su -c "perl /path/to/test.pl --maybe some.options" guest

You have to execute it using -c option for su. The problem is that if you launch it like that, it's supposed to be a shell script. So you have to tell su to actually load the shell and then execute the command.
Most likely
su guest -c test.pl
will work (provided it's in the command path of the user, of course).

Related

Powershell - Run SCP command using CYGWIN but also expand variables

I need to expand variables before running the SCP command as a result I can't use single quote. If I run the script using double quotes in Powershell ISE it works fine.
But doesn't work if I run the script through command prompt.
I'm using zabbix to run the script which calls the script as [cmd /C "powershell -NoProfile -ExecutionPolicy Bypass -File .\myscript.ps1"]
Here is the code that needs to run SCP using Cygwin bash.
if ((test-path "$zipFile"))
{
C:\cygwin\bin\bash.exe -l "set -x; scp /cygdrive/e/logs/$foldername/dir1/$foldername.zip root#10.10.10.10:~/"
}
Output:
/usr/bin/bash: set -x; /cygdrive/e/logs/myfolder/dir1/server.zip root#10.10.10.10:~/: No such file or directory
If I run the same command above in Cygwin manually it works.
I even tried to use bash -l -c but then the SSH session is stuck maybe because the root#10.10.10.10 becomes $1 according to the documentation.
Documentation link
-c If the -c option is present, then commands are read from
the first non-option argument command_string. If there are
arguments after the command_string, the first argument is
assigned to $0 and any remaining arguments are assigned to
the positional parameters. The assignment to $0 sets the
name of the shell, which is used in warning and error
messages.
Figured it out. It was halting when using bash -c was due to StrictHostKeyChecking, the known hosts thing (where you get a prompt to type yes/no). I set the -v switch to SCP and it showed me the Debug logs where it was halting.
Had to set scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null options.
The complete line now looks like the following:
c:\$cygwin_folder\bin\bash.exe -c ("/usr/bin/scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -v -i /cygdrive/c/cygwin/home/myuser/.ssh/id_rsa /cygdrive/e/logs/$foldername/dir1/$foldername.zip root#10.10.10.10:~/")

How to log the output along with error messages to a file while running a script on psql command line on Freebsd OS?

On RHEL, the below command works:
psql -h hostname -U username -p port_no -d database -f /tmp/myfile.sql &> logfile01.txt
On FreeBSD, this throws error:
"Invalid null command"
Please suggest.
If you use this only on the command line then there is no need to change the shell.
To redirect stdout and stderr to a file in C-Shell synthax simply use ">& filename".
Different story is, if you want to write shell scripts. Bourne Shell and it's clones (like i.e. Bash) are better suited for writing script. See this Unix FAQ "Csh Programming Considered Harmful": http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
This redirection works in bash
&> logfile01.txt
, but it does not work in csh which is the default shell in FreeBSD.
# set | grep shell
shell /bin/csh
# ls -la &> logfile01.txt
Invalid null command.
Bash is not installed by default. You can install it
pkg install bash
and configure it as the default shell.

Sysinternal Close Handle working in Command Prompt But Not Powershell

I have a strange issue. I am trying to close down a handle using Powershell using this 1 liner:
(&"D:\handle.exe" -p "–c C –p 3348 -y")
I am getting the following response:
No matching handles found.
When I run the exact same command in Command Prompt
handle.exe -c C -p 3348 -y
I get:
Handle closed.
I am running Powershell and Command Prompt as Admin.
edit: Note: I can run the same command inside the Powershell Command Window and get the same expected result as I did from the normal Windows Command Prompt.
You don't need any fancy syntax. PowerShell can run command-line programs just like cmd.exe can. Just type the command you want and press Enter.
handle -c C -p 3348 -y
It is likely you need to run this from an elevated PowerShell window, but that's not different from cmd.exe.

execute buffer content in current shell upon exec ssh

My perl code does this
#!/usr/bin/perl
sleep 2;
exec 'ssh', '-o', "ConnectTimeout=10", "newhost", "sleep 3;pwd";
the problem is when sleep is executing what ever I type on the terminal (during execution)
disappears to non interactive shell in newhost.
eg:
user#a02$perl test.pl
ls
user#a02$ #ls is not executed
command ls executes if I don't use exec or system.
Is there a way to execute the contents of the buffer?
After a bit of digging i found ssh has has an stdio redirector when used with -n option.
http://www.pixelbeat.org/programming/stdio_buffering/
To tell ssh that the remote command doesn't require any input use the -n option

perl run two system commands error

So in my script I need to make to calls to unix, and I do it via the system command like so:
system "bash -i -c 'addmothernode'";
...
perl code ...
...
system "bash -i -c 'addnode -ip=$_'";
However, whenever I run both of these commands in the same script, for some reason my process is stopped like this:
[1]+ Stopped perl boot.pl
And the script can only be finished when I run fg %1. When I only have one of these system calls in, the perl script finishes successfully. But I need both commands because they depend on each other. Anyone have any ideas about what's going on? Thanks!
UPDATE:
A lot of answers below are saying I don't need to use bash -i to run a system command, and I know typically this is true but I need to use aliases that I have created and if I do not use this the aliases won't be recognized. So I do need bash -i.
This problem is unrelated to perl. You can easily reproduce the situation if you start two bashes in the interactive mode (-i) one after another:
$ cat 1.sh
bash -i -c 'sleep 1'
bash -i -c 'sleep 1'
$ bash 1.sh
[1]+ Stopped bash 1.sh
Of course it would be better to run bash in the non-interactive mode (without -i) or run the program directly, without bash, but if you need for some reason bash -i you can protect its run with setsid:
$ cat 1.sh
setsid bash -i -c 'sleep 1'
setsid bash -i -c 'sleep 1'
echo done
$ bash 1.sh
done
The bash -i means run an interactive shell; so you have two shells both reading from the terminal.
Try removing the -i options.
system "addmothernode";
should work.
To execute a command, bash is not needed. The Perl system function is like the system C function, it calls by default sh.
man system
exec
The standard to which the caller conforms determines which shell is used. See standards(5).
Standard Shell Used
______________________________________________________________
1989 ANSI C, 1990 ISO C, 1999 ISO C, /usr/xpg4/bin/sh
POSIX.1 (1990-2001), SUS, SUSv2, SUSv3,
XPG4
POSIX.1 (1988), SVID3, XPG3, no standard /usr/bin/sh
specified