I have a script on my server named test.sh:
#!/bin/bash
read -p "Select an option [1-4]: " option
echo "You have selected $option"
When I run it through ssh manually, I see this:
me#me:~$ ssh root#server
root#server's password:
[...]
root#server:~# bash test.sh
Select an option [1-4]: 48
You have selected 48
When I run it as ssh remote command, I see this:
me#me:~$ ssh root#server 'bash test.sh'
root#server's password:
48
You have selected 48
I am unsatisfied with this output because it's missing Select an option [1-4]: prompt string and the original script which from has I derived test.sh contains a lot of interactive dialogue strings like this and I need them all.
I know that read prints it's prompt to stderr so I tried to start the script with following commands in case if stderr is omitted, but the output stays still the same:
ssh root#server 'bash test.sh >&2'
ssh root#server 'bash test.sh' >&2
ssh root#server 'bash test.sh 2>&1'
ssh root#server 'bash test.sh' 2>&1
Why this is happening and how to make ssh remote command work as expected?
UPD
I have changed the test.sh to this:
#!/bin/bash
echo Connected
read -p "Select an option [1-4]: " option
echo "You have selected $option"
but the output still missing the prompt string:
me#me:~$ ssh root#server 'bash test.sh'
root#server's password:
Connected
66
You have selected 66
You need to use -t option in ssh to assign a pseudo-terminal to ssh session:
ssh -q -t root#server 'bash test.sh'
Related
#!/bin/ksh
CTN=1
ssh -q user#host 'exec bash -s' << 'ENDSSH'
cd abc/def
./scriptname \$CTN
ENDSSH
exit;
However in the remote server, value of variable CTN is not getting passed.
Please help.
It should be:
CTN=1
ssh -q user#host 'exec bash -s' << ENDSSH
cd abc/def
./scriptname "$CTN"
ENDSSH
Since you want $CTN to expanded locally you must not escape the $ and must not put ENDSSH between single quotes.
I have a single command to ssh to a remote linux host and execute a shell script.
ssh -t -t $USER#somehost 'bash -s' < ./deploy.sh
Inside deploy.sh I have this:
#!/bin/bash
whoami; # I see this command echo
sudo -i -u someoneelse #I see this command echo
whoami; # I DON'T see this command echo, but response is correct
#subsequent commands don't echo
When I run the deploy.sh script locally all commands echo.
How do I get commands to echo after I sudo as another user over ssh?
Had to set -x AFTER sudo as another user
#!/bin/bash
whoami;
sudo -i -u someonelese
set -x #make sure echo on
whoami; #command echoed
I am trying to execute a command on a remote machine, grep the output, put it in a file and then close the connection.
This is what I have:
#!/bin/bash
sshpass -p ***** ssh user#remotemachine | grep 'TEXT' > output.txt
The script is stops and there is no output in output.txt
Any ideas how I can script this?
try this.
#!/bin/bash
sshpass -p ***** ssh user#remotemachine " grep 'TEXT' > output.txt"
in your shell script,you just use ssh login in your remove server,and after executing ssh command,then run grep in you local machine,so ,you get nothing
So for my script I want to ssh into a remote host and remain in the remote host after the script ends and also have the directory changed to match the remote host when the script ends.
#!/bin/ksh
ssh -t -X mylogin#myremotemachine 'cd $HOME/bin/folder1; echo $PWD; ssh -q -X ssh mylogin#myremotemachine; cd $HOME/bin/folder2; echo $PWD'
The PWD gets changed correctly before the second ssh. The reason for the second ssh is because it ends the script in the correct remote host but it will not retain the directory change which I attempted by putting commands after it but they won't execute.
Does anyone have any ideas?
Just launch a shell at the end of the command list:
ssh -t -X mylogin#myremotemachine 'cd $HOME/bin/folder1; echo $PWD; ssh -q -X ssh mylogin#myremotemachine; cd $HOME/bin/folder2; echo $PWD; ksh'
If you want the shell to be a login one (i.e. one that reads .profile), use exec -l:
ssh -t -X mylogin#myremotemachine 'cd $HOME/bin/folder1; exec -l ksh'
If the remote server uses an old ksh release that doesn't support the exec -l builtin and if bash or ksh93 is available, here is a workaround:
ssh -t -X mylogin#myremotemachine 'cd $HOME/bin/folder1; exec bash -c "exec -l ksh"'
I need a shell script that can take remote login in to a system and i can execute a bunch of commands in that system.
I made a script and actually it's working:
#!/bin/bash
USERNAME=KRUNAL
IP=10.61.162.241
ssh -l ${USERNAME} ${IP} "pwd "
ssh -l ${USERNAME} ${IP} "ls -la"
ssh -l ${USERNAME} ${IP} ./a.out
I have problem that if suppose i made script
ssh -l ${USERNAME} ${IP} "pwd " # this execute in remote system
ls -la # this execute in current system.
so every time i need ssh command to execute file on remote system.
Is there any way that i can run bunch of code in remote system with one time login.
You can send as much commands to ssh as you want, provided that you separate them with ; or linebreaks. So this should work:
ssh -l ${USERNAME} ${IP} "pwd; ls -la"
#Joao's suggestion works fine however its impractical when writing many lines.
If this is the case you can do
ssh -1 ${USERNAME} ${IP} bash << 'EOF'
cd /some/directory
./a.out
who am i
for i in `seq 1 10`
do
echo $i
done
EOF
Anything between 'EOF' and the final EOF will be executed in the server side.
You can also replace bash with csh or python and write code for that interpreter instead
If you want the output of the ssh session be stored in a file (say session.log) then replace
ssh -1 ${USERNAME} ${IP} bash << 'EOF'
with
ssh -1 ${USERNAME} ${IP} bash << 'EOF' > 'session.log'
rest remains unchanged