Putty executing a text tile in a linux terminal - linux

I have a one liner that I run from the run box in the start menu:
"C:\Program Files (x86)\casper\PuTTY\putty.exe" -ssh "192.168.1.2" -l casper -pw "<password>" -m \\PROD.MSAD.casp.NET\UserData\CASPER\Home\Documents\pbauth_list.txt -t
It sssh to a linux box, and opens up a terminal and echos these statements intoa terminal. when someone needs elevated access at the linux terminal i just type in user=user than cut and paste the line into the command line - and viola that user has access to a super user account for what ever time period i designate. It works awesome.
The
\\PROD.MSAD.casp.NET\UserData\CASPER\Home\Documents\pbauth_list.txt text
file looks like this
echo "pbrun pbauthcl grant PBAUTH_P_A4_PROD \$user 2 \"\$user needs access\" now all
pbrun pbauthcl grant PBAUTH_P_A4_UTIL \$user 2 \"\$user needs access\" now all
printf '\e[8;50;100t'
"
Notice that the
"printf '\e[8;50;100t'"
is echoed out along with the lines into the terminal .
What this command does is automagically logs into a linux box at 192.168.1.2 and then opens up a terminal and prints the lines to the terminal.
i cut and paste the print statement "printf '\e[8;50;100t'" to resize the terminal.
When a user wants elevated access at the linux command prompt
What I do is type in user= and then cut and paste one of the pbun command and it gets executed at the linux command line - which works fine.
however it would be much cooler if the "printf '\e[8;50;100t'" would just execute and make the terminal larger instead of me having to echo it into the terminal and then cut and paste it into the same terminal to resize it.
I have tried a bunch of different permutations in the test file to get the printf statement to just run, instead of echoing it out.
bash -v echo "printf '\e[8;50;100t'"
bash ;
I get thse kinds of errors though
/usr/bin/printf: /usr/bin/printf: cannot execute binary file
/bin/echo: /bin/echo: cannot execute binary file
how do I execute the "printf '\e[8;50;100t'"
command within the terminal instead of echoing it out ? There has to be a way .

Two possibile solutions that might work for you are the following:
Try -x
Try -o verbose

Related

How to run shell script located on Linux server from Windows environment?

I am trying to run a shell script located on a Linux server from Windows. The shell script does two things:
Do a sed command to replace text in an .sql file in the same directory.
Run the .sql file with sqlplus.
The shell script:
!/bin/sh
arg1=$1
arg2=$2
arg3=$(echo $arg1 | tr '[:lower:]' '[:upper:]')
arg4=$(echo $arg2 | tr '[:lower:]' '[:upper:]')
echo $arg1
echo $arg2
echo $arg3
echo $arg4
sed -i "s/$arg3/$arg4/g" sequence.$arg1.sql
sqlplus $arg2/$arg2#MYDB <<EOF
#sequence.$arg1.sql
exit;
(My database is located on the same Linux server.)
1) Script runs correctly when I log in to the server via MobaXterm
Connect to server with userID.
Set my_env.
cd to the shell script's directory.
Run script with ./myscript.sh with arguments.
2) Same shell script runs successfully via .cmd manually
Create a Windows script test.cmd on my Windows PC.
In the .cmd file I have the line:
plink.exe -ssh userID#Server
After the console window pops up, I repeat the steps 2 to 4 and script runs successfully.
What I am failing to do so is to automate the whole process.
Here's the line in my .cmd file which I attempted:
plink.exe -ssh userID#Server /myfilepath/myscript.sh %arg1% %arg2%
I can see the arguments passed correctly using multiple echo in the shell script. However, the shell script fails to locate the .sql file.
Error log:
/mypath/myscript.sh[1]: !/bin/sh^M not found [No such file or directory]
myarg1value
myarg2value
:No such file or directory[myarg1value]
/mypath/myscript.sh[12]: sqlplus: not found [No such file or directory]
I also tried below, but unfortunately with same result:
plink.exe -ssh userID#Server -m command.txt
Where file command.txt contains:
. my_env
cd /filepath/
./myscript.sh %arg_with_actual_value%
I do not know why it is not working, especially when 2) works and the script is relatively simple.
Do I assume things incorrectly about plink (path, variable, etc.)?
Is Cygwin the only way out?
I tried not to rely on yet another tool as I have been using plink.
EDIT: While the line
sed -i "s/$arg3/$arg4/g" sequence.$arg1.sql
fails to run on the .sh, i can run it on the .cmd file itself via:
plink.exe -ssh userID#Server sed -i "s/%arg3%/%arg4%/g" /myfilepath/sequence.%arg1%.sql
Hence I am suspecting the problem comes from the .sh file not having the required components to run (i.e. set env variable, path, etc)
This is not a solution but partially fixed some issue, thanks to Martin Prikryl and Mofi's input:
in the command.txt, the following needs to be set:
ORACLE_SID
ORACLE_HOME
PATH
after these are set, the sqlplus and sed will work normally. However, passing values from .cmd through plink to Linux's shell script seems to have issue with the actual value being passed. The variable will have the assigned value along with some unreadable characters. In this case,
sqlplus $arg2/$arg2#MYDB
login fails because arg2 contains some other char.
#sequence.$arg1.sql
this line also fails as it will try to opens 2 files, one being called sequence.myvalue and another one called "%s", which i suspect the assigned variable contains some sort of unreadable nextline character.
EDIT: fixed, we can use the same treatment from sed - run sqlplus directly from plink instead of passing value and running a .sh script in Linux:
sqlplus $arg2/$arg2#MYDB #/myfilepath/sequence.%arg1%.sql

Open new terminal window and execute bash file

I have written a bash script 'A' that executes some commands,but after complete execution of script 'A',I want another bash script 'B' which is stored on desktop to be executed new terminal window after execution of 'A' is completed.What command should I write at end of script 'A'?So that script 'B' is executed in new terminal window. I have tried many commands such as gnome-terminal and konsole both with their various arguments but I couldn't make it work.
Say two commands instead of Script A and Script B I use ls -lh and echo
xterm -e "ls -lh; read ;xterm -e \"echo \"Hello_World\"; read \""
read is given just to stimulate human interaction and you can see the terminal appearing and closing.

how to execute ssh comand on .sh file?

I trying to create a .sh file that execute things like "pwd" or "ls" command.
My problem its when i execute the .sh file.
Its seems not recognize the tasks
I tried to use echo
Example : echo 'lsApps' or echo "lsApps"
but it prints the name of the task instead execute the comand
for example i want to execute a .ssh file that makes a pwd
VAR_1=pwd
echo $VAR_1
but it prints me pwd instead the current path ...
Any idea?
echo is used to print on the screen (man page reference). If you do echo 'IsApps' it will take it as a string and print it. If you want to execute a command you can just do it by doing IsApps (acutes not quotes, acute is usually below the escape key). This will execute the command and show the output on the screen. If you want to store the output of the command in a variable, you can do
<variable_name>=`IsApps`
This will store the output in the variable. Note that there is no space between variable name and the command. Also, those are not quotes but instead acutes. To print the variable on screen you can use echo by doing echo $<variable_name>
If you don't want to see the output at all. You can do
IsApps > /dev/null
this will execute the command but you will not see any stdout on your screen.
As far as ssh is concerned, do ssh-keygen and then ssh-copy-id user#remote_ip to set ssh keys so that you don't have to enter your password with ssh. Once you have done that, you can use ssh user#remote_ip in your shell script.

Shell script: Excecute a command in "screen"

I use a USB-Dongle on my linux-server (raspberry).
To open the conecction and use the firmware, to do this, I have to open a terminal with "screen".
screen /dev/ttyACM0
There is no problem to open this connection with a shell script.
My problem is to execute a command in this screen.
The firmware has some command references for example V to get back the version number.
First of all, I tried a pipe (|)
screen /dev/ttyACM0 | echo "V"
But, this have no correct result.
Someone tell me I can use the screen command -X to execute something.
screen -T CUL /dev/ttyACM0
screen -X V
This seems to work.
The problem: "screen" gives an error message: -X: unknow command "V"
When I open the screen and entered the coammand V I get an output.
Some ideas?
Try this command:
screen -X stuff 'V'$(printf \\r)
(you also need the carriage return)

Read user input (which should be a linux command) and execute

I wish to write a simple csh script which loops through all computers in a network and executes a command that is input at the command line
echo -n "Please enter command you would like executed on all computers > \n "
set command = "$<"
say the user enters ls | grep something. How would I then execute this command in the following line? I tried
$command which works fine for input such as echo "Hello World". I get the following error for ls | grep something
ls: |: No such file or directory
ls: grep: No such file or directory
ls: something: No such file or directory
Ideally, I would want to enter several commands at the command line before looping through each computer in the network (which I can already do) and execute. Eg say I wish to copy two different files
sudo cp ./bin/elastix /usr/bin; sudo cp ./lib/transformix /usr/lib
Thanks
loops through all computers in a network and executes a command that is input at the command line
You would be perhaps using ssh so you would do something like:
ssh $hostname "$command"
I'm not a csh user so I may have the syntax wrong. For current machine you may use the eval shell command that should interpret any command sequences, not only simple commands.

Resources