I am struggling to make a program on Python (Ubuntu) "To get a file from a directly connected Linux machine without prompting for Password"
Right now I am using this command on python but wanna put password in advance so it will not prompt me for password.
import os
os.system("echo 'hello world'")
os.system("rsync -rav pi#192.168.2.34:python ~/")
IP Address of Other Linux Machine is: 192.168.2.34
Password is: raspberry
Hostname: pi
You can achieve this by exchanging private keys. This way you can get a file from a directly connected Linux machine without prompting for Password. Here are the steps to exchange private keys:
Execute command ssh-keygen on your Ubuntu terminal.
Keep on pressing enter until something like this shows up:
The key's randomart image is:
+--[ RSA 2048]----+
| . .*|
| . + +o|
| + * + .|
| o E * * |
| S + + o +|
| o o o |
| . . . |
| |
| |
+-----------------+
After that execute ssh-copy-id pi#192.168.2.34 and enter password i.e., raspberry, if that is the password for the other machine.
Now execute python script as normal and it wont prompt for password.
import os
os.system("echo 'hello world'")
os.system("rsync -rav pi#192.168.2.34:python ~/")
You can try the following using pexpect and subprocess, the pexpect should definitely work, subprocess I am not sure:
cmd = "rsync -rav pi#192.168.2.34:python ~/"
from pexpect import *
run(cmd,events={'(?i)password':'your_password\r'})
from subprocess import PIPE,Popen
cmd = "rsync -rav pi#192.168.2.34:python ~/"
proc = Popen(cmd.split(),stdin=PIPE)
proc.stdin.write('your_pass\r')
proc.stdin.flush()
If you don't have pexpect installed use pip install pexpect
If you are on a private network (it should be as addresses are 192.168..), and if you trust all IP addresses on that network (means that no unauthorized user can spool an IP), you can also use host based authentication.
Extract from man page for ssh (I assume you use it as the underlying protocol for rsync) :
Host-based authentication works as follows: If the machine the user logs in from is listed in /etc/hosts.equiv or /etc/shosts.equiv on the remote machine, and the user names are the same on both sides, or if the files ~/.rhosts or ~/.shosts exist in the user's home directory on the remote machine and contain a line containing the name of the client machine and the name of the user on that machine, the user is considered for login.
That is you put in pi home directory a file .shosts containing one line
name_or_ip_address_of_source_machine user_name_on_source_machine
if the file already exists, just add that line.
But ... you must understand that as for BHAT IRSHAD's solution, it implies that you are now allowed to pass any command on dest machine as user pi without password.
Related
I'm trying to run this PowerShell command via Python:
sid = utils.execute_powershell(settings.D01_DC1_PORT,
settings.D01_USER,
settings.PASSWORD,
'(Get-ADForest).Domains | '
'%{Get-ADDomain -Server $_}| '
'select domainsid')
The port, the user and the password are all valid. If I run the same script in PowerShell I see values.
Yet, via Python I get this error:
'Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running.'
What is wrong here?
If I change my script so that only one row "returns" from the query, the code passes:
sid = utils.execute_powershell(settings.D01_DC1_PORT,
settings.D01_USER,
settings.PASSWORD,
'(Get-ADForest).Domains | '
'%{Get-ADDomain -Server $_}| '
'select domainsid -First 1')
I want to automatically login an Azure system to access the virtual machine. I run the following code:
process_1 = subprocess.call(key.SSH_KEY + ' | ' + key.PASSKEY, shell = True) # Login to virtual machine
and receive the following:
/bin/sh: key.PASSKEY: command not found
azureuser#xx.xx.1x.1xx's password:
It believes the key.PASSKEY is another command, when it is the input for the azureuser#xx.xx.1x.1xx's password: part. How do I make sure that the key.PASSKEY is entered as the password automatically when this subprocess command is run?
The answer was found using the following video: https://www.youtube.com/watch?v=8QfD8V_-7ok
I did:
ch = pexpect.spawn(key.SSH_KEY)
ch.logfile = sys.stdout.buffer
ch.expect("azureuser#xx.xx.1x.1xx's password:")
ch.sendline(key.PASSKEY)
ch.expect("azureuser#vm")
ch.sendline('ls')
I am pretty new to Pyvmomi and vsphere automation.
I have been trying to automate the user and group creation in vsphere but could not locate the method in Pyvmomi which could help me automate the process of user creation.
I already have a user created in vcenter (abc#xyz.local)
This user has administrative privileges
Now, I want to create a session with user abc#xyz.local and add new users in Vcenter 'users and groups'. Once the new users are created, I have to add these users to different groups.
All these has to be done via automation using python.
Is there a way to automate this?
Unfortunately, the SSO API is all private and unavailable through pyvmomi and the rest of the SDKs.
As #Kyle Ruddy says, it looks like pyvmomi does not support SSO APIs. However, the golang alternative (govmomi) does. Govmomi also has an a CLI called GOVC which provides a nice wrapper to perform the following (and other things!):
Creating groups
Adding users to groups
Creating users
You could look at GOVCs source code and try and figure out the SOAP calls, but I think that would be more trouble than its worth.
If you are open to the idea of launching a bash commands from python then you could do the following:
import subprocess
import os
# Handy function for GOVC and assume GOVC is on your $PATH
def govc_runner(command):
my_env = os.environ.copy()
# Admin user will need to perform the commmands
my_env["GOVC_USERNAME"] = "abc#xyz.local"
my_env["GOVC_PASSWORD"] = "<ABC_PASSWORD>"
my_env["GOVC_URL"] = "https://<VCENTER>"
my_env["GOVC_INSECURE"] = "true"
process = subprocess.Popen(command, env=my_env, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
return output, error
# New group and user info
newUserUsername = "praseemol"
newUserPassword = "<PARASEEMOL_PASSWORD>"
newGroup = "prasGroup"
# Creating new group and user
govc_runner("govc sso.group.create " + newGroup)
govc_runner("govc sso.user.create -p '" + newUserPassword + "' '" + newUserUsername + "'")
govc_runner("govc sso.group.update -a " + newUserUsername + " " + newGroup)
# Check if it has worked
output, error = govc_runner("govc sso.user.id " + newUserUsername)
if newGroup in output:
print("Yay, it worked:\n" + output)
else:
print("Something went wrong :(")
Hope that helps!
You can automate shell(vcenter) execution by doing ssh through putty for creation of user in system domain of vcenter and mimic same using paramiko library of python.
Official docs to refer for system domain user creation:
https://docs.vmware.com/en/VMware-vSphere/6.0/com.vmware.vsphere.security.doc/GUID-4FBEA58E-9492-409B-B584-C18477F041D8.html
Commands to be executed on vcenter shell:
/usr/lib/vmware-vmafd/bin/dir-cli user create --account william --first-name william --last-name lam --user-password 'VMware1!'
Refer:https://williamlam.com/2015/05/vcenter-server-6-0-tidbits-part-9-creating-managing-sso-users-using-dir-cli.html
To connect to vcenter using paramiko:
How do you execute multiple commands in a single session in Paramiko? (Python)
Pick the answer by "This".
You can fetch the created user using powercli commands:
Get-VIAccount
While using this be sure to find your created user in system domain.
Get_VIAccount -Domain 'domain_name'
The default domain name is usually like: "vsphere.local"
You can also find your domain by using putty to vcenter, enter shell and write,
"sso-config.sh -get_identity_sources"
You will be able to read Sys_Domain: '......'
You can assign role to user using powercli:
Get-VIPermission
If You can automate local user creation, let me know:
https://docs.vmware.com/en/VMware-vSphere/6.7/com.vmware.vsphere.vcsa.doc/GUID-533AE852-A1F9-404E-8AC6-5D9FD65464E5.html
I have to login to bastion Linux host then run kinit and beeline using pbrun, then sftp csv file to Windows.
Query sample:
"SELECT * FROM db.table WHERE id > 100"
Is there a Python script or tool to automate this?
You could put yours query to file, for example hive_script.sql
and run his from terminal
hive -f hive_script.sql
I want to post my findings.
The most difficult part was figuring out expect+pbrun.
Because there are 2 interactive questions I had to pause for a sec after first question.
My expect code:
#!/usr/bin/expect -f
set timeout 300
set usr [lindex $argv 0];
set pwd [lindex $argv 1];
set query_file [lindex $argv 2];
spawn -noecho pbrun $usr &
expect -re "Password:"
send "$pwd\r"
sleep 1
expect "Enter reason for this privilege access:"
send "test\r"
send "kinit -k -t /opt/Cloudera/keytabs/`whoami`.`hostname -s`.keytab `whoami`/`hostname -f`#YOUR_FQDN_NAME.NET;ssl=true\r"
send "beeline -u 'jdbc:hive2://bigdataplatform-your_dev.net:10000/;principal=hive/bigdataplatform-your_dev.net#YOUR_FQDN_NAME.NET;ssl=true' --outputformat=csv2 --verbose=false --fastConnect=true --silent=true -f $query_file;\r"
expect "*]$\ " {send "exit\r"}
expect eof
Query:
select * from gfocnnsg_work.pytest LIMIT 1000000;
The rest is Python and paramiko.
I create Transport object, execute expect script, and save standard output on Windows OS.
Data access path:
Windows desktop->
SSH->
Linux login->
pbrun service login->
kinit
beeline->
SQL->
save echo on Windows
Here's a Python script with details: hivehoney
My program is running inside a VMware virtual machine, my purpose is to get some information about the machine which this vm is hosted on.
I've already done some googling and find a library called pyVmomi.
But I still can't figure out how to get the information I want.
The samples are almost all about getting all vms or all hosts, and there is not obvious way I can adapt them for getting information about the current machine.
Assuming your VM (that is running this pyVmomi script) is running some version of Linux you could use something like dmidecode to find the UUID.
import subprocess
from pyVim import connect
proc = subprocess.Popen(["sudo dmidecode|grep UUID|awk '{print $2}'"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
uuid = out[:-1]
SI = None
SI = connect.SmartConnect(host=ARGS.host,
user=ARGS.user,
pwd=ARGS.password,
port=ARGS.port)
VM = SI.content.searchIndex.FindByUuid(None, uuid,
True,
False)
HOST = VM.runtime.host
print "Host name: {}".format(HOST.name)
What this will do is execute a system command on the Linux box to find the UUID. VMWare uses the BIOS UUID as the default UUID so dmidecode should work here. Next it will connect to a given vSphere host (in this example I assume a vCenter, but an ESXi host should provide the same results here). Next it will search the inventory looking for a VM with a matching UUID. From there it calls the runtime.host method which will return the HostSystem for the VM. Please note due to clustering that host could change.
This should help, install pynetinfo and pass device to the function
#!/usr/bin/python
import netinfo
def get_route( interface ):
r = []
for routes in netinfo.get_routes():
if routes[ 'dev' ] == interface:
r.append( routes[ 'dest' ] )
return r
print get_route( 'wlan0' )