fabric -- cant run commands after user is switched - linux

I have been recently introduced to fabric and trying to approach the following:
access remote host
su root
perform a command, for example change password for multiple users
done!
Please note that I cant use sudo, nor connect directly to the remote host using root. The commands I need to perform can only be performed if I explicitly change user to root.
I was able to approach the main concept of getting to the remote host and play with some commands, using fabric, but the problem im having is that once I switch to root "su root" I cant perform the rest of the commands unless I exit.
example of what im trying to approach:
def mytask():
with settings(user="root"):
run('whoami')
run('echo "TEST using root user"')
run('echo "ITS WORKING!!!"')
or something like this
def mytask():
run ('su root')
run ('passwd testUser')
In both cases once I enter the root password nothing would get executed, I would get the remote command line back, unless I exit back to the original user. I have seen few suggestions about using "fexpect" for prompts but not sure if that would make a difference.
I'm developing on a Linux environment.

You have to use fexpect and fexpect run command
from ilogue import fexpect
prompt = ['Password', 'mypassword'] # ('prompt', 'answer') Case sensitive
def test():
with fexpect.expecting(prompt):
fexpect.local("su -")

Related

Node.js execute shell command which requires an input after a few seconds

I'm trying to use the now cli to purchase a domain through my Node.js server but I cannot figure out how to execute a shell command in Node and to automatically insert the input when asked.
Here's my command using child_process.exec
const { stdout } = await exec(`now --token ${NOW_TOKEN} domains buy ${domainName}.${tld}`)
This does nothing because the command is waiting input (y/N) to confirm buying the domain.
They don't have any option in the commande to bypass the question like --yes.
How can I pass a string y + Enter when the process is waiting ?
Be careful. Your usage can certainly violate their ToS if you are reselling parts of their offering. You may need to reach out to their support channel for clarification.
Regarding the code. You can take a look at this file for inspiration: https://github.com/zeit/now/blob/master/packages/now-cli/test/integration.js
One alternative is:
stdin.write('y\n')

Passing Jenkins credentials through Node JS to shell script

My workflow goes like this :
I have a custom node module installed in my Jenkins slave that calls a few shell scripts . I would like to pass the credentials obtained through the credentials()
method provided by Jenkins declarative pipeline syntax.
What happens right now is that when I pass the username and password environment variables provided by the pipeline to the node module which in turn passes the arguments to the shell script which is used to CURL a file to a remote location
I get bad credentials failure.
But if I do the same thing by simply calling the shell script whithout passing thorugh the node_module the same creds work fine.
Is there any workaround for this behaviour ? I am just trying to understand here
Thanks in advance

The linux's permission

Description:
I used root account login in,and create a file (only root can write & read)in other account(if user is test)home directory.issues comming,when i use test account login in ,use 'vim' & 'wq!' command unexpectedly can save success!!,what happen ?why?
It is very difficult to understand your post. I'm really trying to, but I might interpret it wrongly:
You log in as root and create a file in user test's home directory?
Then, you log out from root and log in as test?
Then, as user test, you launch Vim and issue command "force write & quit?
You are perplexed as to why you are allowed to create a file as user test, inside a directory owned by user test?

How to create linux terminal like tutorialspoint?

TutorialsPoint Java Compiler
In tutorialspoint, they have created linux terminal using term.js.
I have integrated same github library in my project, it is working fine but I am trying to understand the flow of tutorialspoint.
My assumption:
In tutroialspoint each time they are creating new user_id under root user(cg) and running terminal(nodejs server) using that user_id so every time when you reload page there will be a different user_id (run whoami in terminal), so another user can't operate other users files.
I am running nodejs server using forever.js under root user, I want to implement same type of functionality. What is correct way to do this? and if there is another way please elaborate.
I think they are creating a new user each time you visit the page and providing you a subshell of that user. It can be easily achieve by using Shell Programming techniques. Creating a new user each time thing is probably nothing more than a security measure.
So I will briefly explain the concept in 5 steps:
1 - Create a new user:
shell_exec('useradd --expiredate 2016-09-10 [username]');
http://www.computerhope.com/unix/useradd.htm
2 - Login to this newly created user account:
shell_exec('su [username]');
3 - Get user input to the PHP script using AJAX(dynamically).
4 - Execute user's command and send the output to user:
<?php
$output = shell_exec("[user's command]");
echo "<pre>$output</pre>";
?>
5 - Repeat from 3.

How to listen to svn commit event on working directory on developer/client machine?

I need to write a node.js program that will somehow get triggered anytime a developer checks in code into svn. This will update a file in the working directory. The developers work on Mac OS X and Windows. The program needs to run on both machines.
Is there a way I can somehow listen to svn client's commit?
Are there any sdk for svn that allows plugin/extension?
Will watching .svn hidden directory (that svn creates for its own use) for changes do it? if so, how can I know by looking at this directory that a file was committed?
At first I thought hooks might be the way to go but hooks are run on the machine that host's svn and they are mostly for admin tasks such as sending email alerts or kicking off builds
First you need to understand that there is no way to know whether an update has occurred on the server without connecting to the server. Hence you cannot do it simply by looking at the local folders because that is not how svn works.
A workaround to achieve what you want would be the following. On the server, write a "post-commit" hook that takes a counter from a text file, increments it, and writes it back. Deposit this text file somewhere where your clients can download it. I'm going to assume this will be on "http://www.example.com/commit-id.txt". Then, on the clients, use a shell script that monitors this text file for changes and executes the desired action. Using windows powershell, for instance, this could work as follows. For Mac you need to use a different shell but I'm sure this script could be easily ported.
function get-current-commit-id {
trap [Exception] {
# Make sure the script doesn't freak out when server is down or connection
$commitid
return
}
# The rand.next() ensures by brute force that the text file is not cached
[int] $clnt.DownloadString("http://www.example.com/commit-id.txt?q="+$rand.next())
}
$clnt = new-object System.Net.WebClient
$commitid = get-current-commit-id
while( 1 ){
$commitidnew = get-current-commit-id
if( $commitidnew -ne $rsid ){
Write-Output "Commit occured on server"
### execute desired action here
### perhaps you'll also want to run "svn up"
$commitid = $rsidnew
}
### check for new update every 10 seconds
sleep 10
}

Resources