SSH inside perl script hangs - linux

I am trying to execute the following command using perl script :
my $sum = `$sudo $ssh $server netstat -Aan | /bin/grep 1158 | /bin/egrep '#IP[0]' | wc -l`;
Where $sudo = full path to sudo command , same for $ssh , $server = the file server that I want to pull the data from , and #Ip[0] = ip address.
When I run the exact command from the shell (not as a script) I get the required result (which is an integer number). However , when I run the script it just gets hangs when it reaches to that point.
I cant see anything wrong with the syntax , I am using back ticks to save the returned output , could you please assist?

Related

bash + how to avoid specific messages in the log file

when I run the bash script on my Linux machine we get the following errors in my log ,
note - we set in the script:
exec > $log 2>&1 , ( in order to send all standard error/output to $log )
the errors messages:
tput: No value for $TERM and no -T specified
in order to filter this errors messages we try to set in the bash script that:
export TERM=xterm
but without help
after digging we found that happened in some case for example when we perform ssh to remote machine and runs commands on remote machine VIA ssh
in order to avoid that we set TERM=xterm in the bash script as the following:
ssh user#$remote_machine "TERM=xterm /tmp/script.sh"
but it’s very not elegant solution and because my script use a lot of ssh and curl command then it’s not practical solution to set this on each SSH or curl etc
so my big question is
how to filter the message – tput: No value for $TERM and no -T specified ?
so we not get these ugly message in my $log file
Filter out all tput: No value from logging:
exec > >(grep -v 'tput: No value for $TERM and no -T specified' >$log) 2>&1

Collecting system data from Red Hat Linux

I am planning to write a small program .The input for this should be the IP,username,password for a Linux machine ,and it should give me the system details of that machine as output.
I am planning to write this using Shell ,using RSH for the login . I am in no way asking for a solution ,but could you please point me towards other options that I have ? I am not really comfortable using Shell scripts .
Thanks in advance
i have a same demand. and what i do is:
first write a script which will be executed at target host (T). something like this
> cat check_server.sh
#!/usr/bin/env bash
# execute at target host
all_cmd=(
"uname -a"
"lscpu"
"free -m"
)
function _check {
for one_cmd in "${all_cmd[#]}"; do
echo -e "\n\n$one_cmd" >> /tmp/server_info.txt
eval "$one_cmd" >> /tmp/server_info.txt
done
}
then execute it in target and copy back result, like this
_cmd=`base64 -w0 check_server`
ssh $user#$ip "echo $_cmd | base64 -d | bash"
scp $user#$ip:/tmp/server_info.txt ./

running bash piped comand from mono linux

I am trying to run bash command with pipe using mono process start
here is code snippet I am trying
single commands works, however pipe commands fails to run, what am I missing here ?
ProcessStartInfo oInfo = new ProcessStartInfo(command, args);
oInfo.UseShellExecute = false;
oInfo.CreateNoWindow = true;
oInfo.RedirectStandardOutput = true;
oInfo.RedirectStandardError = true;
StreamReader srOutput = null;
StreamReader srError = null;
Process proc = System.Diagnostics.Process.Start(oInfo);
proc.WaitForExit();
I tried running "ps -aux" which runs fine. However ps -aux | grep gnome command failed.
I tried these scenarios
scenario 1:
command = ps
argument = -aux | grep gnome
scenario 2:
command = ps
argument = "-c ' -aux | grep gnome ' "
scenario 3 :
command = ps
argument = " -c \" -aux | grep gnome \" "
all these failed with
error: garbage option
Usage:
ps [options]
Try 'ps --help '
or 'ps --help '
for additional help text.
Also on side question, the reason I am trying to do this is to figure out of a particular daemon is already running. Is there a standard way to get this info.
for instance in windows we can query running services using ServiceController.GetServices().
Is something similar available on mono/Linux directly ?
When you add "|" to a bash line the bash interpreter splits the command in two processes and feeds the output from one to the other, when you call that command using Process it sends the argument as is.
The most close you can achieve is to start yourself the two process and feed one with the output of the other through the input/output streams.
About part 2 of your question, if the program runs as privileged then Process.GetProcesses will list all running processes on the system included daemons.

Executing bash commands automatically under sudo is not working

I need a script to execute sudo commands without requesting a password.
In the following scripts I have a strange bash stdin behaviour:
username#username-laptop:~$ sudo -Si | echo "password"
password
root#username-laptop:~# exit
username#username-laptop:~$ sudo -Si | echo "password"
password
[sudo] password for rootname:
(password is a root password on my machine)
Why the password has not been requested in the first time, and has been in the second (and always after that)? Also I don't understand how stdin works in bash, where is an error in my code?
I know this is a very bad idea to put a root password to the command directly, but I need it to execute correctly.
Maybe you shoud try:
echo "Password" | sudo -Si
instead of:
sudo -Si | echo "Password"
The output of the first command (before the "|") is used as input to the second command.
You should be aware that the first command cannot do any screen output while the second command cannot receive any keyboard input unless you do some programming tricks...

Invocation command using SSH getting failed?

As per project requirement, i need to check the content of zip file generated which been generated on remote machine.This entire activity is done using automation framework suites. which has been written in shell scripts. I am performing above activity using ssh command abd execute unzip command with -l and -q switches. But this command is getting failed. and shows below error messages.
[SOMEUSER#MACHINE IP Function]$ ./TESTS.sh
ssh SOMEUSER#MACHINE IP unzip -l -q SOME_PATH/20130409060734*.zip | grep -i XML |wc -l
unzip: cannot find or open SOME_PATH/20130409060734*.zip, SOME_PATH/20130409060734*.zip.zip or SOME_PATH/20130409060734*.zip.ZIP.
No zipfiles found.
0
the same command i had written manually but that works properly. I really have no idea.Why this is getting failed whenever i executed via shell scripts.
[SOMEUSER#MACHINE IP Function]$ ssh SOMEUSER#MACHINE IP unzip -l -q SOME_PATH/20130409060734*.zip | grep -i XML |wc -l
2
Kindly help me to resolve that issue.
Thanks in Advance,
Priyank Shah
when you run the command from your local machine, the asterisk character is being expanded on your local machine before it is passed on to your remote ssh command. So your command is expecting to find SOME_PATH/20130409060734*.zip files on your machine and insert them into your ssh command to be passed to the other machine, whereas you (I'm assuming) mean, SOME_PATH/20130409060734*.zip files on the remote machine.
for that, precede the * character by a backslash ( \ ) and see if it helps you. In some shells escape character might be defined differently and if yours is one of them you need to find the escape character and use that one instead. Also, use quotes around the commands being passed to other server. Your command line should look something like this in my opinion:
ssh SOMEUSER#MACHINE_IP "/usr/bin/unzip -l -q SOME_PATH/20130409060734\*.zip | grep -i XML |wc -l"
Hope this helps

Resources