Why does my ssh channel hang on read_to_end in Rust? - ssh

pub fn execute_command(
address: &String,
username: &str,
command: &Command,
buf: Option<&Vec<u8>>,
) -> anyhow::Result<Vec<u8>> {
let tcp = TcpStream::connect(address)?;
let mut sess = Session::new()?;
sess.set_tcp_stream(tcp);
sess.handshake()?;
sess.userauth_agent(&username)?;
let mut channel = sess.channel_session()?;
let commands = format!("{:?}", utilities::nixify(command));
trace!("channel acquired for {}", address);
channel.exec(&commands)?;
trace!("executed successfully command for {}", address);
if let Some(buf) = buf {
trace!("writing buffer at address {}", address);
channel.write_all(buf.as_slice())?;
trace!("wrote buffer at address {}", address);
}
let mut channel_output = Vec::new();
channel.read_to_end(&mut channel_output)?;
trace!("channel output len: '{}'", channel_output.len());
channel.wait_close()?;
trace!("channel exit status: '{}'", channel.exit_status()?);
Ok(channel_output)
}
The thing I'm executing on the remote machine outputs some binary data serialized using bincode. This code snippet never gets to the last trace "channel output len ..". My guess would be because it never received EOF, but how does one send an EOF to stdout? I tried flushing stdout on the remote but it didn't help.

Related

Arduino UNO WiFi Rev 2 not posting to SSL connections

I've been having an issue trying to get my UNO WiFi Rev 2 to make a POST request to my https azure server. I've been able to successfully POST to this server using a MKR1000 using the WiFi101 library, so it's weird that I can't get the UNO to POST using the WiFiNINA library. I've tried two approaches to this POST request, if anybody could offer a solution to either of them, I would appreciate it.
My first attempt was based off the WiFININA libary's example for HTTPS POST:
This example creates a client object that connects and transfers
data using always SSL.
It is compatible with the methods normally related to plain
connections, like client.connect(host, port).
Written by Arturo Guadalupi
last revision November 2015
*/
#include <SPI.h>
#include <WiFiNINA.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "secret"; // your network SSID (name)
char pass[] = "secret"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key index number (needed only for WEP)
int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "secret"; // name address for Google (using DNS)
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
int HTTP_PORT = 443;
String HTTP_METHOD = "POST";
char HOST_NAME[] = "secret";
//IPAddress server(secret);
String PATH_NAME = "/wtid/logCovid";
WiFiSSLClient client;
String queryString = "uuid=5555&manufacturerID=6666";
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to WiFi");
printWiFiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, HTTP_PORT)) {
Serial.println("connected to server");
// Make a HTTP request:
// MUST HAVE THESE HEADERS AND IN THIS ORDER!!!
client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(queryString.length());
client.println();
// Body AKA the data we are sending to the server
client.print(queryString);
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
// do nothing forevermore:
while (true);
}
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Here is the Serial monitor response whenever I upload that script:
Starting connection to server...
disconnecting from server.
My second approach was using the ArduinoHttpClient with a WiFiSSLClient, but again no luck:
#include <ArduinoHttpClient.h>
#include <WiFiNINA.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// WiFi Settings ///////
char ssid[] = "secret"; // your network SSID (name)
char pass[] = "secret"; // your network password (use for WPA, or use as key for WEP)
char serverAddress[] = "secret"; // server address
int port = 443;
WiFiSSLClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
void setup() {
Serial.begin(9600);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
}
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() {
Serial.println("making POST request");
String contentType = "application/x-www-form-urlencoded";
String postData = "uuid=4444&manufacturerID=5555";
client.post("/wtid/logCovid", contentType, postData);
// read the status code and body of the response
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
Serial.println("Wait five seconds");
delay(5000);
}
Here is the Serial monitor response whenever I upload that script:
making POST request
Status code: -2
Response:
Wait five seconds
I've updated the WiFiNINA firmware and uploaded the SSL certificates via the arduino IDE to the board as well, and I am able to make SSL GET requests to sites like google.com without issue. I do get Serial responses ensuring that I connected to the wifi, just didn't want to put that part of the Serial monitor response.
Thank you to anyone who can help!
:)

Correctly handle exceptions in Akka async continuations

To integrate async / task methods with akkling actor system I've written following method
let actorOfTask (t: 'a -> Task<'b>) =
(fun (ctx: Actor<_>) ->
let rec loop() =
actor {
let! data = ctx.Receive()
task {
try
let! t' = data |> t
return t'
with e ->
// TODO : Raise exception in order to supervisor can catch it
printf "Exception %O" e
return raise e
}
|> Async.AwaitTask
|!> ctx.Sender()
}
loop())
It is based on suggestion how to integrate actors with async workflows (example here https://github.com/Horusiath/Akkling/blob/0b5b0ffa4cd516407706ed230f81915452cdb183/tests/Akkling.Tests/Actors.fs#L124)
But issue here if async method throw exception it is not handled by parent supervisor
I'm expecting this code should work but it is not
let ss =
Strategy.OneForOne(fun error ->
printf "Error %O" error
Directive.Escalate)
let system = System.create "sys" <| Configuration.defaultConfig()
spawnAnonymous system { props (eventsHandler env) with SupervisionStrategy = Some ss }
Though if exception happens in regular actor flow (not integrated with async) this supervisor handle it.
Any suggestions how to fix it ?
Thanks
From Akka.Net documentation
https://getakka.net/articles/actors/receive-actor-api.html
WARNING
To complete the Task with an exception you need send a Failure message to the sender. This is not done automatically when an actor throws an exception while processing a message.
try
{
var result = operation();
Sender.Tell(result, Self);
}
catch (Exception e)
{
Sender.Tell(new Failure { Exception = e }, Self);
}
So actor should be implemented like this
let actorOfTask (sendTo: IActorRef<_>) (t: 'a -> Task<_>) =
(fun (ctx: Actor<_>) ->
let rec loop() =
actor {
let! data = ctx.Receive()
task {
try
let! t' = data |> t
return t' :> obj
with e ->
return Akka.Actor.Failure(Exception = e) :> obj
}
|> Async.AwaitTask
|!> (retype sendTo)
}
loop())

Disassociated exception in Akka.Remoting

Using Akka.net I am trying to implement simple scenario.
I have created 2 servers and 1 client, where Servers receives the messages sent from client and processes it.
Setup works fine sometimes and sometimes it gives following error, I am not able to figure out the cause:
**
No response from remote. Handshake timed out or transport failure detector triggered.
Cause: Unknown
Association with remote system akka.tcp://RemoteFSharp#172.27.**.94:8777 has
failed; address is now gated for 5000 ms. Reason is: [Akka.Remote.EndpointDisass
ociatedException: Disassociated
at Akka.Remote.EndpointWriter.PublishAndThrow(Exception reason, LogLevel leve
l)
at Akka.Remote.EndpointWriter.Unhandled(Object message)
at Akka.Actor.ActorCell.<>c__DisplayClass109_0.<Akka.Actor.IUntypedActorConte
xt.Become>b__0(Object m)
at Akka.Actor.ActorBase.AroundReceive(Receive receive, Object message)
at Akka.Actor.ActorCell.ReceiveMessage(Object message)
at Akka.Actor.ActorCell.AutoReceiveMessage(Envelope envelope)
at Akka.Actor.ActorCell.Invoke(Envelope envelope)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Akka.Actor.ActorCell.HandleFailed(Failed f)
at Akka.Actor.ActorCell.SystemInvoke(Envelope envelope)]
**
Client Config:
akka {
log-dead-letters-during-shutdown = off
actor {
handshake-timeout = 600 s
serializers {
wire = "Akka.Serialization.WireSerializer, Akka.Serialization.Wire"
}
serialization-bindings {
"System.Object" = wire
}
provider = "Akka.Remote.RemoteActorRefProvider, Akka.Remote"
}
remote {
helios.tcp {
maximum-frame-size = 20000000b
tcp-keepalive = on
transport-class =
"Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.Remote"
transport-protocol = tcp
port = 8760
hostname = 172.27.**.94
}
}
log-remote-lifecycle-events = INFO
}
Server Config :
akka {
log-dead-letters-during-shutdown = off
actor {
handshake-timeout = 600 s
serializers {
wire = "Akka.Serialization.WireSerializer, Akka.Serialization.Wire"
}
serialization-bindings {
"System.Object" = wire
}
provider = "Akka.Remote.RemoteActorRefProvider, Akka.Remote"
}
remote {
helios.tcp {
maximum-frame-size = 20000000b
tcp-keepalive = on
transport-class =
"Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.Remote"
transport-protocol = tcp
port = 8777
hostname = 172.27.**.94
}
}
log-remote-lifecycle-events = INFO
}
Also I am using Newtonsoft.Json for serialization as follows:
let CreateEmployeeActor (system: ActorSystem) actorName =
(spawn system actorName
(fun mailbox ->
let rec loop (count: int)=
actor {
let! message = mailbox.Receive()
let sender = mailbox.Sender()
let deserializedEmailData = JsonConvert.DeserializeObject<EmployeeActorMsgs> (message)
match deserializedEmailData with
| InItEmployee ->
//Some Logic
}
loop (0)
))

Outgoing connection stream closed

I have an actor with the behaviour:
def receive: Receive = {
case Info(message) =>
val res = send("INFO:" + message)
installAckHook(res)
case Warning(message) =>
val res = send("WARNING:" + message)
installAckHook(res)
case Error(message) =>
val res = send("ERROR:" + message)
installAckHook(res)
}
private def installAckHook[T](fut: Future[T]): Unit = {
val answerTo = sender()
fut.onComplete {
case Success(_) => answerTo ! "OK"
case Failure(ex) => answerTo ! ex
}
}
private def send(message: String): Future[HttpResponse] = {
import context.system
val payload: Payload = Payload(text = message,
username = slackConfig.username, icon_url = slackConfig.iconUrl,
icon_emoji = slackConfig.iconEmoji, channel = slackConfig.channel)
.validate
Http().singleRequest(RequestBuilding.Post(slackConfig.hookAddress, payload))
}
And a test
val actorRef = system.actorOf(SlackHookActor.props(SlackEndpointConfig(WebHookUrl,iconEmoji = Some(":ghost:"))))
actorRef ! Error("Some error message")
actorRef ! Warning("Some warning message")
actorRef ! Info("Some info message")
receiveN(3)
and in the afterAll() method I do a shutdown on the actor system using TestKit.
It works, the request makes it to the server, but there are errors from the akka streams part:
[ERROR] [06/26/2015 11:34:55.118] [SlackHookTestingSystem-akka.actor.default-dispatcher-10] [ActorSystem(SlackHookTestingSystem)] Outgoing request stream error (akka.stream.AbruptTerminationException)
[ERROR] [06/26/2015 11:34:55.120] [SlackHookTestingSystem-akka.actor.default-dispatcher-13] [ActorSystem(SlackHookTestingSystem)] Outgoing request stream error (akka.stream.AbruptTerminationException)
[ERROR] [06/26/2015 11:34:55.121] [SlackHookTestingSystem-akka.actor.default-dispatcher-8] [ActorSystem(SlackHookTestingSystem)] Outgoing request stream error (akka.stream.AbruptTerminationException)
Seems like since I have a Future completed the outgoing connection should be already closed, so is this a bug or am I missing sth?
You need to also shut down the http connection pools, something like
Http().shutdownAllConnectionPools().onComplete{ _ =>
system.shutdown()
}
Maybe the akka http testkit provides some helpers

How to interact with the script after its execution using Jsch

Scenario:
1.I am able to execute a bash script on remote ssh server successfully.
2.The script wants the user to enter some input's to proceed.
The Program hangs after the script is executed.
Q:1 Is this possible using JSCH or any other java based libraries?
Q:2 Which is the best library in java to handle such scenario?
Below is my piece of code :
public class SshMultiCommands
{
public void execute(String u,String h,String p) throws Exception
{
JSch jsch = new JSch();
String user = u;
String host = h;
String passwd = p;
int port = 22;
Session session = jsch.getSession(user, host, port);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
Channel channel = session.openChannel("shell");
OutputStream ops = channel.getOutputStream();
channel.setOutputStream(ops,true);
((ChannelShell)channel).setPtyType("vt102");
((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP");
PrintStream ps = new PrintStream(channel.getOutputStream());
channel.connect();
Thread.sleep(1000);
InputStream input = channel.getInputStream();
//commands
ps.println("ls -l");
ps.println("bash /opt/dla.sh");
ps.println("3"); // The sample user Input to script.This is getting printed but is not getting executed
printResult(input, channel);
ps.close();
channel.disconnect();
session.disconnect();
//System.out.println("OT Session Completed");
}
private static void printResult(InputStream input,
Channel channel) throws Exception
{
int SIZE = 1024;
byte[] tmp = new byte[SIZE];
while (true)
{
while (input.available() > 0)
{
int i = input.read(tmp, 0, SIZE);
if(i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed())
{
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try
{
Thread.sleep(300);
}
catch (Exception ee)
{
}
}
}
}
Solution: Put sleep of 5-10 sec after you run the script or program. And keep flushing the output stream after every command. If you already used sleep, extend its time.
The program or script that u run takes a bit of time to take over the system's standard input and out streams. If u pass all the commands and params immediately the parameters which were intented for the program goes to the system's bash instead !