Auto Executing Script For linux? - linux

My Actual Problem was to auto execute a sh file to another host and return that output in my system. Is this possible??
"" I've to execute file # host2 and get write input # host1 ""

Use SSH:
piskvor#host1$ ssh piskvor#host2 "/home/piskvor/myscript.sh" > myscript.out
What I did here: from host1, I opened a SSH connection as piskvor to host2 and used it to execute /home/piskvor/myscript.sh (assuming it exists and I can run it). The output is redirected to file myscript.out at host1.
If you need password-less login, look into SSH keys.

Related

How to ssh into another machine immediately after logging into a machine

Problem: The location I'm working from cannot ssh into the master node for my application, say 10.30.30.1 which is hosted by another country. To solve this, I have a jump box which I can ssh from my location say 10.50.50.1
And after logging into the jump box, I again run the command ssh 10.30.30.1
Is there any way to save me a step of first logging into the jump box and then ssh into master node, because that's the only thing I use the jump box for.
Can I configure the jump box to directly run that ssh command the moment I log in?
Are there any commands which allow me to login directly to master via the jump box in a single command or putty / MOBA XTerm configuration?
Put the following lines into your ~/.ssh/config:
Host target-host
ProxyJump jump-host
This assumes both the server and the client running OpenSSH >= 7.3. For older versions you can use ProxyCommand and netcat instead of ProxyJump. netcat must be installed on the jump host:
Host target-host
ProxyCommand ssh jump-host netcat -w 120 %h %p
Say you first execute ssh server1, then on server1, you execute ssh server2. The thing you want is when you execute ssh server1 on your local computer, you can automatically login to server2.
The way to do this is add a script that runs automatically when you login to server1. You can do this by just adding the code ssh server2 into your ~/.bashrc file (if you are using Ubuntu. Or in other OS, the file name is similar to ~/.bashxxx).
But after doing so, you still need to type password for server2 every time you login into server1. If you don't want to type server2's password by hand, you can use a password-free ssh connection from server1 to server2. See this page to find out how.

Caputure the keystroke events of remote machine and display them on local machine

I have two linux let say A and B systems connected via SSH. On machine A I have a QR scanner connected to it. I want to scan the QR code on machine A redirect it to a file and then display the decoded sting on machine B. For this purpose what I am trying to do is, I do ssh from the local B machine which runs a script to redirect the stdin to a file to capture the decoded strings. It just invoke a script on remote machine which redirect stdin timeout 5 cat > qrCode.txt.
The real problem is when invoke the scripts locally on my machine A it works fine, but when I try to do the same from B machine on A using SSH, I get the file qrCode.txt on remote machine B but it's empty. Does not contain anything.
So how can I redirect the stdin of remote machine to a file and then read from that file to display the decoded string on local machine.
I guess the terminal is missing, because the Qr scanner flush out everything to terminal but when i do ssh, there is no access to terminal so it doesn't capture the keystrokes.
Is there anyway to achieve this?
Here is the ssh command on system B
#!/bin/bash
echo "Going to invoke remotes script"
sshpass -p <password> ssh user#192.168.2.10 -q -tt "/home/user/scanQr.sh"
used -tt parameter to avoid the warning pseudo terminal will not be allocated because stdin is not a terminal
and this is the script on remote machine A
#!/bin/bash
cd /home/user
cat > code.txt

Remote execute script on Linux

Wondering if it is possible to write a shell script like this, and if possible, any reference/sample implementation I can refer to? Thanks.
Step 1, scp a local file on a local box to a remote box ABC, may need
input password or we can hard code password into script possible?
Step 2, remote execute a script on box ABC, and leverage the file
uploaded in Step 1
Step 3, the output of Step 2 (which is on
console/stdout) is redirected to local box.
I tried this:
scp ~/Downloads/data/1.dat root#host:/root/data /root/exercise/test /root/data/1.dat
I understand that you want to copy a file to a remote machine, run a command there with that file as an argument and get the output on your local machine. Apparently, you need the test program, which is on the remote machine.
Your try takes you halfway there. You could do it as follows:
scp ~/Downloads/data/1.dat root#host:/root/data
ssh root#host '/root/exercise/test /root/data/1.dat'
The first command copies your file, the second runs the command on the remote machine. Depending on the test command, you can new get some output file back to your local machine:
scp root#host:/root/results/outputfile .
Or, if the command writes to standard out, you could redirect the output to a file on the remote machine by appending > /root/results/outputfile to the ssh command and then scp it back to your local machine.
You can execute commands using ssh, for example:
$ ssh user#host ls -la
will connect to host host as user, and after successful authorization execute ls -la command, presenting the output locally. After command finishes connection will be closed.

how to write a shell script to make an ssh connection to a machine and continue remaining script on that machine

I am writing a script where I run the script on one server through which I create an ssh connection to another machine and want to continue the following script code on the remote machine.... Can any body tell what is the way to do it?
if you want to execute a command on remote host through ssh and get the output on local host, that what i understand from you question then use.
ssh -n <hostname/IP> 'command'
where -n will Redirects stdin from /dev/null
you can store the output to a variable or file as
var =`ssh -n <hostname/IP> 'command'`
or
ssh -n <hostname/IP> 'command' >> output.txt
also if you want to send multiple command use ; for command separator.
NOTE: ssh without password should be enable from local host to remote host.
else you need to specify password explicitly.

execute command on ssh portforwading

I have a user that is only allowed to open a ssh connection for a port forwarding.
Example from the .ssh/authorized_keys:
no-pty,no-X11-forwarding,permitopen="localhost:1237",command="/bin/echo INTERACTIVE SHELL NOT SUPPORTED" ssh-rsa AAAA ...
Is there a way to execute a script as soon as this user opens the port forwarding? If the user would login with ssh I could use a .bashrc file or something like that. But in this case no shell is executed.
regards
frafu

Resources