Interactively call SCP from Powershell - powershell

I've been banging my head against a powershell shaped wall all day. For work reasons, I want to be able to upload a file from a windows machine to a remote linux box. There's some work to do before so I foolishly believed I could just drop in a call to SCP in my script. But it doesn't like it one bit:
Putting
scp api.zip ${user}#${currEnv}:/${uploadLocation}
where user is the remote login name, currEnv is the ip address and uploadLocation is /tmp (at least while I'm debugging)
in the code just causes the execution to hang completely - in fact the same thing happens if I run that command from the PS command line, even if I manually replace all the variables.
I tried replacing it with pscp which works better in that it actually connects, but then throws up the usual warning about this being the first time I've connected and should type 'y' to proceed (again, even from the command line iteself) - but that won't let me type anything in. I've also even tried using wsl scp ... to run it (don't tell the IT guy), but that also seems to hang - it shows me the server banner, but never gets to the bit where it should be asking for my remote password.
Am I missing something?

Related

Issue with PowerShell/Linode Terminal

I am accessing my linode (Ubuntu 16.04 LTS) with "LISH Console via SSH", using PowerShell to do the SSH. I do not know if this is a problem with PowerShell, or the LISH Console I am SSHing to. I can connect fine and am able to run commands fine, however there is a small annoyance when I try to type longer commands. Below is what a longer commmand should look like:
user#host:~# scp -r local.directory.that.i.want.to.copy other.directory.im.copying.to
OR
user#host:~# scp -r local.directory.that.i.want.to.copy ot
her.directory.im.copying.to
However, as I type that command out, I reach some sort of char limit...
user#host:~# scp -r local.directory.that.i.want.to.copy ot
Which then continues on the same line that I was typing on:
her.directory.im.copying.toirectory.that.i.want.to.copy ot
While it does still work, it gets a bit annoying that I can't really see what I've typed. Are there settings I can adjust within the LISH access terminal?

Redirect WSL input in Powershell

I've been trying to write a powershell script that automates my windows workspace setup and configuration and am currently stuck trying to redirect input to WSL when executing it for the first time. The core of the problem is that Ubuntu's first launch prompts for a username and password, then logs in to a bash shell. I tried writing down the input lines into a text file like so:
Username
Password
Password
exit
Then, I tried redirecting the input of wsl to the file:
Start-Process ubuntu2004.exe -RedirectStandardInput stdin.txt -NoNewWindow -Wait
The above didn't work as executing WSL just starts spamming Enter new UNIX username: adduser: only one or two names allowed. I tried doing same in CMD with the < input redirection, but the result is the same.
This is not exactly the answer to your question, but in my opinion, ansible is better suited for such a task.
I myself recently became interested in assembling a workspace in wsl and ansible seemed to me the best solution.
Before starting the build, you will need to perform minimal steps (create a user and install several packages, all this can be placed in the readme), but then there will be no restrictions.
You can find several ready-made examples of wsl assembly via ansible on github.
A few ideas for setting the username/password in a new Ubuntu WSL instance:
First, a "PowerShell sendkeys" via COM or Interop might work for this. It's probably the closest in behavior to what you are actually asking.
Second, and perhaps most promising, I just tried this with a new Debian WSL installed from the Store (since I didn't want to mess with my Ubuntu install).
When running debian.exe (like ubuntu2004.exe), I let it run the install, then I Ctrl+C'd out of it when it started asking for the default username/password. At that point, the WSL instance is installed, but with only root. I assume that your script can let the command run for a certain period of time, then kill the process to replicate this.
From your script, you should then be able to run wsl -u root useradd --create-home --user-group --groups adm,dialout,cdrom,floppy,sudo,audio,dip,video,plugdev,netdev --password "encryptedPassword" username (see here for creating the encrypted password). I think that will get you a stock Ubuntu user the way that WSL sets it up.
You'll then need to either create a /etc/wsl.conf file (instructions) letting the instance know that that user is the default, or LxRunOffline lists this as one of its features.
But I'd also throw in that you might just want to keep a "backup" of an existing WSL instance that you start from. Do a wsl --export <distroname> <imagename.tgz>, then you can import it when setting up the new Windows host by copying the tgz over and doing a wsl --import <DistroName> <DirectoryWhereYouwantItToLive> <imagename.tgz>.
If you want, you can keep this image up to date with your desired WSL configuration, so that you don't have to recreate it when you rebuild the Windows hosts. That said, this is where I do follow #Mystic's suggestion of using Ansible to store my WSL "configuration as code". It allows me to not only recreate my WSL instances, but also that same configuration when I set (or reset) a Linode host or another Linux system.

Python subprocess call sudo passwordless

I am currently working in a remote environment which has a user with certain sudo permissions for which it does not require inputting any password.
Specifically it can perform some nginx commands without the need of a password, so far so good. The problem I face comes when I have a python script that takes care of checking for updates and if there are changes tries to reload nginx. As seen below
Popen(['sudo', 'nginx', '-T'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
call(["sudo", "systemctl", "reload", "nginx.service"])
When the script reaches one of this steps a prompt pops up on the terminal asking for a password, which should not be necessary since the user running the script has the permissions to run the commands without inputting it.
Question: Is there maybe a way, without hardcoding the password in a variable, to tell the script to "inherit" the permissions of the user when I run it?
Some additional information:
If the script itself is invoked with sudo everything works, but sadly this is not an option and sudo should be used only for the specific parts that completely require it (nginx reloads)
Python version is 3.5
EDIT: Solved this thanks to a friend:
The solution was to add the 'shell=True' paramater to the Popen.
Popen('sudo nginx -T'], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
The issue might be, that there is a mismatch in the sudoers nginx-path and the PATH variable used during script execution.
Try to either match the Popen call to the full PATH given in the sudoers file or adjust the PATH in the shell calling the Python script.

Continue/run commands after ssh into VM

I have to take quite a few steps before I get into the file I need to be, which is why I'm trying to set up an alias in my terminal, that gets me to the file by running that alias.
The following steps are needed to arrive where I have to be:
cd Sites
vagrant ssh
cd /var/www/miniportal.billetten.dk/logs/
sudo -s
cd /etc/apache2/sites-available/
nano 25-av_miniportal.conf
Edit line 33 in that file (I guess it's possible to jump to that line)
I tried setting up an alias like this, but the problem is that it stops running the rest of the command after I SSH'd into Vagrant. if I manually exit Vagrant, it continues the command (and of course returns an error, because there is no such folder).
The question is: How do I make sure that everything from step 3 is executed AFTER step 2 is done logging in through SSH?
My ultimate goal is to set up an Apple Automator program that lets me put in a value that gets entered on line 33, but I'm fine with just an alias for now.
I know I asked this question a long time ago, but in the meantime I found a solution and forgot I had posted this question.
My alias in my .zshrc-file looks like this:
alias changeCust='ssh -t root#192.168.56.101 "nano +32 /etc/apache2/sites-enabled/25-av_miniportal.conf && service apache2 reload"'
In other words, it SSHs into vagrant as root (it asks for my password), nanos into a file on line 32 (or whatever line you need), then, when the file is saved, it reloads apache2 and the changes are applied.
Just use the below one and change the values.
alias AliasName='ssh -t root#your.ip.addres.here "nano +lineNumber /path/to/file"'

Is it possible to have Perl run shell script aliases?

Is it possible to have a Perl script run shell aliases? I am running into a situation where we've got a Perl module I don't have access to modify and one of the things it does is logs into multiple servers via SSH to run some commands remotely. Sadly some of the systems (which I also don't have access to modify) have a buggy SSH server that will disconnect as soon as my system tries to send an SSH public key. I have the SSH agent running because I need it to connect to some other servers.
My initial solution was to set up an alias to set ssh to ssh -o PubkeyAuthentication=no, but Perl runs the ssh binary it finds in the PATH instead of trying to use the alias.
It looks like the only solutions are disable the SSH agent while I am connecting to the problem servers or override the Perl module that does the actual connection.
Perhaps you could put a command called ssh in PATH ahead of the ssh which runs ssh as you want it to be run.
Alter the PATH before you run the perl script, or use this in your .ssh/config
Host *
PubkeyAuthentication no
Why don't you skip the alias and just create a shell script called ssh in a directory somewhere, then change the path to put that directory before the one containing the real ssh?
I had to do this recently with iostat because the new version output a different format that a third-party product couldn't handle (it scanned the output to generate a report).
I just created an iostat shell script which called the real iostat (with hardcoded path, but you could be more sophisticated), passing the output through an awk script to massage it into the original format. Then, I changed the path for the third-party program and it started working fine.
You could declare a function in .bashrc (or .profile or whatever) with that name. It could look like this (might break):
function ssh {
/usr/bin/ssh -o PubkeyAuthentication=no "$#"
}
But using a config file might be the best solution in your case.