net-ssh and remote environment
have you tried something like:
ssh.exec!("source /home/you/.zshrc")
puts ssh.exec!("echo $PATH")
?
Losing environment variable set via ssh execute. Ruby & net-ssh
Each exec creates an environment on it's own, environmont variables are lost.
Like you do with your &&
(execute next command if first succeeds) or with ;
(execute anyway) you can chain commands.
You can also send a block like this to do multiple actions
Net::SSH.start("host", "user") do |ssh|
ssh.exec! "cp /some/file /another/location"
hostname = ssh.exec!("hostname")
ssh.open_channel do |ch|
ch.exec "sudo -p 'sudo password: ' ls" do |ch, success|
abort "could not execute sudo ls" unless success
ch.on_data do |ch, data|
print data
if data =~ /sudo password: /
ch.send_data("password\n")
end
end
end
end
ssh.loop
end
Or use the gem net-ssh-session.
Remote execution of SSH command hangs in ruby using Net::SSH for a particular command
I figured out how to fix it.
I added > /dev/null 2>&1
at the end of the command, and it worked.
cockroach start --background --max-sql-memory=.25 --cache=.25 --advertise-addr=%net_remote_ip%:%crdb_database_port% --certs-dir=%crdb_database_certs_path%/certs --store=%name% --listen-addr=%eth0_ip%:%crdb_database_port% --http-addr=%eth0_ip%:%crdb_dashboard_port% --join=%net_remote_ip%:%crdb_database_port% > /dev/null 2>&1;
Net::SSH::Multi using the session.exec, how do you get the output straight away? Ruby
Adding session.loop after session.exec allows the program to wait for the output.
Such as:
session.exec(command)do |ch, stream, data|
puts "[#{ch[:host]} : #{stream}] #{data}"
end
session.loop
# Or session.wait also does the same job.
Cant run Ruby script via gem ssh-net
The problem is in your ssh.exec!
call. Net::SSH
does not use a login shell to run your commands. A login shell is required for Bash to execute .bash_profile
.
Since your rbenv setup is in .bash_profile
, your exec!
call will be missing your full Ruby environment, and therefore running bundle
later in you restart
script will fail.
The solution should be to manually execute .bash_profile
, so that rbenv gets loaded:
output = ssh.exec!("source ~/.bash_profile; ...rest of commands...")
More information and other proposals here:
net-ssh and remote environment
How to access commands in bin folder through ruby net-ssh?
net-ssh and remote environment
you need to source .bashrc/.zshrc/ etc
ssh.exec!("source ~/.zshrc; /usr/bin/env ruby -v")
Using Net::SSH Ruby library to remote command execution that requires sudo su - another user
This is a bit of a sysadmin-y answer, but I think you are authenticating twice: once to log in as "master" (using master's keypair) and then a second time "master" sudo-ing the su to "appmanager" but using a password (hence the "lecture" message). But I think you're not answering the password challenge the second time. There are a few ways to get around this that come to mind:
1) Login as appmanager directly using that account's keypair. If you're worried about the security of the appmanager account, you can restrict ssh remote commands among other things.
2) As master, call a binary (not a script!) that is setuid as "appmanager" that simply calls the stopApp.sh script. An example.
3) Set the appropriate group that master is in to NOPASSWD in /etc/sudoers.
How to load environment variables in a remote AIX machine through ssh while running script from Jenkin pipeline?
This is solved with below changes.
Step 1: Edit ~/.ssh/environment. Add variable PERL5LIB="/path of the module/"
Step 2: Edit /etc/ssh/sshd_config. Change variable PermitUserEnvironment from no to yes. Uncomment it if commented. This will enable access of environment variables to SSH.
Step 3: Restart SSHD service. (This is imp. I had tried step 1 and 2 before also but not restarted the service so solution was not working)
We can create a script and run it before executing automation test from pipeline.
Related Topics
Native Threads in Ruby 1.9.1, Whats in It for Me
More Concise Version of Max/Min Without the Block
Advice on How to Validate Names and Surnames Using Regex
How to Get a Reference to a Method
Flash Message with HTML_Safe from the Controller in Rails 4 (Safe Version)
Garbage Collector in Ruby 2.2 Provokes Unexpected Cow
How to Make a Ruby Enumerator That Does Lazy Iteration Through Two Other Enumerators
Generating a Race Condition with Mri
Can't All or Most Cases of 'Each' Be Replaced with 'Map'
Rake Aborted! Undefined Method 'Migration_Error=' for Activerecord::Base:Class
Rails 5 - Using Polymorphic Associations - Rendering the Views
Rmagick Error After Installing Os X Mavericks
Cancan Abilities in Separate File
In Rspec, Using Let Variable Inside Before :All Block
Ruby: How to Call Function Before It Is Defined
Ruby Minitest Assert_Output Syntax
Ruby on Rails. Bundler. Cucumber. Rake Aborted! Command Failed with Status (1)