SSH using Mobaxterm in python - python-3.x

Am trying to automate a SSH access to a remote server with Mobaxterm in Python.
I could launch Mobaxterm and send the SSH command to it using below command
import subprocess
import time
moba_path = "C:\Program Files (x86)\Mobatek\MobaXterm\MobaXterm.exe"
output = subprocess.Popen(f'{moba_path} -newtab "ssh -t -l ubuntu server1 hostname"', shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
And I get the below expected output, with the "hostname"
How do I return back the response, so that I can check if the SSH access was successful or not ?
.

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.

How to provide sudo command/password once after connecting to the server using python

Once after connecting to the unix server with my id credentials, I am trying to login as sudo user by providing sudo command and password. I tried different ways, but I am not successful. Below is my code
import paramiko
import time
#define SSH
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(policy=paramiko.AutoAddPolicy)
#Connect to ther server
ssh.connect('server',22,username='username',password='password')
transport = ssh.get_transport()
#create a channel
chan = transport.open_session()
#interactive shell
chan.get_pty()
chan.invoke_shell()
print(chan.recv(2000))
#execute command
chan.exec_command('sudo S su - test server')
chan.send('password')
chan.send('\n')
chan.exec_command('whoami')
print(chan.recv(2000))
chan.close()
is there any other way to provide the sudo commanfd and password
I've always used paramiko shell to do such things. It would basically mean treating it like 'expect' where it checks for a prompt based on some regex pattern to see the completion of a command. Based on the response from the command output or the prompt, you can send the subsequent command.
Here's a more complete overview:
https://github.com/fgimian/paramiko-expect

inserting the Linux shell script file from windows to Linux box using powershell

so i am working on power shell and created the shell script file in C drive now i want to send that file using plink.exe by using power shell to Linux box.
Tried below commands
PS C:\> .\plink.exe -pw -i "R***t" root#192.168.1.12 ".\adduser.sh"
Unable to open connection:
PS C:\> .\plink.exe -i ssh "R***t" root#192.168.1.12 ".\adduser.sh"
PS C:\> .\plink.exe -i ssh "R***t" -P 22 root#192.168.1.12 ".\adduser.sh"
Unable to open connection:
PS C:\> .\plink.exe -i ssh "R***t" -P 22 root#192.168.1.12 ".\adduser.sh"
Unable to open connection:
basically how we can connect to Linux box usin g plink.exe through power shell
if we need any sshkey let me confirm and how can we generate the ssh
key for plink.exe and what are the options need to use for ssh key
so if any valuable suggestions appreciable and thanks in advance.....
plink is only a command-line interface for putty. Similar to the ssh command in unix/linux. You cannot upload/transfer files using plink.
You can use the pscp to do it.
Here's a reference to how you can create SSH keys to connect to your server without using a password.
https://the.earth.li/~sgtatham/putty/0.67/htmldoc/Chapter8.html#pubkey
plink is one tool in the entire suite of Simon Tatham's SSH package called PuTTY.
PuTTY provides SSH capabilities to WIndows users, and it comes with a bunch of compiled command line tools.
Plink is the equivalent of the SSH (secure (remote) shell) command on a linux machine.
Pscp is the equivalent of the SCP (Secure CoPy) command.
Psftp is the equivalent of SFTP (Secure File Transfer Protocol) which uses the same transfer methods as SCP but with an FTP like comman line interface.
PuTTY itself is a graphical tool that uses the same code as plink to create remote shells or commands over SSH or Telnet protocols (and many more), but it's probably a bit much to go into right now.
From the looks of it, it seems you are trying to get a shell script copied to your Linux server, so you should not be using the command to open a shell, but rather use the command to do a secure copy.
In short, use PSCP, not PLINK. (and maybe read the manual on it, so you get the arguments right)
TO insert the shell script file from windows machine to Linux box you need to download the "plink" file from internet and keep it in C:\drive and run the command like below
PS C:\> .\pscp.exe .\abc.sh root#1.2.3.4.:/root
after that you can use the plink to connect the linux box by using below command
PS C:\> .\plink.exe -pw "L****e" root#1.2.3.4 -P 22 "ls"
here "-pw" means password and "-P" means port number
I have briefly tried "Post-SSH" third-party module to Powershell, and it looks quite good. :)
Find-module Posh-SSH
Find-module Posh-SSH | Install-module
After that you can
$session = New-SSHSession -ComputerName "1.2.3.4" -Credential (Get-Credential)
Invoke-SSHCommand -Session $session -Command 'ls -l'
And finally close the session again
$session | Remove-SSHSession
http://www.thomasmaurer.ch/2016/04/using-ssh-with-powershell/

How to connect and run shell script on remote server, via shell script

I hardly know anything about shell script or terminal commands.
Want to achieve:
Make a shell script, that connects to remote server and after connecting to that server, run some commands on that server.
Is it possible?
I have tried googling, but didn't find something, i am looking for.
You can do this using ssh (secure shell), you can refer this question for answers How to use SSH to run a shell script on a remote machine?
/tmp/sh.sh is shell script on remote server.
#!/bin/bash
ssh "root#server-ip" 'sh -c "( (nohup /tmp/sh.sh) )"'
#use following for suppressing the output from remote server script.
ssh "root#server-ip" 'sh -c "( (nohup /tmp/sh.sh &>/dev/null ) & )"'

Send commands to Linux terminal with PuTTY

How can I send a command (not to open a file) with PuTTY that will run on a Linux terminal?
For example:
putty.exe 10.31.2.121 -l root -pw password | echo "Hi"
So that I will see the "Hi" on the Linux console?
Use plink.exe (from the developer of PuTTY).
And do something like the following
C:\putty\plink.exe user1#192.168.0.1 -pw P#55W0rD!
-m command.txt
I took this from Automate Cisco SSH connections with plink on Windows.

Resources