python script to SSH into remote computer and run command - python-3.x

i'm new to python3 and scripting. I've got 2 commands I need to write a script for. what i'm looking to do is ssh into a remote computer and run a command.
i've tested it manually and works fine but i'd need to put it in a script. Thanks for any help in advance.
sshpass -p 'SecretPassword' ssh user#10.X.X.X
run a local command on remote computer
Bartibog

Related

How to do ssh and scp from linux server to Windows

I want to understand how can we do ssh and scp from linux server to windows server. Can we simply run ssh and scp command like below from linux server.
ssh user#IPaddressWindowsserver
scp /home/ubuntu/myfile username#IP_of_windows_machine:/C:/Users/Anshul/Desktop.
I also want to execute these commands from jenkins pipeline script, I am using windows as my node in jenkins.
Can I simply run like below using bat script.
bat 'ssh user#IPaddressWindowsserver'
bat 'scp /home/ubuntu/myfile username#IP_of_windows_machine:/C:/Users/Anshul/Desktop'
Note:- I want to do ssh from linux to windows only, not the other way round.

establish ssh connection and execute command remotely [duplicate]

I wish to run a script on the remote system and then wish to stay there.
Running following script:-
ssh user#remote logs.sh
This do run the script but after that I am back to my host system. i need to stay on remote one. I tried with..
ssh user#remote logs.sh;bash -l
somehow it solves the problem but still not working exactly as a fresh login as the command:-
ssh user#remote
Or it will be better if i could include something in my script that would open the bash terminal in the same directory where the script was running. Please suggest.
Try this:
ssh -t user#remote 'logs.sh; bash -l'
The quotes are needed to pass both commands to ssh. The -t option forces a pseudo-tty allocation.
Discussion
Consider:
ssh user#remote logs.sh;bash -l
When the shell parses this line, it splits it into two commands. The first is:
ssh user#remote logs.sh
This runs logs.sh on the remote machine. The second command is:
bash -l
This opens a login shell on the local machine.
The quotes were added above to prevent the shell from splitting up the commands this way.

Execute bash script on a remote-linux

I have a bash script lets say test.sh. This script contains the following:
#!/bin/bash
echo "khaled"
ads2 svcd&
This script simply prints my name (just for test purposes) and execute ads-service application in the background. When i run the script on my ubuntu, it works correctly. As a test i checked which programs run on the kernel
As you see. ads2 runs and has 12319 process-id.
Now what I'm trying to do is to run the script on the ubuntu, however remotely from a windows pc.
Therefore i opened command-line on windows and executed the following command:
ssh nvidia#ubuntu ip-address ~/test.sh
And i get the following
As you see the scripts run and prints khaled,however on windows command line and what i want is that the script is executed on the ubuntu. this justify why the lineads2 svcd& doe not do anything, neither on windows (which makes sense, since ads2 is installed on ubuntu) nor on linux.
So how can i execute the script on ubuntu ?
thanks in advance
Use the full path to start ads2. When using remote SSH your environment variables may be different than in a local shell.
#!/bin/bash
echo "khaled"
/home/nvidia/ads2 svcd&
Not sure where ads2 is located.
Try the following to locate it on your Ubuntu local shell.
command -v ads2
You may also need nohup to persist the process beyond the life of the SSH session.
If you have the script on the remote server and you want to run this, you would add back ticks,
ssh user#server './test/file.sh'
The script's output would be sent to your local machine, as if you ran the command from your local machine.

ssh command is showing no such file or directory when executing within shell script

I am trying to run a script in remote machine using ssh.
sshpass -p "" ssh abc#remote.com "bash -s" < path/file.sh
When I execute this command normally, It is executing.
But when i put this into another shell script and executing it then it showing
no such file or directory
I tried putting #!/bin/bash in the top of code also.
But no use. Please can anyone help me?
Actually second time it runs on your local machine not on remote machine try following
ssh abc#remote.com "cd /home && ls -l"

Linux: Run a shell command remotely and print result

I want to create a script that runs shell commands on a remote Linux machine and print out the result. Some thing like a "run once" SSH. Or for those familiar with Android development, something like "adb shell". For example, I want to run "ls" on my remote machine and display the results on the local host.
This is supported directly by ssh:
ssh [options] [user#]hostname [command]
eg
ssh user#host ls
If not command is given an interactive shell is usually run by default.

Resources