Execute SSH command on Ubuntu from C# - asp.net

I want to execute a SSH command from an ASP.NET application. I found Tamir.SharpSsh. In my case the Ubuntu server only allows key based login so i am using the following code but that throws a connection timeout error. my code is as follows
string cmd = "cd projects/test;sudo python parallel.py";
SshExec sshexec = new SshExec("IP Address", "", "");
sshexec.AddIdentityFile(Server.MapPath("~/assets/mykey-openssh"), "key-phrase");
sshexec.Connect();
sshexec.RunCommand(cmd);
Appreciate if someone can point me in the correct direction.

Related

How to run sqlplus on oracle via R

I am running SQL-code on a oracle database. Some commands require to run them via sqlplus. Is there a way to avoid my commandline solution but directly running sqlplus via, e.g. dbSendStatement().
Pseudo code to not share any sensible information
# Via dbSendStatement ------------------------------------------------------------------------------
con <- odbc::dbConnect(odbc::odbc(),
Driver = "oracle",
Host = "HOST",
Port = "PORT",
SVC = "SVC",
UID = Sys.getenv("USRDWH"),
PWD = Sys.getenv("PWDDWH"),
ssl = "true",
timeout = 10)
# Error
odbc::dbSendStatement(con, "EXEC SQL CODE")
# actual error message:
#> Error in new_result(connection#ptr, statement, immediate) :
#> nanodbc/nanodbc.cpp:1594: 00000: [RStudio][OracleOCI] (3000) Oracle Caller Interface: ORA-00900: invalid SQL statement
# Via system command -------------------------------------------------------------------------------
cmd <- paste0("sqlplus ",
Sys.getenv("USRDWH"), "/", Sys.getenv("PWDDWH"),
"#", "HOST", ":", "PORT", "/", "SVC", " ",
"#", "EXEC script.sql")
cmd
#> [1] "sqlplus USR/PWD#HOST:PORT/SVC #EXEC script.sql"
# Works
system(cmd,
intern = TRUE)
Code like that always connects directly to the database. sqlplus is a specific client tool; it doesn't have its own API for those kind of interactions. In other words, you always connect to the database; you can't connect to sqlplus as it is not a service.
Your best option would be to convert your SQL in such a way that you can run it natively in your code using a direct database connection (i.e. don't use sqlplus). If your SQL commands cannot be adapted, then you will need to write a shell interaction to manipulate sqlplus as you did with cmd in your example.
That said, this implementation in your example is very insecure, as it will allow anyone with access to your host to see the database username, password, and connection info associated with the process while it is running. There are much more secure ways of scripting this, including the use of an auto-open Oracle Wallet to hold the credentials so you don't have to embed them in your code (which is always a bad idea, too).
Using Oracle Wallet, your cmd call would then look more like this:
sqlplus /#TNS_ALIAS #EXEC script.sql
This is still not perfect, but is a step or two in the right direction.

Send a command to an ubuntu server from my website

I have a wordpress website (I can make a new one if its not compatible) I want to send a command from there to my remote ubuntu server how would I do this.
Your issue will be that to start the service you need escalated privileges. You'll need to add an exception to your sudoers.
You could add something like this to /etc/sudoers.d/mycommand
Defaults:www-data !requiretty
Cmnd_Alias MYCOMMAND = /usr/bin/service myservice start
www-data ALL=(ALL) NOPASSWD:MYCOMMAND
Then you can use a php command such as
exec("/usr/bin/sudo /usr/bin/service myservice start");

Connection to MySQL Server using RMySQL Library through Bastion in R

On my local machine, I have ssh into the bastion where I can then connect to the remote MySQL server. I know that this is working because in terminal, it says that I have successfully connected and when I use an app like SQLPro and attempt to connect to the MySQL server with the correct permissions, I am able to successfully log in. Also, the command line
mysql -u username -p
works after I ssh.
Now, I am trying to use the library RMySQL to connect to the server and using
con<-dbConnect(MySQL(), user = "username", password = "pw", host = "127.0.0.1")
I get the return
Error in .local(drv, ...) : Failed to connect to database: Error: Can't connect to MySQL server on '127.0.0.1' (61)
It seems that R cannot determine that I have connected to the bastion. I say this because I have used the line above before on the remote server and it worked just fine.
con<-dbConnect(MySQL(), user = "username", password = "pw", host = "localhost")
If you have a workbench then go to server-> client connection and check the Host name. Your host name might be incorrect
I'm running R on linux.
After a few hours of searching, the following documentation for AWS finally gave me the command I needed to connect to an RDS instance via an AWS bastion host:
https://aws.amazon.com/premiumsupport/knowledge-center/rds-connect-using-bastion-host-linux/
The "syntax 2" at the above link worked for me to set up the tunnel:
ssh -i "Private_key.pem" -f -N -L 3306:RDS_Instance_Endpoint:3306 ec2-user#EC2-Instance_Endpoint -v
This successfully forwarded my local port 127.0.0.1:3306 to the RDS port 3306.
I then connected to the RDS instance from within R with just:
cn = dbConnect(RMariaDB::MariaDB(), user = "myDataBaseUserName", password = "myPassword", host = "127.0.0.1", dbname = "mySchemaName")

Powershell remote install .exe file errors and not finding setup.iss file

Hi I am trying to figure out how to install .exe files to 5 server machine but I am having trouble trying to install silently on my own machine.
I have this command
Invoke-Command -ScriptBlock {Start-Process -FilePath \\xxx-STUDENT3-W7\Users\bkoo004\Documents\test\ccleaner402.exe \r}
but I can't find the setup.iss file in the Windows folder.
Also when I use this command
Invoke-Command -computername xxxxxxxxxxx.edu -ScriptBlock {start-process -filepath "\\xxx-S
TUDENT3-W7\Users\bkoo004\Documents\test\ccleaner402.exe" } -Credential $cred
It gives me an error saying that
This command cannot be executed due to the error: The network name cannot be found.
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
But I know that network name is right because when I run
Invoke-Command -computername xxxxxxxxxxx.edu -ScriptBlock {get-process } -Credential $cred
It returns the get-process of that server.
I figured that for not getting the setup.iss file it is because the program that i am trying to install doesn't use installshield but for the error trying to run start-process on my remote server I have no idea what it is.
Not sure if you are running into the double-hop problem on not, but it sounds like you are. So I though I'd give you a little more information about it. The Bob Loblaw version.
What is a server and what is a client? A server, it accepts things, is the computer you remote onto. A client, it gives things, is the computer you use to do the remoting. So in the command Invoke-Command -computername xxxxxxxxxxx.edu ..., "xxxxxxxxxxx.edu" is the server.
From your description, it looks like you already ran the command Enable-PSRemoting on your server. With remoting enabled on the server you should be able to do Enter-PSSession -ComputerName xxxxxxxxxxx.edu and have an interactive command prompt on the client.
If you enter a remote session and do Get-ChildItem "\\ComputerName\Share" the command is going to fail (it fails for safety reasons). That's the double-hop, because you're going from one computer to another. The network share is another computer. So you're going like this:
Client -> Server -> Network Share
Hippity-Hoppity
You need to setup more "things" to fix the double-hop. First on your server(s) you need to run the command Enable-WSManCredSSP Server so it will accept credentials from clients. Second on your client(s) you need to run the command Enable-WSManCred -Role Client -DelegateComputer * so it gives out your credential to servers.
Now with CredSSP configured to give and accept credentials, you should have resolved the doulbe-hop.
Enter-PSSession -ComputerName Computer1 -Authentication Credssp -Credential (Get-Credential)
Now you should be able to get to your network shares from the remote session Get-ChildItem "\\ComputerName\Share".
Hope this helps you out a bit.
P.S. There is always money in the banana stand.

Password SSH authentication method in RCurl

I'm using the ftpUpload function in the RCurl package to upload files to an sftp file server. I'm having difficulty working out the authentication call.
Below is my call:
ftpUpload(what = "some-file.png",
to = "sftp://some-ftp-server.com:22/path/to/some-file.png",
verbose = TRUE,
userpwd = "my_userid:my_password")
As a result I get:
* About to connect() to some-ftp-server.com port 22 (#0)
* Trying some-ftp-server.com... * connected
* Connected to some-ftp-server.com (some ip address) port 22 (#0)
* SSH authentication methods available: publickey,password
* Using ssh public key file /home/.ssh/id_dsa.pub
* Using ssh private key file /home/.ssh/id_dsa
* SSH public key authentication failed: Unable to open public key file
* Authentication failure
* Closing connection #0
Error in function (type, msg, asError = TRUE) : Authentication failure
I wasn't the one to setup the sftp server, and I'm somewhat of an ssh noob -- apologies. What I do know is that I'm able to login using my_userid and my_password with Filezilla and that the server has an .htaccess and .htpasswd file.
I'm hoping that there is some way to authenticate using ftpUpload with just my userid and password. It seems that password is one of the two available methods, but I can't seem to get ftpUpload to understand that I'd like to use the later alone.
The .htpasswd file seems to contain my_userid:my_password, though the password portion is encrypted. I'm open to loading that in a certain place for ftpUpload to access, but I'm not sure how to point ftpUpload in the right directions.
Finally, I've tried playing around with and looking through the libcurl options listed here: http://www.omegahat.org/RCurl/philosophy.html and more fully explained here: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
Alas, no luck. Any help appreciated!
It seems a little late, but I am currently trying something similar (public key authentication, though) and got it working!
Here is what I did:
I did set up a public key authentication basically following the instructions at http://www.howtoforge.com/set-up-ssh-with-public-key-authentication-debian-etch
That is, I run on the server the command
ssh-keygen -t rsa -C "e#mail.com" -f "ir_rsa"
to generate a public and a private key. (Just as a sidenote, I first tried to use
puttygen on my local windows machine to create working keys but failed.)
Then I
add the public key to the authorized keys (still on the remote server)
mkdir ~/.ssh
chmod 700 ~/.ssh
cat id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
and copy both the public and private key file on the local machine where
R is running. Then I can upload a file from R using
require(RCurl)
ftpUpload(what = "myfile.txt",
to = "sftp://myusername#my.host.com:22/path/to/myfile",
verbose = TRUE,
.opts = list(
ssh.public.keyfile = "path/to/local/pubkeyfile",
ssh.private.keyfile = "path/to/local/privatekeyfile"
)
)

Resources